Fibonacci Tree
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4026 Accepted Submission(s): 1251
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
题意:给出n个点,m条边,每条边有两个权值,1和0,表示白边和黑边,现在问你,有没有一颗生成树,里面白边的数量是斐波那契数列(F[n]=F[n-1]+F[n-1])里的某一个数
思路:用kruskal求两次生成树,一次是先取白边,取完再取黑边。 另一次是先取黑边,取完再取白边。这样可以求出白边数量最多的生成树和白边数量最少的生成树。
那么显而易见,在这两个极值之间的数目的白边,肯定是可以构成生成树的。所以只要判断这个区间里有没有斐波那契数列数就OK。 注意白边是1黑边是0,求出的生成树的长度就是白边的数目。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 100100
int n,m;
int F[N];
int pre[N];
struct Edge
{
int u,v,w;
} e[N];
bool cmp(Edge a,Edge b)
{
return a.w>b.w;
}
bool cmp1(Edge a,Edge b)
{
return a.w<b.w;
}
void init()
{
memset(F,0,sizeof(F));
int k=1,i=2;
F[1]=F[2]=1;
while(i+k<=N)
{
int j=i+k;
F[i+k]=1;
k=i;
i=j;
}
}
int finds(int x)
{
int r=x;
while(r!=pre[r])
r=pre[r];
int j;
while(x!=pre[x])
{
j=x;
x=pre[x];
pre[j]=r;
}
return r;
}
int kruskal()
{
int ans=0;
for(int i=0;i<m;i++)
{
int x=finds(e[i].u);
int y=finds(e[i].v);
if(x==y) continue;
pre[x]=y;
ans+=e[i].w;
}
int num=0;
for(int i=1;i<=n;i++)
if(pre[i]==i)
num++;
if(num>1)
return -1;
return ans;
}
int main()
{
int T;
int u,v,w;
init();
scanf("%d",&T);
for(int t=1; t<=T; t++)
{
scanf("%d %d",&n,&m);
for(int i=1; i<=n; i++)
pre[i]=i;
for(int i=0; i<m; i++)
scanf("%d %d %d",&e[i].u,&e[i].v,&e[i].w);
sort(e,e+m,cmp);
int maxn=kruskal();
if(maxn==-1)
{
printf("Case #%d: No\n",t);
continue;
}
for(int i=1; i<=n; i++)
pre[i]=i;
sort(e,e+m,cmp1);
int minn=kruskal();
int flag=0;
for(int i=minn; i<=maxn; i++)
if(F[i])
{
flag=1;
break;
}
if(flag)
printf("Case #%d: Yes\n",t);
else
printf("Case #%d: No\n",t);
}
return 0;
}