文章出處

The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 28207   Accepted: 10073

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

2
3 3
1 2 1
2 3 2
3 1 3
4 4
1 2 2
2 3 2
3 4 2
4 1 2

Sample Output

3
Not Unique!

Source

 
求一個圖是否有不同的最小生成樹
大家都太暴力,枚舉去那條邊,暴力一遍判斷重復
但其實可以更快
可以參考這篇論文(注意,文章中代碼我認為有錯,請自行思考)
http://www.docin.com/p-806495282.html
 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<climits>
 6 #include<algorithm>
 7 #include<queue>
 8 #define LL long long
 9 using namespace std;
10 typedef struct{
11     int to,frm,dis;
12 }edge;
13 edge gra[20010];
14 int num=0,fa[110];
15 int n,m;
16 int cmp(const edge &a,const edge &b){
17     return a.dis<b.dis;
18 }
19 int fnd(int x){
20     return x==fa[x]?x:fnd(fa[x]);
21 }
22 int uni(int x,int y){
23     int fx=fnd(x);
24     int fy=fnd(y);
25     fa[fy]=fx;
26     return 0;
27 }
28 inline int read(){
29        int sum=0;char ch=getchar();
30        while(ch>'9'||ch<'0')ch=getchar();
31        while(ch<='9'&&ch>='0'){
32              sum=sum*10+ch-'0';
33              ch=getchar();
34        }
35        return sum;
36 }
37 int kru(){
38     int ans=0;
39     sort(gra+1,gra+m+1,cmp);
40     for(int i=1;i<=n;i++)fa[i]=i;
41     for(int i=1;i<=m;i++){
42         int x=gra[i].frm;
43         int y=gra[i].to;
44         int fx=fnd(x);
45         int fy=fnd(y);
46         if(fx!=fy){
47             int j=i+1;
48             while(j<=m&&gra[j].dis==gra[i].dis){
49                 int y1=gra[j].frm;
50                 int x1=gra[j].to;
51                 int fy1=fnd(y1);
52                 int fx1=fnd(x1);
53                 if((fx1==fx&&fy1==fy)||(fx1==fy&&fy1==fx))return -1;
54                 j++;
55             }
56             ans+=gra[i].dis;
57             uni(fx,fy);
58         }
59     }
60     return ans;
61 }
62 int main(){
63     int t;
64     t=read();
65     while(t--){
66         memset(gra,0,sizeof(gra));
67           n=read(),m=read();
68           num=0;
69         for(int i=1;i<=m;i++){
70             gra[i].frm=read();
71             gra[i].to=read();
72             gra[i].dis=read();
73         }
74 
75         int ans=kru();
76         if(ans==-1)printf("Not Unique!\n");
77         else printf("%d\n",ans);
78     }
79     return 0;
80 }
View Code

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()