HDOJ 4786 Fibonacci Tree

最大生成树夹最小生成树,老题目了,依稀记得当年在成都靠这题捡了个铜。。。。。

Fibonacci Tree

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1572    Accepted Submission(s): 479

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

2013 Asia Chengdu Regional Contest

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn=100100;

int nf,fib[100];

int getFib()
{
    fib[0]=1;fib[1]=2;
    nf=2;
    for(int i=2;fib[nf-1]<=100100;i++)
    {
        fib[nf]=fib[nf-1]+fib[nf-2];
        nf++;
    }
}

int n,m;
int fa[maxn];

int find(int x)
{
    if(x==fa[x]) return x;
    return fa[x]=find(fa[x]);
}

struct Edge
{
    int u,v,c;
}edge[maxn];

bool cmp1(Edge x,Edge y)
{
    return x.c<y.c;
}

bool cmp2(Edge x,Edge y)
{
    return x.c>y.c;
}

int Kruscal()
{
    int cnt=n,ans=0;
    for(int i=0;i<=n+1;i++) fa[i]=i;
    for(int i=0;i<m;i++)
    {
        int f1=find(edge[i].u);
        int f2=find(edge[i].v);

        if(f1!=f2)
        {
            fa[f1]=f2;
            ans+=edge[i].c;
            cnt--;
            if(cnt==1) break;
        }
    }
    return (cnt==1)?ans:0x3f3f3f3f;
}

int main()
{
    getFib();
    int T_T,cas=1;
    scanf("%d",&T_T);
    while(T_T--)
    {
        scanf("%d%d",&n,&m);

        for(int i=0;i<m;i++)
        {
            int a,b,w;
            scanf("%d%d%d",&a,&b,&w);
            edge[i].u=a; edge[i].v=b; edge[i].c=w;
        }
        sort(edge,edge+m,cmp1);
        int MiMST=Kruscal();
        sort(edge,edge+m,cmp2);
        int MxMST=Kruscal();

        bool flag=false;
        for(int i=0;i<nf;i++)
        {
            if(fib[i]>=MiMST&&fib[i]<=MxMST)
            {
                flag=true; break;
            }
        }

        printf("Case #%d: ",cas++);
        if(flag) puts("Yes");
        else puts("No");
    }
    return 0;
}
时间: 2024-10-20 04:52:42

HDOJ 4786 Fibonacci Tree的相关文章

hdoj 4786 Fibonacci Tree 【生成树+想法】

题目:hdoj 4786 Fibonacci Tree 题意:给出 n 个点 m 条边的图,边只有两种颜色,白色和黑色,让你判断能不能让一个生成树中白边的个数为斐波那契数. 分析:这是个想法题目,前提是知道生成树的定义:生成树必须是所有点都在树中 那么既然要是斐波那契数,我只要把白色边的最大个数和最小个数求出来,如果这个范围内有斐波那契数的话,那么就满足条件. 当然这样求的前提条件是期间的所有的生成树都是满足条件的.即都是满足能够生成树的. ok,AC代码: #include<iostream>

Hdoj 4786 Fibonacci Tree 【kruskal】

Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2588 Accepted Submission(s): 822 Problem Description Coach Pang is interested in Fibonacci numbers while Uncle Yang wants him to do s

HDU 4786 Fibonacci Tree 最小生成树变形

思路: 这题比赛的时候想了好久,最后队友机智的想到了. 不过那时不是我敲的,现在敲的1A. 想好就容易了. 直接把1或者0当做边的权值,然后按边从小到大排序,然后算最小生成用到了几条白边,然后再按边从大到小排序,然后再算白边用了几条.然后最小和最大需要用到的白边都算出来了.如果在这最小最大区间中存在那个啥数列的话就是Yes,否则就是No. 为什么在这区间里面就是对的呢?刚开始我也想了好久,然后发现,因为白边权值是1,然后黑边是0,然后假设用到白边最小的是6,最大的是10,那么,我们可以用黑边去替

HDU 4786 Fibonacci Tree(生成树,YY乱搞)

http://acm.hdu.edu.cn/showproblem.php?pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1733    Accepted Submission(s): 543 Problem Description Coach Pang is interested in

POJ 4786 Fibonacci Tree

Fibonacci Tree Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 478664-bit integer IO format: %I64d      Java class name: Main Coach Pang is interested in Fibonacci numbers while Uncle Yang wants him to do som

HDU 4786 Fibonacci Tree 并查集+生成树=kruskal

Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2149    Accepted Submission(s): 682 Problem Description Coach Pang is interested in Fibonacci numbers while Uncle Yang wants him t

hdu 4786 Fibonacci Tree(最小生成树)

Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2952    Accepted Submission(s): 947 Problem Description Coach Pang is interested in Fibonacci numbers while Uncle Yang wants him t

hdu 4786 Fibonacci Tree ( 最小生成树 )

Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2487    Accepted Submission(s): 796 Problem Description Coach Pang is interested in Fibonacci numbers while Uncle Yang wants him t

HDU 4786 Fibonacci Tree 生成树

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4786 题意:有N个节点(1 <= N <= 10^5),M条边(0 <= M <= 10^5).当中一部分边被染成了黑色,剩下的边是白色,问能不能建立一棵树,树中有斐波那契数个白色边. 思路:用克鲁斯卡尔建三次树,第一是用全部边建树.推断能否建成一棵树,第二次用黑边建树,最多能够用到x条黑边(不成环),n-1-x就是最少须要用的白边的数量,第三次用白边建树,最多能够用到y条白边.假设在[y