【2013 ICPC亚洲区域赛成都站 F】Fibonacci Tree(最小生成树+思维)

探讨了在一个带权图中寻找生成树的问题,其中边的权值为1或2,代表颜色,目标是找到一个生成树,其白色边的数量为斐波那契数。通过判断图的连通性和使用Kruskal算法,确定了白边数量的可能范围,并检查该范围内是否存在斐波那契数。

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

题意:

有一个n个点,m条边的图,给定边的权值为1(白色)或2(黑色),问是否存在一个生成树,使得其中白边的数量为斐波那契数?

题解:

首先判断这个图是否为连通图,若不是直接输出No。

然后只要用白边优先(最大生成树)的总权值减去黑边优先(最小生成树)的总权值,就可以得到一个白边数量的区间,然后枚举斐波那契数即可。

题目链接:HDOJ 4786

#include<bits/stdc++.h>
#define MAX 100000
using namespace std;
int n,m,p1[MAX+5],p2[MAX+5],fib[55];
struct edge{
    int u,v,w;
}e[MAX+5];
int find(int r,int p[])
{
    if(p[r]!=r)
        p[r]=find(p[r],p);
    return p[r];
}
void init(int p[])
{
    for(int i=0;i<=MAX;i++)
        p[i]=i; 
}
bool cmp1(edge a,edge b){return a.w>b.w;}
bool cmp2(edge a,edge b){return a.w<b.w;}
int KurskalMax(int p[])
{
    init(p);
    sort(e,e+m,cmp1);
    int cnt=0,cost=0,i;
    for(i=0;i<m;i++)
    {
        int fu=find(e[i].u,p),fv=find(e[i].v,p);
        if(fu!=fv)
        {
            p[fu]=fv;
            cost+=e[i].w;
            cnt++;
        }
        if(cnt==n-1)break;
    }
    return cost;
}
int KurskalMin(int p[])
{
    init(p);
    sort(e,e+m,cmp2);
    int cnt=0,cost=0,i;
    for(i=0;i<m;i++)
    {
        int fu=find(e[i].u,p),fv=find(e[i].v,p);
        if(fu!=fv)
        {
            p[fu]=fv;
            cost+=e[i].w;
            cnt++;
        } 
        if(cnt==n-1)break;
    }
    if(cnt!=n-1)return -1;
    return cost;
}
int main()
{
    int i;
    fib[1]=1;fib[2]=2;
    for(int i=3;i<=40;i++)
        fib[i]=fib[i-1]+fib[i-2];
    int T;
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++)
    {
        scanf("%d%d",&n,&m); 
        for(i=0;i<m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            e[i].u=u;e[i].v=v;e[i].w=w;
        }
        int L=KurskalMin(p1),R=KurskalMax(p2),flag=0;
        if(L!=-1)
        {
            for(i=L;i<=R;i++)
            {
                if(fib[lower_bound(fib+1,fib+40+1,i)-fib]==i)
                {
                    flag=1;
                    break;
                }
            }
        }
        printf("Case #%d: ",cas);
        if(flag)printf("Yes\n");
        else printf("No\n");
    } 
    return 0;
}

转载于:https://www.cnblogs.com/kannyi/p/9871921.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值