HDU 4786 Fibonacci Tree(最小生成树变式)

探讨如何通过构建最大与最小生成树确定是否能形成含有Fibonacci数个白边的生成树。使用Kruskal算法并结合二分查找验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
 
题目大意:给定一个图,里面有的边是白色,剩下的全为黑色。问存不存在一个生成树里面包含的白色边的个数恰为Fibonacci numbers。
看了题解始终没明白。。。题解的解法是白边边权为 1 ,黑边边权为 0 。构造一个最大生成树,用掉的白边数就是所有生成树里白边的最大个数Max。再构造一个最小生树,用掉的白边数就是所有生成树里白边的最小个数Min。若Min~Max之间包含 Fibonacci numbers 就符合题意。据说是因为一条黑边可以被一条白边代替,依然保持图的连通性,所以只要Min ~ Max之间包含 Fibonacci numbers 就OK。依然不明白是为什么。。。求大神告知。生成树用 Kruskal,查找Fibonacci numbers 是打了一个表然后二分找的。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1e5 + 5;
int p[maxn],f[100],v[maxn],u[maxn],e[maxn],w[maxn];
int len = 1;
int n,m;
bool cmp(int x,int y)
{
    return w[x] < w[y];
}
void init()
{
    memset(f,0,sizeof(f));
    f[0] = 1,f[1] = 2;
    while(f[len] <= 100000) ++len,f[len] = f[len - 1] + f[len - 2];
}
int Find(int x)
{
    return p[x] == x ? x : p[x] = Find(p[x]);
}
int main()
{
    int t,x,y,c;
    scanf("%d",&t);
    init();
    int pre = t;
    while(t--)
    {
        scanf("%d %d",&n,&m);
        for(int i = 1;i <= m; ++i) e[i] = i;
        for(int i = 1;i <= m; ++i) scanf("%d %d %d",&u[i],&v[i],&w[i]);
        sort(e + 1,e + m + 1,cmp);
        int temp = 0,mi = 0,ma = 0;
        for(int i = 1;i <= n; ++i) p[i] = i;
        for(int i = 1;i <= m; ++i)
        {
            int now = e[i];
            int x = Find(u[now]),y = Find(v[now]);
            if(x != y)
            {
                ++temp;
                if(w[now] == 1) ++mi;
                p[y] = x;
            }
        }
        if(temp <  n - 1)
        {
            printf("Case #%d: No\n",pre - t);
            continue;
        }
        for(int i = 1;i <= n; ++i) p[i] = i;
        for(int i = m;i >= 1; --i)
        {
            int now = e[i];
            int x = Find(u[now]),y = Find(v[now]);
            if(x != y)
            {
                if(w[now] == 1) ++ma;
                p[y] = x;
            }
        }
        temp = lower_bound(f,f + len,mi) - f;
        printf("Case #%d: ",pre - t);
        if(f[temp] <= ma) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值