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?
(Fibonacci number is defined as 1, 2, 3, 5, 8, ... )
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
题目大意:给定一个图,里面有的边是白色,剩下的全为黑色。问存不存在一个生成树里面包含的白色边的个数恰为Fibonacci numbers。
看了题解始终没明白。。。题解的解法是白边边权为 1 ,黑边边权为 0 。构造一个最大生成树,用掉的白边数就是所有生成树里白边的最大个数Max。再构造一个最小生树,用掉的白边数就是所有生成树里白边的最小个数Min。若Min~Max之间包含 Fibonacci numbers 就符合题意。据说是因为一条黑边可以被一条白边代替,依然保持图的连通性,所以只要Min ~ Max之间包含 Fibonacci numbers 就OK。依然不明白是为什么。。。求大神告知。生成树用 Kruskal,查找Fibonacci numbers 是打了一个表然后二分找的。
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 1e5 + 5; int p[maxn],f[100],v[maxn],u[maxn],e[maxn],w[maxn]; int len = 1; int n,m; bool cmp(int x,int y) { return w[x] < w[y]; } void init() { memset(f,0,sizeof(f)); f[0] = 1,f[1] = 2; while(f[len] <= 100000) ++len,f[len] = f[len - 1] + f[len - 2]; } int Find(int x) { return p[x] == x ? x : p[x] = Find(p[x]); } int main() { int t,x,y,c; scanf("%d",&t); init(); int pre = t; while(t--) { scanf("%d %d",&n,&m); for(int i = 1;i <= m; ++i) e[i] = i; for(int i = 1;i <= m; ++i) scanf("%d %d %d",&u[i],&v[i],&w[i]); sort(e + 1,e + m + 1,cmp); int temp = 0,mi = 0,ma = 0; for(int i = 1;i <= n; ++i) p[i] = i; for(int i = 1;i <= m; ++i) { int now = e[i]; int x = Find(u[now]),y = Find(v[now]); if(x != y) { ++temp; if(w[now] == 1) ++mi; p[y] = x; } } if(temp < n - 1) { printf("Case #%d: No\n",pre - t); continue; } for(int i = 1;i <= n; ++i) p[i] = i; for(int i = m;i >= 1; --i) { int now = e[i]; int x = Find(u[now]),y = Find(v[now]); if(x != y) { if(w[now] == 1) ++ma; p[y] = x; } } temp = lower_bound(f,f + len,mi) - f; printf("Case #%d: ",pre - t); if(f[temp] <= ma) printf("Yes\n"); else printf("No\n"); } return 0; }