HDU4736-Fibonacci Tree

来源:互联网 发布:现货分时指标公式源码 编辑:程序博客网 时间:2024/06/11 03:37
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): 2509    Accepted Submission(s): 801


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
24 41 2 12 3 13 4 11 4 05 61 2 11 3 11 4 11 5 13 5 14 2 1
 

Sample Output
Case #1: YesCase #2: No
 

Source
2013 Asia Chengdu Regional Contest
 

Recommend
We have carefully selected several similar problems for you:  5177 5176 5175 5173 5172 
 

题目要求:就是在所给的多条边中,判断是否存在一个生成树,使得白色的边数为fibonacci数;
感觉很简单,以白边1和黑边0为权值求一次最小生成数t1和最大生成树t2(数值)。判断在t1~t2间是否有fibonacci数;不能构成生成树的直接输出NO;这里用kruskal求最小(da)生成树.

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>

#define maxn 100000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657

const ull inf = 1LL << 61;
const double eps=1e-5;

using namespace std;

bool cmp(int a,int b){
return a>b;
}
int fb[30];
int fa[maxn];
void f()
{
fb[1]=1;
fb[2]=1;
for(int i=3;i<=30;i++)
fb[i]=fb[i-1]+fb[i-2];
}
void init(int n)
{
for(int i=1;i<=n;i++)
fa[i]=i;
}
int findfa(int x)
{
if(x==fa[x])return x;
else return fa[x]=findfa(fa[x]);
}
void Union(int x,int y)
{
int fa_x=findfa(x);
int fa_y=findfa(y);
fa[fa_x]=fa_y;
}
struct node
{
int u,v,w;
}edge[maxn];
bool cmp1(node a,node b)
{
return a.w<b.w;
}
bool cmp2(node a,node b)
{
return a.w>b.w;
}
int n,m;
int Kruskal(int k)
{
init(n);
if(k==1)sort(edge+1,edge+1+m,cmp1);
if(k==2)sort(edge+1,edge+1+m,cmp2);
int cnt=1;
int ans=0;
for(int i=1;i<=m;i++)
{
if(findfa(edge[i].u)!=findfa(edge[i].v))
{
Union(edge[i].u,edge[i].v);
ans+=edge[i].w;
cnt++;
}
}
if(cnt==n)
{
return ans;
}
else return 0;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t;
cin>>t;
f();
for(int i=1;i<=t;i++)
{
cout<<"Case #"<<i<<": ";
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
int t1=Kruskal(1);
int t2=Kruskal(2);
int mark=0;
for(int i=1;fb[i]<=t2;i++)
{
if(t1<=fb[i]&&fb[i]<=t2){mark=1;cout<<"Yes"<<endl;break;}
}
if(!mark)cout<<"No"<<endl;
}
return 0;
}



0 0