杭电4786--Fibonacci Tree(生成树)

题目: http://acm.hdu.edu.cn/showproblem.php?pid=4786

题意: 存不存在所含白边数为菲波那契数的生成树(包含图中所有节点)。 判断图是否连通那边还有点模糊。--->  枚举菲波那契数。 参考bin神代码。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
bool flag1, flag2;  int sum, Y;
struct Sorrow
{
    int a, b, c;
} edge[100001];
bool cmp1(Sorrow a, Sorrow b)
{
    return a.c < b.c;
}
bool cmp2(Sorrow a, Sorrow b)
{
    return a.c > b.c;
}
int father[100001], n;
void init()
{
    memset(father, -1, sizeof(father));
    /* for(int i = 1; i <= n; i++)
        father[i] = i; */
}
int Find(int a)
{
    if(father[a] == -1)
        return a;
    else
        return father[a] = Find(father[a]);
}
int mercy(int a, int b, int c)
{
//    int Y = 0;
    int Q = Find(a);
    int P = Find(b);
    if(Q != P)
    {
        father[Q] = P;
        if(c)
            Y++;
    }
    return Y;
}
bool Judge()
{
    int cnt = 0;
    for(int i = 1; i <= n; i++)
        if(Find(i) != Find(1))
            return false;
    return true;
}
int b[100001];
int Bitch()
{
    sum = 1;
    b[0] = 1; b[1] = 2;
    while(b[sum] < 100001)
    {
        b[sum+1] = b[sum] + b[sum-1];
        sum++;
    }
}
int main()
{
    int T, Q = 1;
    scanf("%d", &T);
    while(T--)
    {
        int m, L, R;
        init(); //flag1 = flag2 = true;
        scanf("%d %d", &n, &m);
        for(int i = 0; i < m; i++)
            scanf("%d%d%d", &edge[i].a, &edge[i].b, &edge[i].c);
        sort(edge, edge+m, cmp1); Y = 0;
        for(int i = 0; i < m; i++)
            L = mercy(edge[i].a, edge[i].b, edge[i].c);
        flag1 = Judge();
        init();
        sort(edge, edge+m, cmp2); Y = 0;
        for(int i = 0; i < m; i++)
            R = mercy(edge[i].a, edge[i].b, edge[i].c);
        flag2 = Judge();
        printf("Case #%d: ", Q++);
        if(flag1 == false && flag2 == false)
        {
            printf("No\n");
            continue;
        }
        Bitch();
        bool flag = false;
        for(int i = 0; i < sum; i++)
        {
            if(b[i] >= L && b[i] <= R)
                flag = true;
        }
        if(flag)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
} 
时间: 2024-09-30 12:26:59

杭电4786--Fibonacci Tree(生成树)的相关文章

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

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

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

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

HDU 4786 Fibonacci Tree 最小生成树变形

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

HDU--4786 Fibonacci Tree 生成树+贪心?

N个顶点,M条边,每条边可能为黑色或是白色( 0 or 1 ),问有没有可能用为斐波那契数的数目的白色边构成一棵生成树.所以需要删掉图中的环,根据每次删掉的边有一个白色边的上限和下限,判断一下中间有没有斐波那契数就可以了.实现方法是根据颜色排序,先放黑色边得到的是最小数目的白色边构成的生成树,先放白色边得到是最大数目的白色边构成的生成树. #include<cstring> #include<string> #include<fstream> #include<i

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

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 Fibonac

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 ( 最小生成树 )

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

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