hdu4786 生成树中1的个数为fibonacci数
为什么在最小值和最大值之间就行,模模糊糊==
1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 int father[100005],f[30]; 6 struct dian{ 7 int x,y,w; 8 }a[100005]; 9 int cmp(dian n1,dian n2) 10 { 11 return n1.w<n2.w; 12 } 13 int findf(int x) 14 { 15 if (father[x]!=x) father[x]=findf(father[x]); 16 return father[x]; 17 } 18 int main() 19 { 20 int T,t,i,n,m,minc,maxc,flag; 21 scanf("%d",&T); 22 for (t=1;t<=T;t++) 23 { 24 scanf("%d%d",&n,&m); 25 for (i=1;i<=m;i++) 26 scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w); 27 sort(a+1,a+m+1,cmp); 28 for (i=1;i<=n;i++) father[i]=i; 29 minc=0; 30 for (i=1;i<=m;i++) 31 if (findf(a[i].x)!=findf(a[i].y)) 32 { 33 if (a[i].w) minc++; 34 father[findf(a[i].x)]=findf(a[i].y); 35 } 36 37 for (i=1;i<=n;i++) father[i]=i; 38 maxc=0; 39 for (i=m;i>=1;i--) 40 if (findf(a[i].x)!=findf(a[i].y)) 41 { 42 if (a[i].w) maxc++; 43 father[findf(a[i].x)]=findf(a[i].y); 44 } 45 flag=1; 46 for (i=2;i<=n;i++) 47 if (findf(i)!=findf(1)) flag=0; 48 49 f[0]=f[1]=1; 50 for (i=2;i<=25;i++) f[i]=f[i-1]+f[i-2]; 51 printf("Case #%d: ",t); 52 if (flag==0) {printf("No\n"); continue; } 53 flag=0; 54 for (i=1;i<=25;i++) 55 if (f[i]>=minc&&f[i]<=maxc) flag=1; 56 if (flag==0) printf("No\n"); 57 else printf("Yes\n"); 58 } 59 }