• 最大生成树与最小生成树


    hdu4786

    Fibonacci Tree

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


    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
     
    题意:给出一个无向连通图,边有两种颜色黑色和白色,黑色的权值是0,白色的权值是1,问是否存在一棵树,该树的边权和是斐波那契数;
    分析:首先求出最小生成树(权值和s1)和最大生成树(权值和s2),则一定存在所有的可能(s)满足s1<=s<=s2,只要在s1和s2范围内有斐波那契数,就行,至于怎样理解,也很好说,其实可以这样来理解,对于一棵树由于黑色和白色边两种,我们在任意一种状态,我们可以取出一条黑边然后用一条白边来取代这条黑边并且保证这棵树是联通的。
    程序:
    #include"stdio.h"
    #include"string.h"
    #include"stdlib.h"
    #define M 200000
    struct st
    {
        int u,v,w;
    }edge[M];
    int f[M];
    int cmp1(const void *a,const void *b)
    {
        return (*(struct st*)a).w-(*(struct st *)b).w;
    }
    int cmp2(const void *a,const void *b)
    {
        return (*(struct st*)b).w-(*(struct st *)a).w;
    }
    int finde(int x)
    {
        if(x!=f[x])
            f[x]=finde(f[x]);
        return f[x];
    }
    void make(int a,int b)
    {
        int x=finde(a);
        int y=finde(b);
        if(x!=y)
            f[x]=y;
    }
    int main()
    {
        int h[44];
        h[1]=1;
        h[2]=2;
        int i;
        for(i=3;i<=30;i++)
            h[i]=h[i-1]+h[i-2];
        int w;
        scanf("%d",&w);
        int kk=1;
        while(w--)
        {
            int n,m;
            scanf("%d%d",&n,&m);
            for(i=1;i<=n;i++)
                f[i]=i;
            for(i=0;i<m;i++)
            {
                int a,b,c;
                scanf("%d%d%d",&a,&b,&c);
                make(a,b);
                edge[i].u=a;
                edge[i].v=b;
                edge[i].w=c;
            }
            int k=0;
            for(i=1;i<=n;i++)
            {
                if(f[i]==i)
                    k++;
            }
            if(k>1)
            {
                printf("Case #%d: No
    ",kk++);
                continue;
            }
                
            qsort(edge,m,sizeof(edge[0]),cmp1);
            for(i=1;i<=n;i++)
                f[i]=i;
            int sum1=0;
            for(i=0;i<m;i++)
            {
                int u=edge[i].u;
                int v=edge[i].v;
                if(finde(u)!=finde(v))
                {
                    sum1+=edge[i].w;
                    make(u,v);
                }
            }
    
            qsort(edge,m,sizeof(edge[0]),cmp2);
            for(i=1;i<=n;i++)
                f[i]=i;
            int sum2=0;
            for(i=0;i<m;i++)
            {
                int u=edge[i].u;
                int v=edge[i].v;
                if(finde(u)!=finde(v))
                {
                    sum2+=edge[i].w;
                    make(u,v);
                }
            }
            //printf("%d %d
    ",sum1,sum2);
            int flag=0;
            for(i=1;i<=28;i++)
            {
                if(h[i]>=sum1&&h[i]<=sum2)
                    flag++;
            }
            if(flag)
                printf("Case #%d: Yes
    ",kk++);
            else
                printf("Case #%d: No
    ",kk++);
        }
    }
    



  • 相关阅读:
    没时间写文章::>_<::
    使 iis 支持 flv 视频播放
    CentOS7系统配置国内yum源(阿里云)
    android adb启动失败问题 adb server is out of date. killing... ADB server didn't ACK * failed to start daemon *
    android开发 获取手机分辨率大小
    c++流操作>rdbuf()
    ROR:Mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost'
    android map如何获得Google Map API key
    Android有效解决加载大图片时内存溢出的问题
    Android开发之Intent跳转到系统应用中的拨号界面、联系人界面、短信界面
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348198.html
Copyright © 2020-2023  润新知