标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3006 Accepted Submission(s): 966
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<algorithm> 5 using namespace std; 6 const int MAXN=100010; 7 struct Node { 8 int s,e,c; 9 }; 10 Node dt[MAXN]; 11 int pre[MAXN]; 12 int M,t1,N; 13 int cmp1(Node a,Node b){ 14 return a.c<b.c; 15 } 16 int cmp2(Node a,Node b){ 17 return a.c>b.c; 18 } 19 /*int cmp1(const void *a,const void *b){ 20 if((*(Node *)a).c<(*(Node *)b).c)return -1; 21 else return 1; 22 } 23 int cmp2(const void *a,const void *b){ 24 if((*(Node *)a).c>(*(Node *)b).c)return -1; 25 else return 1; 26 }*/ 27 int find(int x){ 28 return pre[x]= x==pre[x]?x:find(pre[x]); 29 } 30 bool merge(Node a){ 31 if(!pre[a.s])pre[a.s]=a.s; 32 if(!pre[a.e])pre[a.e]=a.e; 33 int f1,f2; 34 f1=find(a.s);f2=find(a.e); 35 if(f1!=f2){ 36 pre[f1]=f2; 37 t1++; 38 if(a.c)return true; 39 } 40 return false; 41 } 42 int kruskal(){int tot=0; 43 t1=1; 44 for(int i=0;i<M;i++){ 45 if(merge(dt[i]))tot++; 46 } 47 if(t1==N)return tot; 48 else return -1; 49 } 50 bool fp[MAXN]; 51 void gf(){ 52 int a,b,c=0; 53 memset(fp,false,sizeof(fp)); 54 a=1;b=2; 55 fp[a]=fp[b]=true; 56 while(c<MAXN){ 57 c=a+b; 58 fp[c]=true; 59 a=b; 60 b=c; 61 } 62 } 63 int main(){ 64 int T,s1,s2,ans,flot=0; 65 scanf("%d",&T); 66 while(T--){ 67 flot++; 68 memset(pre,0,sizeof(pre)); 69 scanf("%d%d",&N,&M); 70 for(int i=0;i<M;i++){ 71 scanf("%d%d%d",&dt[i].s,&dt[i].e,&dt[i].c); 72 } 73 // qsort(dt,M,sizeof(dt[0]),cmp1); 74 sort(dt,dt+M,cmp1); 75 s1=kruskal(); 76 //qsort(dt,M,sizeof(dt[0]),cmp2); 77 sort(dt,dt+M,cmp2); 78 memset(pre,0,sizeof(pre)); 79 s2=kruskal(); 80 //printf("%d %d\n",s1,s2); 81 gf(); 82 ans=0; 83 if(s1<0||s2<0){ 84 printf("Case #%d: No\n",flot); 85 continue; 86 } 87 //for(int i=0;i<100;i++)printf("fp[%d]=%d ",i,fp[i]);puts(""); 88 if(s1>s2){ 89 int q=s1; 90 s1=s2; 91 s2=q; 92 } 93 for(int i=s1;i<=s2;i++){ 94 if(fp[i])ans=1; 95 } 96 if(ans)printf("Case #%d: Yes\n",flot); 97 else printf("Case #%d: No\n",flot); 98 } 99 return 0; 100 }
标签:
原文地址:http://www.cnblogs.com/handsomecui/p/4725600.html