Fibonacci Tree
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4696 Accepted Submission(s): 1475 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, ... )
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
思路:先排序一次将黑色边放前面,求一次最小生成树用到的白色边数,再排序一次将白色边放前面求一次最小生成树用到的白色边数,最后判断两数之间有无斐波拉契数即可。
# include# include # define MAXN 100000using namespace std;int pre[MAXN+1], f[25]={1,1};struct node{ int a, b, c;}edge[MAXN+1];bool cmp1(node a, node b){ return a.c < b.c;}bool cmp2(node a, node b){ return a.c > b.c;}void init(int n){ for(int i=0; i<=n; ++i) pre[i] = i;}int find(int x){ if(x != pre[x]) pre[x] = find(pre[x]); return pre[x];}int main(){ int t, n, m, cas=1; for(int i=2; i<25; ++i) f[i] = f[i-1] + f[i-2]; scanf("%d",&t); while(t--) { bool flag = false; int road=0, white1=0, white2=0; scanf("%d%d",&n,&m); init(n); for(int i=0; i