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 <= 10 5) and M(0 <= M <= 10 5).
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#include #include 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; } 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 =ran2&&a[i]<=ran1) flag=1; if(flag) printf("Case #%d: Yes\n",tot++); else printf("Case #%d: No\n",tot++); } return 0; }