(HDU

来源:互联网 发布:常凯申 知乎 编辑:程序博客网 时间:2024/05/29 12:41

(HDU - 4786)Fibonacci Tree

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5378 Accepted Submission(s): 1687

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 <= 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

题目大意:一个n个点,m条边的无向图,它的边只有白边(1)和黑边(0)两种,问能不能生成一棵树,树的白边数是一个Fibonacci树。

思路:先按黑边优先排序,得到白边数最少几条sum1,然后按白边优先排序,得到白边数最多几天sum2,如果再sum1和sum2之间存在一个Fibonacci数则符合要求(即如果有一个数sum1<=x<=sum2)则白边数为x的生成树一定存在。ps:题目并没有保证一定可以生成树,所以还需要判断能不能生成树,不能也要输出No。

#include<cstdio>#include<algorithm>#include<cmath>using namespace std; const int maxn=100005;int fa[maxn],fib[25];struct node{    int u,v,w;}edge[maxn];bool cmp1(node a,node b)//黑边优先排序 {    return a.w<b.w;}bool cmp2(node a,node b)//白边优先排序 {    return a.w>b.w; } int find(int x){    if(x==fa[x]) return x;    else return fa[x]=find(fa[x]);}void getfib(){    fib[0]=1,fib[1]=1;    for(int i=2;i<25;i++) fib[i]=fib[i-1]+fib[i-2];    //printf("%d\n",fib[24]);}int main(){    getfib();    int T,kase=1;    scanf("%d",&T);     while(T--)    {        int n,m;        scanf("%d%d",&n,&m);        for(int i=0;i<=n;i++) fa[i]=i;        for(int i=0;i<m;i++) scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);        sort(edge,edge+m,cmp1);        int cnt=0,sum1=0,sum2=0;//sum1白边最少数量,sum2白边最多数量         for(int i=0;i<m;i++)        {            int fu=find(edge[i].u),fv=find(edge[i].v);            if(fu!=fv)            {                fa[fu]=fv;                cnt++;                sum1+=edge[i].w;            }            if(cnt==n-1) break;        }        for(int i=0;i<=n;i++) fa[i]=i;        sort(edge,edge+m,cmp2);        cnt=0;        for(int i=0;i<m;i++)        {            int fu=find(edge[i].u),fv=find(edge[i].v);            if(fu!=fv)            {                fa[fu]=fv;                cnt++;                sum2+=edge[i].w;            }            if(cnt==n-1) break;        }        if(cnt!=n-1) //不能够成树叶也要输出No         {            printf("Case #%d: No\n",kase++);            continue;        }        bool flag=false;        for(int i=2;i<25;i++)            if(fib[i]>=sum1&&fib[i]<=sum2)            {                flag=true;                break;            }        if(flag) printf("Case #%d: Yes\n",kase++);        else printf("Case #%d: No\n",kase++);     }    return 0;}