HDU 4786 Fibonacci Tree

Fibonacci Tree

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


 

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

 

题目明说了是生成树,但是树的权重代表是其颜色。

题目要求:找到一种生成树(不要求是最小生成树),其中包含的白色边(1代表的)个数为一个斐波那契数列中的数。

思路:如果采用暴力的话,复杂度为O(N*M),会超时。需要采用一种巧妙的方法。以c(边的颜色)分别按照升序和降序排序,假设分别得到es1和es2两个序列,那么我们按照最小生成树的规则在这两种序列中分别生成两棵树t1和她,那么t1是所有生成树中包含黑边最多的(也就是包含白边最少的),而t2则是包含白边最多的。由于c为0和1,采用相加的方法,我们就能得到生成树中白边数的范围。由于数据中不存在重复边,那么我们可以确定范围中是否包含有一个斐波那契数即可。

PS: 若不存在生成树,也是NO

 

#include <cstdio>
#include <string.h>
#include <algorithm>
const int MaxN = 100005, MaxM = 100005;
struct edge
{
    int u, v, c;
}edges[MaxM];
int T, N, M, fa[MaxN];
int fib[25];

inline bool cmp1(edge e1, edge e2)    // 黑边优先
{
    return e1.c < e2.c;
}

inline bool cmp2(edge e1, edge e2)    // 白边优先
{
    return e1.c > e2.c;
}

int myfind(int c)
{
    int temp, f = c;
    while(fa[f] != f)    f = fa[f];
    while(fa[c] != f)
    {
        temp = fa[c];
        fa[c] = f;
        c = temp;
    }
    return f;
}

bool myunion(int a, int b)
{
    int af = myfind(a), bf = myfind(b);
    if(af != bf)
    {
        fa[bf] = af;
        return true;
    }
    return false;
}

int main()
{
    scanf("%d", &T);
    fib[0] = fib[1] = 1;
    for(int i = 2; i < 25; i++)    fib[i] = fib[i - 1] + fib[i - 2];
    for(int t = 1; t <= T; t++)
    {
        scanf("%d %d", &N, &M);
        for(int i = 0; i < M; i++)
            scanf("%d %d %d", &edges[i].u, &edges[i].v, &edges[i].c);
        int len = N - 1;
        std::sort(edges, edges + M, cmp1);
        for(int i = 0; i <= N; i++)    fa[i] = i;
        int num = 0, _min = 0, _max = 0;        // 白边数的最少和最多
        for(int i = 0; i < M; i++)
        {
            if(num >= len)    break;
            if(myunion(edges[i].u, edges[i].v))
            {
                num++;
                _min += edges[i].c;
            }
        }
        num = 0;
        for(int i = 0; i <= N; i++)    fa[i] = i;
        std::sort(edges, edges + M, cmp2);
        for(int i = 0; i < M; i++)
        {
            if(num >= len)    break;
            if(myunion(edges[i].u, edges[i].v))
            {
                num++;
                _max += edges[i].c;
            }
        }
        bool flag = false;      // 判断是否存在斐波那契数在范围内或者不存在生成树
        if(num == len)
        {
            for(int i = 0; i < 25; i++)
            {
                if(fib[i] >= _min && fib[i] <= _max)
                {
                    flag = true;
                    break;
                }
            }
        }
        printf("Case #%d: %s\n", t, flag ? "Yes" : "No");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值