• HDU 4786 Fibonacci Tree


    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
     

    Source


    题意:给你些边跟它的权值,权值仅仅能是0或者1,要你求出一颗生成树,使得该树的白边的边数为斐波那契数列,白边的权值为1.

    思路:我们能够求出须要最小的白边跟最多的白边,又由于生成树的边比較特殊,权值为0,1   所以我们仅仅须要求出最大生成树,便是须要的最多白边数,求出最小生成树,则为须要的最少白边树。

    然后仅仅须要推断白边数的区间是否有斐波那契数就能够了。其他的边替换为黑边就能够了

    本题另一个坑点。那就是树本身不连通那么就输出No

    AC代码:

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    int f[100005];
    struct p
    {
        int u,v,w;
    }num[100005];
    int a[40];
    
    int n,m;
    int cnt;
    
    bool cmp1(p x,p y)
    {
        return x.w<y.w;
    }
    
    bool cmp2(p x,p y)
    {
        return x.w>y.w;
    }
    
    int find(int x)
    {
        if(x!=f[x])
            f[x]=find(f[x]);
        return f[x];
    }
    
    int kra()
    {
        int i,tot=n;
        int sum=0;
        for(i=0;i<cnt;i++)
        {
            int x=find(num[i].u);
            int y=find(num[i].v);
            if(x==y)
                continue;
            f[x]=y;
            sum+=num[i].w;
            tot--;
            if(tot==0)break;
        }
        return sum;
    }
    
    int main()
    {
        int i,j;
        int t;
        a[1]=1;
        a[2]=2;
        for(i=3;i<=25;i++)
            a[i]=a[i-2]+a[i-1];
        scanf("%d",&t);
        int tot=1;
        while(t--)
        {
            scanf("%d %d",&n,&m);
            for(i=1;i<=n;i++)
                f[i]=i;
            cnt=0;
            for(i=1;i<=m;i++)
            {
                int a,b,c;
                scanf("%d %d %d",&a,&b,&c);
                num[cnt].u=a;
                num[cnt].v=b;
                num[cnt++].w=c;
            }
    
            sort(num,num+cnt,cmp2);
            int ran1=kra();
            for(i=1;i<=n;i++)
                f[i]=i;
            sort(num,num+cnt,cmp1);
            int ran2=kra();
            bool ff = true;
            for(int i = 1;i <= n;i++)
                if(find(i) != find(1))
                {
                    ff = false;
                    break;
                }
            if(!ff)
            {
                printf("Case #%d: No
    ",tot++);
                continue;
            }
    
            int flag=0;
            for(i=1;i<25;i++)
                if(a[i]>=ran2&&a[i]<=ran1)
                    flag=1;
            if(flag)
               printf("Case #%d: Yes
    ",tot++);
            else
                printf("Case #%d: No
    ",tot++);
        }
    
        return 0;
    }
    


  • 相关阅读:
    Javascript操作DOM常用API总结
    实用技巧之Visio绘制流程框图
    实用技巧之Visio绘图
    回复《竞赛部的一封信》
    学习之路
    《软件工程》上课精华分享
    《编译原理》上课笔记2
    sql server2008 远程过程调用失败
    《编译原理》上课笔记1
    python的第五天(复习数据类型)
  • 原文地址:https://www.cnblogs.com/wzjhoutai/p/6839572.html
Copyright © 2020-2023  润新知