http://poj.org/problem?id=1679
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 20487 | Accepted: 7207 |
Description
Input
Output
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!程序:
#include"stdio.h" #include"string.h" #include"stack" #include"math.h" #include"algorithm" #include"stdlib.h" #define M 222 #define inf 100000000 #define eps 1e-12 using namespace std; struct node { int u,v,w; }e[M*M]; int cmp(node a,node b) { return a.w<b.w; } int f[M]; int finde(int x) { if(x!=f[x]) f[x]=finde(f[x]); return f[x]; } int main() { int T; scanf("%d",&T); while(T--) { int n,m,i,j; scanf("%d%d",&n,&m); for(i=0;i<m;i++) scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w); sort(e,e+m,cmp); int num=0; int fuck=0; int sum=0; for(i=1;i<=n;i++) f[i]=i; for(i=0;i<m;) { j=i; while(e[i].w==e[j].w&&j<m) { int x=finde(e[j].u); int y=finde(e[j].v); if(x!=y) fuck++; j++; } j=i; while(e[i].w==e[j].w&&j<m) { int x=finde(e[j].u); int y=finde(e[j].v); if(x!=y) { f[x]=y; sum+=e[i].w; num++; } j++; } i=j; if(num==n-1) break; } if(fuck>num) printf("Not Unique!\n"); else printf("%d\n",sum); //printf("%d %d\n",fuck,num); } }