首页 > 代码库 > HDU 4786 Fibonacci Tree
HDU 4786 Fibonacci Tree
Problem Description
Coach Pang is interested in Fibonacci numbers while Uncle Yang wants him to do some research on Spanning Tree. So Coach Pang decides to solve the following problem:
Consider a bidirectional graph G with N vertices and M edges. All edges are painted into either white or black. Can we find a Spanning Tree with some positive Fibonacci number of white edges?
Consider a bidirectional graph G with N vertices and M edges. All edges are painted into either white or black. Can we find a Spanning Tree with some positive Fibonacci number of white edges?
(Fibonacci number is defined as 1, 2, 3, 5, 8, ... )
Input
The first line of the input contains an integer T, the number of test cases.
For each test case, the first line contains two integers N(1 <= N <= 105) and M(0 <= M <= 105).
Then M lines follow, each contains three integers u, v (1 <= u,v <= N, u<> v) and c (0 <= c <= 1), indicating an edge between u and v with a color c (1 for white and 0 for black).
For each test case, the first line contains two integers N(1 <= N <= 105) and M(0 <= M <= 105).
Then M lines follow, each contains three integers u, v (1 <= u,v <= N, u<> v) and c (0 <= c <= 1), indicating an edge between u and v with a color c (1 for white and 0 for black).
Output
For each test case, output a line “Case #x: s”. x is the case number and s is either “Yes” or “No” (without quotes) representing the answer to the problem.
Sample Input
2 4 4 1 2 1 2 3 1 3 4 1 1 4 0 5 6 1 2 1 1 3 1 1 4 1 1 5 1 3 5 1 4 2 1
Sample Output
Case #1: Yes Case #2: No
Source
2013 Asia Chengdu Regional Contest
题意:给你些边跟它的权值,权值仅仅能是0或者1,要你求出一颗生成树,使得该树的白边的边数为斐波那契数列,白边的权值为1.
思路:我们能够求出须要最小的白边跟最多的白边,又由于生成树的边比較特殊,权值为0,1 所以我们仅仅须要求出最大生成树,便是须要的最多白边数,求出最小生成树,则为须要的最少白边树。
然后仅仅须要推断白边数的区间是否有斐波那契数就能够了。其他的边替换为黑边就能够了
本题另一个坑点。那就是树本身不连通那么就输出No
AC代码:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int f[100005]; struct p { int u,v,w; }num[100005]; int a[40]; int n,m; int cnt; bool cmp1(p x,p y) { return x.w<y.w; } bool cmp2(p x,p y) { return x.w>y.w; } int find(int x) { if(x!=f[x]) f[x]=find(f[x]); return f[x]; } int kra() { int i,tot=n; int sum=0; for(i=0;i<cnt;i++) { int x=find(num[i].u); int y=find(num[i].v); if(x==y) continue; f[x]=y; sum+=num[i].w; tot--; if(tot==0)break; } return sum; } int main() { int i,j; int t; a[1]=1; a[2]=2; for(i=3;i<=25;i++) a[i]=a[i-2]+a[i-1]; scanf("%d",&t); int tot=1; while(t--) { scanf("%d %d",&n,&m); for(i=1;i<=n;i++) f[i]=i; cnt=0; for(i=1;i<=m;i++) { int a,b,c; scanf("%d %d %d",&a,&b,&c); num[cnt].u=a; num[cnt].v=b; num[cnt++].w=c; } sort(num,num+cnt,cmp2); int ran1=kra(); for(i=1;i<=n;i++) f[i]=i; sort(num,num+cnt,cmp1); int ran2=kra(); bool ff = true; for(int i = 1;i <= n;i++) if(find(i) != find(1)) { ff = false; break; } if(!ff) { printf("Case #%d: No\n",tot++); continue; } int flag=0; for(i=1;i<25;i++) if(a[i]>=ran2&&a[i]<=ran1) flag=1; if(flag) printf("Case #%d: Yes\n",tot++); else printf("Case #%d: No\n",tot++); } return 0; }
HDU 4786 Fibonacci Tree
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。