标签:direct sum script mem esc i+1 and input begin
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 24152 | Accepted: 8587 |
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!
题目链接:http://poj.org/problem?
id=1679
题目大意:n个点m条路。给出每条路以及边权。推断最小生成树是否是唯一的。
解题思路:克鲁斯卡尔,推断是否存在等效边。这题数据太弱了。我推断等效边的方法不太对,竟然过了= =
代码例如以下:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int fa[102]; struct EG { int u,v,w; }eg[5005]; void get_fa() { for(int i=0;i<105;i++) fa[i]=i; } int find (int x) { return x==fa[x]?x:fa[x]=find(fa[x]); } void Union(int a,int b) { int a1=find(a); int b1=find(b); if(a1!=b1) fa[a1]=b1; } int cmp(EG a,EG b) { return a.w<b.w; } int main(void) { int t; scanf("%d",&t); while(t--) { int n,m,ans=0,p=0,cnt=0; get_fa(); scanf("%d%d",&n,&m); for(int i=0;i<m;i++) { scanf("%d%d%d",&eg[i].u,&eg[i].v,&eg[i].w); } sort(eg,eg+m,cmp); for(int i=0;i<m;i++) { if(find(eg[i].u)!=find(eg[i].v))//假设当前边须要增加且下一条边也须要增加且它们权值相等即为等效边 { if(i+1<m&&find(eg[i+1].u)!=find(eg[i+1].v)&&eg[i].w==eg[i+1].w) { p=1; break; } Union(eg[i].u,eg[i].v); ans+=eg[i].w; cnt++; } if(cnt>=n) break; } if(!p) printf("%d\n",ans ); else printf("Not Unique!\n"); } }
hdu 1679 The Unique MST (克鲁斯卡尔)
标签:direct sum script mem esc i+1 and input begin
原文地址:http://www.cnblogs.com/cxchanpin/p/6852703.html