Codeforces 160D Edges in MST【思维+并查集+求桥(有重边)】

D. Edges in MST
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a connected weighted undirected graph without any loops and multiple edges.

Let us remind you that a graph's spanning tree is defined as an acyclic connected subgraph of the given graph that includes all of the graph's vertexes. The weight of a tree is defined as the sum of weights of the edges that the given tree contains. The minimum spanning tree (MST) of a graph is defined as the graph's spanning tree having the minimum possible weight. For any connected graph obviously exists the minimum spanning tree, but in the general case, a graph's minimum spanning tree is not unique.

Your task is to determine the following for each edge of the given graph: whether it is either included in any MST, or included at least in one MST, or not included in any MST.

Input

The first line contains two integers n and m (2 ≤ n ≤ 105) — the number of the graph's vertexes and edges, correspondingly. Then follow m lines, each of them contains three integers — the description of the graph's edges as "ai bi wi" (1 ≤ ai, bi ≤ n, 1 ≤ wi ≤ 106, ai ≠ bi), where ai and bi are the numbers of vertexes connected by the i-th edge, wi is the edge's weight. It is guaranteed that the graph is connected and doesn't contain loops or multiple edges.

Output

Print m lines — the answers for all edges. If the i-th edge is included in any MST, print "any"; if the i-th edge is included at least in one MST, print "at least one"; if the i-th edge isn't included in any MST, print "none". Print the answers for the edges in the order in which the edges are specified in the input.

Examples
input
4 5
1 2 101
1 3 100
2 3 2
2 4 2
3 4 1
output
none
any
at least one
at least one
any
input
3 3
1 2 1
2 3 1
1 3 2
output
any
any
none
input
3 3
1 2 1
2 3 1
1 3 1
output
at least one
at least one
at least one
Note

In the second sample the MST is unique for the given graph: it contains two first edges.

In the third sample any two edges form the MST for the given graph. That means that each edge is included at least in one MST.


题目大意:


给出N个点,M条边,然你判断这M条边,是三种类型的哪一种:

我们知道,最小生成树在有些图中是不唯一的:

①一条边在所有最小生成树方案中的存在,输出any

②一条边在所有最小生成树方案中都不存在,输入none.

③一条边至少出现在一种最小生成树的方案中,输出at least one


思路:


①我们首先将边按照权值从小到大排序,然后对应这个问题,我们每一次取出权值相同的所有边进行操作。


②对于每次操作,我们知道,如果一条边的两个点已经在集合中了,那么对应这条边就一定不用加入最小生成树中,而且当前边也不可能存在方案去替换集合中的解。

那么对于一条边的两个点不都在同一集合中的情况,我们需要判定这些边哪些作为any出现,哪些作为at least one出现。

我们知道,如果一条边可以被另外一条边所替换,那么就是作为at least one出现,那么这样的边是什么样子的呢?这条边一定会和同权值的边以及原图构成一个环才行,只有这样,才可能被同权值的其他边所替换,称为at least one的一员,否则,如果一条边是一个桥,那么对应就只能作为any出现。

这里我们需要调出原图跑的话,时间复杂度会非常高,既然我们已经用了并查集去搞集合,那么我们不妨在建边的时候,一条边a【i】.x,a【i】.y,我们直接将其祖先建立起来一条边即可:add(find(a【i】.x,a【i】.y));

然后根据这个图去找桥。


