hdu4786-图论训练3-最小生成树

D - Fibonacci Tree
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit  Status

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 <= 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条边,边有黑白两种颜色,用这些边来建一个生成树(可以使用任意边,黑边白边都可以),在所有的生成树中使用白边的数目形成一个数列,问这个数列中有没有斐波那契数列里数字;

  我们可以求出用白边最少和用白边最多的两课生成树,然后得到两个数字,判断这两个数字之间有没有斐波那契数即可,

我的代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn=400000;
int fib[60];
int n,m;
struct Edge
{
    int u,v,w;
}d[maxn];
bool cmp1(Edge a,Edge b)
{
    return a.w<b.w;
}
bool cmp2(Edge a,Edge b)
{
    return a.w>b.w;
}

void get_fib()
{
    fib[0]=0;
    fib[1]=1;
    int i;
    for(i=2;i<=26;i++)
        fib[i]=fib[i-1]+fib[i-2];

}
int par[maxn],rak[maxn];
void init()
{
    for(int i=0;i<=n;i++)
    {
        par[i]=i;
        rak[i]=0;
    }
}
int find_(int x)
{
    if(par[x]==x)
        return x;
    else
        return par[x]=find_(par[x]);
}
void unite(int x,int y)
{
    x=find_(x);
    y=find_(y);
    if(x==y)
        return;
    if(rak[x]<rak[y])
    {
        par[x]=y;
    }
    else
    {
        par[y]=x;
        if(rak[x]==rak[y])
            rak[x]++;
    }
}
bool vis[maxn];
int main()
{
    int t,T=0;
    int l,r;
    get_fib();
    cin>>t;
    while(t--)
    {
        scanf("%d%d",&n,&m);
        l=0,r=0;
        for(int i=0;i<m;i++)
        scanf("%d%d%d",&d[i].u,&d[i].v,&d[i].w);

        printf("Case #%d: ",++T);

        init();///白边为辅
        sort(d,d+m,cmp1);
        for(int i=0;i<m;i++)
        {
            Edge e=d[i];
            if(find_(e.u)!=find_(e.v))
            {

                unite(e.u,e.v);
                l+=e.w;
            }
        }

        init();///白边为主
        memset(vis,false,sizeof(vis));
        sort(d,d+m,cmp2);
        for(int i=0;i<m;i++)
        {
            Edge e=d[i];
            if(find_(e.u)!=find_(e.v))
            {
                vis[e.u]=vis[e.v]=true;
                unite(e.u,e.v);
                r+=e.w;
            }
        }
        
        bool fg=true;
         for(int i=1;i<=n;i++)///检测是否所有顶点入集合,保证是生成树啊
         {
             if(!vis[i])
                {
                    fg=false;
                    break;
                }
         }
         if(!fg)
         {
             cout<<"No"<<endl;
             continue;
         }
         
        int flag=0;
        for(int i=1;i<=26;i++)
        {
            if(l<=fib[i]&&r>=fib[i])
            {
                flag=1;
            }
        }
        if(flag)
            printf("Yes\n");
        else
            printf("No\n");

    }
    return 0;
}


你可能感兴趣的:(hdu4786-图论训练3-最小生成树)