标签:logs nim any ota uniq color tin 次小生成树 accept
http://poj.org/problem?id=1679
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 30120 | Accepted: 10778 |
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
1 #include <algorithm> 2 #include <cstdio> 3 4 using namespace std; 5 6 const int N(10001); 7 int num,n,m; 8 int fa[N],cnt; 9 int Fir,MST; 10 int u,v,w,used[N]; 11 struct Edge 12 { 13 int u,v,w; 14 } edge[N<<1]; 15 16 bool cmp(Edge a,Edge b) 17 { 18 return a.w<b.w; 19 } 20 21 int find(int x) 22 { 23 return fa[x]==x?x:fa[x]=find(fa[x]); 24 } 25 26 int Kruskal() 27 { 28 int ans=0; cnt=0; 29 sort(edge+1,edge+m+1,cmp); 30 for(int i=1; i<=n; i++) fa[i]=i; 31 for(int i=1; i<=m; i++) 32 { 33 int fx=find(edge[i].u),fy=find(edge[i].v); 34 if(fx!=fy) 35 { 36 fa[fx]=fy; 37 used[++cnt]=i; 38 ans+=edge[i].w; 39 } 40 if(cnt==n-1) return ans; 41 } 42 return ans; 43 } 44 45 int SecKru(int cant) 46 { 47 int ans=0; cnt=0; 48 for(int i=1;i<=n;i++) fa[i]=i; 49 for(int i=1;i<=m;i++) 50 { 51 if(cant==i) continue; 52 int fx=find(edge[i].u),fy=find(edge[i].v); 53 if(fx!=fy) 54 { 55 cnt++; 56 fa[fx]=fy; 57 ans+=edge[i].w; 58 } 59 if(cnt==n-1) return ans; 60 } 61 return 0x7fffffff; 62 } 63 64 int main() 65 { 66 scanf("%d",&num); 67 for(;num--;) 68 { 69 scanf("%d%d",&n,&m); 70 for(int i=1;i<=m;i++) 71 scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w); 72 Fir=Kruskal(); MST=0x7fffffff; 73 for(int i=1;i<n;i++) 74 { 75 MST=min(SecKru(used[i]),MST); 76 } 77 if(Fir==MST) 78 printf("Not Unique!\n"); 79 else printf("%d\n",Fir); 80 } 81 return 0; 82 }
标签:logs nim any ota uniq color tin 次小生成树 accept
原文地址:http://www.cnblogs.com/Shy-key/p/6821073.html