这个问题样例就有出现重边的情况,而市面上大部分的求双联通分量求桥的代码都只能处理没有重边的问题,这里小学了一手,大家注意一下写法。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
    int x,y,w,pos;
}a[1250000];
int ans[150000];
int n,m;
int cmp(node a,node b)
{
    return a.w<b.w;
}
/****************************/
struct node2
{
    int from,to,w,next;
}e[1500000];
int cont;
int head[150000];
int vis[150000];
int dfn[150000];
int low[150000];
int fa[150000];
int cnt;
void add(int from,int to,int w)
{
    e[cont].to=to;
    e[cont].w=w;
    e[cont].next=head[from];
    head[from]=cont++;
}
void Dfs(int u,int from)
{
    low[u]=dfn[u]=cnt++,vis[u]=1;
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        if(vis[v]==1&&e[i].w!=from)low[u]=min(low[u],dfn[v]);
        else if(!vis[v])
        {
            Dfs(v,e[i].w);
            low[u]=min(low[u],low[v]);
            if(low[v]>dfn[u])ans[e[i].w]=1;
        }
    }
}
/****************************/
int f[1250000];
int find(int a)
{
    int r=a;
    while(f[r]!=r)
    r=f[r];
    int i=a;
    int j;
    while(i!=r)
    {
        j=f[i];
        f[i]=r;
        i=j;
    }
    return r;
}
void merge(int a,int b)
{
    int A,B;
    A=find(a);
    B=find(b);
    if(A!=B)
    {
        head[a]=head[b]=head[A]=head[B]=-1;
        vis[a]=vis[b]=vis[A]=vis[B]=0;
        dfn[a]=dfn[b]=dfn[A]=dfn[B]=0;
        low[a]=low[b]=low[A]=low[B]=0;
        f[B]=A;
    }
}
/****************************/
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        cont=0;
        cnt=1;
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        memset(vis,0,sizeof(vis));
        memset(head,-1,sizeof(head));
        memset(ans,0,sizeof(ans));
        for(int i=1;i<=n;i++)f[i]=i;
        for(int i=1;i<=m;i++)scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w),a[i].pos=i;
        sort(a+1,a+1+m,cmp);
        for(int i=1;i<=m;i++)
        {
            int ss=i;
            int ee=i;
            while(ee+1<=m&&a[ee].w==a[ee+1].w)ee++;
            for(int j=ss;j<=ee;j++)
            {
                int fx=find(a[j].x);
                int fy=find(a[j].y);
                if(fx!=fy)
                {
                    ans[a[j].pos]=2;
                    add(fx,fy,a[j].pos);
                    add(fy,fx,a[j].pos);
                }
            }
            for(int j=ss;j<=ee;j++)
            {
                int fx=find(a[j].x);
                int fy=find(a[j].y);
                if(fx!=fy&&vis[fx]==0)Dfs(fx,-1);
                if(fx!=fy&&vis[fy]==0)Dfs(fy,-1);
            }
            for(int j=ss;j<=ee;j++)
            {
                merge(a[i=j].x,a[j].y);
            }
            i=ee;
        }
        for(int i=1;i<=m;i++)
        {
            if(ans[i]==0)printf("none\n");
            if(ans[i]==1)printf("any\n");
            if(ans[i]==2)printf("at least one\n");
        }
    }
}















当前提供的引用内容并未提及关于Codeforces比赛M1的具体时间安排[^1]。然而,通常情况下,Codeforces的比赛时间会在其官方网站上提前公布,并提供基于不同时区的转换工具以便参赛者了解具体开赛时刻。 对于Codeforces上的赛事而言,如果一场名为M1的比赛被计划举行,则它的原始时间一般按照UTC(协调世界时)设定。为了得知该场比赛在UTC+8时区的确切开始时间,可以遵循以下逻辑: - 前往Codeforces官网并定位至对应比赛页面。 - 查看比赛所标注的标准UTC起始时间。 - 将此标准时间加上8小时来获取对应的北京时间(即UTC+8)。 由于目前缺乏具体的官方公告链接或者确切日期作为依据,无法直接给出Codeforces M1比赛于UTC+8下的实际发生时段。建议定期访问Codeforces平台查看最新动态更新以及确认最终版程表信息。 ```python from datetime import timedelta, datetime def convert_utc_to_bj(utc_time_str): utc_format = "%Y-%m-%dT%H:%M:%SZ" bj_offset = timedelta(hours=8) try: # 解析UTC时间为datetime对象 utc_datetime = datetime.strptime(utc_time_str, utc_format) # 转换为北京时区时间 beijing_time = utc_datetime + bj_offset return beijing_time.strftime("%Y-%m-%d %H:%M:%S") except ValueError as e: return f"错误:{e}" # 示例输入假设某场Codeforces比赛定于特定UTC时间 example_utc_start = "2024-12-05T17:35:00Z" converted_time = convert_utc_to_bj(example_utc_start) print(f"Codeforces比赛在北京时间下将是:{converted_time}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值