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
#include
#include
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.wdfn[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");
        }
    }
}















你可能感兴趣的:(双连通,思维,并查集)