Fibonacci Tree

F - Fibonacci Tree
Time Limit:2000MS Memory Limit:32768KB
64bit IO Format:%I64d & %I64u

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 <= 10 5) and M(0 <= M <= 10 5).
  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
题目大意:给出一些点,一些边,有黑边与白边,求其生成树中有没有白边的条数等于斐波拉契数列中的数。
分析:求得是白边的数,可以求出白边的上限与下限,再在这个区间内找有没有属于斐波拉契数列中的数,求上下限时用kruskal算法,优先选用白边,求出白边数的上限,优先选用黑边,求出白边数的下限。
代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string.h>
#include<cstdio>
#define inf 0x3f3f3f3f
#define maxn 100005
using namespace std;
struct node
{
    int st;
    int en;
    int len;
};
int n,m,ans;
int fa[maxn];
node E[maxn];
int fib[maxn];
bool cmp1(const node &n1,const node &n2)
{
    return n1.len>n2.len;
}//优先使用白边
bool cmp0(const node &n1,const node &n2)
{
    return n1.len<n2.len;
}//优先使用黑边
int find(int x)
{
     if(fa[x]==x)
     return fa[x];
     else
     return fa[x]=find(fa[x]);
}
void kruska()
{
     ans=0;
     for(int i=1;i<=n;i++)
     fa[i]=i;
     for(int i=1;i<=m;i++)
     {
         int fx=find(E[i].st);
         int fy=find(E[i].en);
         if(fx!=fy)
         {
             ans+=E[i].len;
             fa[fx]=fy;
         }
     }
}
int main()
{
    int t,i,j,k,a,b,c,x=1,y=2,w,min1,max1,flag,fg,fr;
    memset(fib,0,sizeof(fib));
    fib[1]=1;
    fib[2]=1;
    while(1)
    {
        fib[x+y]=1;
        w=x+y;
        x=y;
        y=w;
        if(x+y>maxn)
        break;
    }//预处理斐波拉契数列
    scanf("%d",&t);
    for(i=1;i<=t;i++)
    {
        fg=1;
        scanf("%d%d",&n,&m);
        for(j=1;j<=m;j++)
        {
             scanf("%d%d%d",&a,&b,&c);
             E[j].st=a;
             E[j].en=b;
             E[j].len=c;
        }
        sort(E+1,E+m+1,cmp1);
        kruska();
        max1=ans;//白边上限
        fr=find(1);
        for(int w=2;w<=n;w++)
        {
           if(find(w)!=fr)
           {fg=0;break;}
        }//若一开始图不连通,输出No
        sort(E+1,E+m+1,cmp0);
        kruska();
        min1=ans;//白边下限
        for(j=min1;j<=max1;j++)
        {
           if(fib[j]==1)
            {flag=1;break;}
           else
            {flag=0;continue;}
        }//求上下限间是否有符合斐波拉契数列中的数
        if(fg==0)
        printf("Case #%d: No\n",i);
        else if(flag==0)
        printf("Case #%d: No\n",i);
        else if(flag==1)
        printf("Case #%d: Yes\n",i);
    }
    getchar();
    getchar();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值