标签:case strong set directed unique prope pac mes iostream
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 30194 | Accepted: 10809 |
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!
Source
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; #define N 101 struct Edge { int x,y,z; }edge[2*N]; int t,x,y,flag,ans,ans2,n,m,num2; int far[N]; bool path[2*N]; void set() { for(int i=1;i<=n;i++) far[i]=i; } void unionn(int x,int y) { far[x]=y; } int f(int x) { far[x]==x?x:far[x]=f(far[x]); } bool cmp(Edge a,Edge b) { return a.x<b.y; } void kruskal() { set(); int num1=0; sort(edge+1,edge+m+1,cmp); for(int i=1;i<=m;i++) { int fx=f(edge[i].x),fy=f(edge[i].y); if(fx!=fy) { unionn(fx,fy); ans+=edge[i].z; path[++num1]=i; } if(num1==n-1) break; } for(int k=1;k<=num1;k++) { set(); ans2=0;num2=0; for(int i=1;i<=m;i++) { if(i==path[i]) continue; int fx=f(edge[i].x),fy=f(edge[i].y); if(fx!=fy) { unionn(fx,fy); ans2+=edge[i].z; num2++; } if(num2==n-1&&ans2==ans)flag=1; } } } int main() { scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=1;i<=m;i++) scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].z); flag=0; ans=0; kruskal(); if(flag) printf("Not Unique!\n"); else printf("%d\n",ans); } return 0; }
标签:case strong set directed unique prope pac mes iostream
原文地址:https://www.cnblogs.com/zzyh/p/6830594.html