The Unique MST

The Unique MST

http://poj.org/problem?id=1679

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 36744   Accepted: 13395

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

 

模板题

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cmath>
  4 #include<algorithm>
  5 #include<string>
  6 #include<cstring>
  7 #include<vector>
  8 #include<queue>
  9 #define lson l,mid,rt<<1
 10 #define rson mid+1,r,rt<<1|1
 11 #define N 250500
 12 #define MOD 1e9+7
 13 #define INF 0x3f3f3f3f
 14 typedef long long ll;
 15 using namespace std;
 16 struct sair{
 17     int x,y,v;
 18 }a[100005];
 19 int fa[1005];
 20 int n,m;
 21 bool cmp(sair a,sair b){
 22     return a.v<b.v;
 23 }
 24 
 25 int Find(int x){
 26     int r=x,y;
 27     while(x!=fa[x]){
 28         x=fa[x];
 29     }
 30     while(x!=r){
 31         y=fa[r];
 32         fa[r]=x;
 33         r=y;
 34     }
 35     return x;
 36 }
 37 
 38 int join(int x,int y){
 39     int xx=Find(x);
 40     int yy=Find(y);
 41     if(xx==yy){
 42         return 0;
 43     }
 44     fa[xx]=yy;
 45     return 1;
 46 }
 47 
 48 vector<int>v;
 49 
 50 
 51 int check(int xxx){
 52     int ans=0;
 53     int xxxx=1;
 54     for(int i=1;i<=n;i++){
 55         fa[i]=i;
 56     }
 57     for(int i=1;i<=m;i++){
 58         if(i!=xxx){
 59             if(join(a[i].x,a[i].y)){
 60                 ans+=a[i].v;
 61                 xxxx++;
 62             }
 63         }
 64     }
 65     if(xxxx==n)
 66         return ans;
 67     return -1;
 68 }
 69 
 70 int main(){
 71     std::ios::sync_with_stdio(false);
 72     int t;
 73     cin>>t;
 74     while(t--){
 75         cin>>n>>m;
 76         for(int i=1;i<=n;i++){
 77             fa[i]=i;
 78         }
 79         for(int i=1;i<=m;i++){
 80             cin>>a[i].x>>a[i].y>>a[i].v;
 81 
 82         }
 83         int ans1=0;
 84         v.clear();
 85         sort(a+1,a+m+1,cmp);
 86         for(int i=1;i<=m;i++){
 87             if(join(a[i].x,a[i].y)){
 88                 ans1+=a[i].v;
 89                 v.push_back(i);
 90             }
 91         }
 92         int flag=1;
 93         for(int i=0;i<v.size();i++){
 94             if(check(v[i])==ans1){
 95                 flag=0;
 96                 break;
 97             }
 98         }
 99         if(flag){
100             cout<<ans1<<endl;
101         }
102         else{
103             cout<<"Not Unique!"<<endl;
104         }
105     }
106 }
View Code

 

posted on 2018-10-03 20:47  Fighting_sh  阅读(208)  评论(0编辑  收藏  举报

导航