| ||||||||||
Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
大牛指教,那出问题啊?为什么总是wa……#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int n,m; int p[110],r[110],del[12100]; bool o[12100]; struct node{ int x,y,w; }; node e[12100]; int Find(int x) { if(p[x]!=x) p[x]=Find(p[x]); return p[x]; } void init(){ for(int i=0;i<=n;i++) { p[i]=i; r[i]=0; } } bool cmp(node a,node b){ return a.w<b.w; } int kluskal(int x){ int i,f1,f2,sum=0,j=0; for( i=0;i<m;i++) { if(!o[i]) { f1=Find(e[i].x); f2=Find(e[i].y); if(f1!=f2) { if(r[f1]>r[f2]) p[f2]=f1; else { p[f1]=f2; if(r[f1]==r[f2]) r[f2]++; } if(x) del[j]=i; j++; sum+=e[i].w; if(j==n-1) return sum; } } } return 0; } int main(){ int t,i,a=0,b; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); for(i=0;i<m;i++) scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].w); init(); memset(del,0,sizeof(del)); memset(o,0,sizeof(o)); sort(e,e+m,cmp); a=kluskal(1); if(m>n-1){ for(i=0;i<n-1;i++) { init(); o[del[i]]=1; b=kluskal(0); if(a==b) { printf("Not Unique!\n"); return 0; } o[del[i]]=0; } } printf("%d\n",a); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator