Fibonacci Tree
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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(1表示这条边为白边,0表示这条边为黑边)。问此无向图是否存在一棵生成树,含有的白边数目恰为菲波拉契数。
思路:有关生成树很容易想到MST,再看一下数据规模,10^5个点,是Kruskal的标准数据规模。不难发现,我们只要求出此图的最大生成树和最小生成树,即可得到此图生成树的白边数的取值范围[Min,Max]。则问题就可简化为在[Min,Max]内是否存在菲波拉契数。而10^5以内的菲波拉契数仅有24个,这样打个表就能轻松AC。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
struct Edge
{
int u,v;
int val;
}edge[200010];
int pre[200010];
int find(int x)
{
if(pre[x]==-1)
return x;
return pre[x]=find(pre[x]);
}
bool cmp1(Edge a,Edge b)
{
return a.val<b.val;
}
bool cmp2(Edge a,Edge b)
{
return a.val>b.val;
}
int main()
{
int fi[31]={0,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025};
int t;
int cas=0;
int n,m;
scanf("%d",&t);
while(t--)
{
int ccc=0;
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].val);
}
sort(edge,edge+m,cmp1);
memset(pre,-1,sizeof(pre));
int cnt=0;
for(int i=0;i<m;i++)
{
int t1=find(edge[i].u);
int t2=find(edge[i].v);
if(t1!=t2)
{
pre[t1]=t2;
ccc++;
if(edge[i].val==1)
cnt++;
}
}
if(ccc!=n-1)
{
printf("Case #%d: No\n",++cas);
continue;
}
int low=cnt;
memset(pre,-1,sizeof(pre));
sort(edge,edge+m,cmp2);
cnt=0;
for(int i=0;i<m;i++)
{
int t1=find(edge[i].u);
int t2=find(edge[i].v);
if(t1!=t2)
{
pre[t1]=t2;
if(edge[i].val==1)
cnt++;
}
}
int high=cnt;
bool flag1=false;
for(int i=1;i<=24;i++)
{
if(fi[i]>=low&&fi[i]<=high)
flag1=true;
}
if(flag1)
printf("Case #%d: Yes\n",++cas);
else
printf("Case #%d: No\n",++cas);
}
return 0;
}