Fibonacci Tree
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2793 Accepted Submission(s): 886
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 <= 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).
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
即为给一个图,每条边可以涂黑色或者白色,求问图中去边以后最后的生成树中,白色边数量可不可以为Fibonacci数(fibonacci数为f1=1,f2=2,fn=fn-1+fn-2),可以输出Yes,不可以输出No。
做法:只要白边优先和黑边优先两种顺序做两次最小生成树。得到白边数量的区间,然后枚举斐波那契数列就可以了。注意如果一开始是非连通的,输出NO。(来自网上)
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
using namespace std;
typedef struct{
int u,v,c;
}edge;
edge mp[200010];
long long par[200010];
long long n,m;
int find(int x)
{
if(par[x] == x) return x;
return par[x] = find(par[x]);
}
long long cnt;
bool cmp1(edge a,edge b)
{
return a.c<b.c ;
}//求黑边多的
bool cmp2(edge a,edge b)
{
return a.c>b.c ;
}
void solve()
{
for(int i=0;i<=n;i++)
par[i] = i;
for(int i=0;i<m;i++)
{
int t1 = find(mp[i].u);
int t2 = find(mp[i].v);
if(t1 != t2)
{
par[t1] = t2;
if(mp[i].c==1)cnt++;
}
}
}
long long fi[1000010];
void init()
{
fi[1] = 1;fi[2] = 2;
for(int i=3;i<1000010;i++)
fi[i] = fi[i-1]+fi[i-2];
}
int main()
{
long long T,g=1;
cin >> T;
init();
while(T--)
{
cin >> n >> m;
//memset(mp,0,sizeof(mp));
for(int i=0;i<m;i++)
scanf("%lld%lld%lld",&mp[i].u,&mp[i].v,&mp[i].c);
//cin >> mp[i].u >> mp[i].v >> mp[i].c;
sort(mp,mp+m,cmp1);
cnt = 0;
solve();
long long low = cnt;
sort(mp,mp+m,cmp2);
cnt = 0;
solve();
long long height = cnt;
long long flag = 0;
for(int i = 1;i<=n;i++)
if(find(i) != find(1))
{
flag = 1;
break;
}
if(flag)
{
printf("Case #%d: No\n",g++);
continue;
}
for(int i=1;i<50;i++)
{
if(fi[i]>=low&&fi[i]<=height)
{
flag = 1;
break;
}
}
if(flag)
printf("Case #%d: Yes\n",g++);
else
printf("Case #%d: No\n",g++);
}
return 0;
}