POJ1679------The Unique MST

来源:互联网 发布:枕头高度 知乎 编辑:程序博客网 时间:2024/05/17 00:06
The Unique MST
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 24371 Accepted: 8674

判断最小生成树是否唯一;


Description

Given a connected undirected graph, tell if its minimum spanning tree is unique.

Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V', E'), with the following properties:
1. V' = V.
2. T is connected and acyclic.

Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E') of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E'.

Input

The first line contains a single integer t (1 <= t <= 20), the number of test cases. Each case represents a graph. It begins with a line containing two integers n and m (1 <= n <= 100), the number of nodes and edges. Each of the following m lines contains a triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.

Output

For each input, if the MST is unique, print the total cost of it, or otherwise print the string 'Not Unique!'.

Sample Input

23 31 2 12 3 23 1 34 41 2 22 3 23 4 24 1 2

Sample Output

3Not Unique!

#include <iostream>#include<cstdio>#include<string>#include<vector>#include<iomanip>#include<cmath>#include<cctype>#include<climits>#include<cstring>#include<algorithm>#include<set>#include<map>#include<cstdlib>#define LL long long#define uint unsigned int#define uLL unsigned LL#pragma comment(linker,"/STACK:102400000,102400000")using namespace std;struct Edge{    int u,v,w;    int equa,used,del;    bool operator < (const Edge& x)const    {        return w<x.w;    }}que[10010];int fa[110];bool first;int n,m;int fint(int x){    return fa[x]!=x?fa[x]=fint(fa[x]):x;}void Uset(){    for(int i=0;i<=n;i++)    {        fa[i]=i;    }}int Kruscal(){    Uset();    int g,h;    int sum=0,cnt=0;    for(int i=0;i<m;i++)    {        if(que[i].del)continue;        g=fint(que[i].u);h=fint(que[i].v);        if(g!=h)        {            fa[g]=h;            cnt++;            sum+=que[i].w;            if(first)                que[i].used=1;            if(cnt==n-1)                break;        }    }    return sum;}int main(){    ios::sync_with_stdio(0);    int t;    while(cin>>t)    {        while(t--)        {            cin>>n>>m;            for(int i=0;i<m;i++)            {                scanf("%d%d%d",&que[i].u,&que[i].v,&que[i].w);                que[i].del=que[i].equa=que[i].used=0;                for(int j=0;j<i;j++)                {                    if(que[i].w==que[j].w)                        que[i].equa=que[j].equa=1;                }            }            sort(que,que+m);            first =1 ;            int weight1=Kruscal(),weight2;            first=0;            bool flag=1;            for(int i=0;i<m;i++)            {                if(que[i].used&&que[i].equa==1)                {                    que[i].del=1;                    weight2=Kruscal();                    if(weight1==weight2)                    {                        flag=0;                        break;                    }                    que[i].del=0;                }            }            if(flag)                printf("%d\n",weight1);            else                printf("Not Unique!\n");        }    }    return 0;}


0 0