HDU 4786--Fibonacci Tree【并查集,最小生成树】
2015-03-28 15:44
501 查看
Fibonacci Tree
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2579 Accepted Submission(s): 819
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
一开始超时的十几遍,就是不知道为啥错,后来写3560的时候也是超时,无意中发现头文件如果是#include<stdio.h>就AC,当时想哭的心都有的。以后再也不用#incldue<cstdio>了。
///* //题目大意:给定一个图,确定是一个连通图。每条边有一定的颜色,黑或白。求是否有这样一棵生成树保证所有的树边的总数为一个Fibonacci数。 //解题思路: //按边的颜色排序两次,一次黑边优先,利用并查集构造一个生成树,此时是所有方法中白边最少的情况,x。 //而后白边优先,得出的是白边最多的情况,y。找找x,y二者之间是否包括一个Fibonacci数即可。 //*/ #include <stdio.h> #include <string.h> #include <algorithm> #define maxn 100100 using namespace std; struct node { int u,v,c; }; int cmp1(node a,node b){ return a.c<b.c; } int cmp2(node a,node b){ return a.c>b.c; } int p[maxn]; node s[maxn]; int n,m,ans; int find(int x){ if(x==p[x]) return p[x]; return p[x]=find(p[x]); } int a[35]; void fib(){ a[1]=1; a[2]=2; for(int i=3;i<30;++i) a[i]=a[i-1]+a[i-2]; } int check (int l,int r){ if(l==-1 || r==-1) return 0; for(int i=1;i<30;++i){ if(l<=a[i] && a[i]<=r) return 1; } return 0; } int main(){ int t,i,q=0; scanf("%d",&t); fib(); while(t--){ scanf("%d%d",&n,&m); for(i=0;i<m;++i) scanf("%d%d%d",&s[i].u,&s[i].v,&s[i].c); for(int i=0;i<=n;++i) p[i]=i; sort(s,s+m,cmp1); int sum=0; ans=0; int num1,num2; for(i=0;i<m;++i){ int fa=find(s[i].u); int fb=find(s[i].v); if(fa!=fb){ p[fa]=fb; sum+=s[i].c; } } for(i=1;i<=n;++i){ if(p[i]==i) ans++; if(ans>1) num1=-1; else num1=sum; } // printf("sum=%d\n",sum); sum=0; ans=0; for(int i=0;i<=n;++i) p[i]=i; sort(s,s+m,cmp2); for(i=0;i<m;++i){ int fa=find(s[i].u); int fb=find(s[i].v); if(fa!=fb){ p[fa]=fb; sum+=s[i].c; } } for(i=1;i<=n;++i){ if(p[i]==i) ans++; if(ans>1) num2=-1; else num2=sum; } // printf("sum1=%d\n",sum); // printf("%d %d \n",num1,num2); if(check(num1,num2)){ printf("Case #%d: ",++q); printf("Yes\n"); } else{ printf("Case #%d: ",++q); printf("No\n"); } } return 0; }
相关文章推荐
- hdu 4786 Fibonacci Tree ( 最小生成树 )
- hdu 4786 Fibonacci Tree 乱搞 智商题目 最小生成树
- hdu 4786 Fibonacci Tree 乱搞 智商题目 最小生成树
- HDU 4786 Fibonacci Tree 最小生成树 kruskal算法
- hdu 4786 Fibonacci Tree 乱搞 智商题目 最小生成树
- HDU 4786 Fibonacci Tree(最小生成树变式)
- hdu 4786 Fibonacci Tree(最小生成树)
- hdu 4786 Fibonacci Tree 乱搞 智商题目 最小生成树
- hdu 4786 Fibonacci Tree(最小生成树)
- HDU-#4786 Fibonacci Tree(最大最小生成树)
- HDU_4786_Fibonacci Tree(并查集、生成树、斐波那契)(13成都现场F题)
- hdu 4786 Fibonacci Tree 乱搞 智商题目 最小生成树
- hdu 4786 Fibonacci Tree 乱搞 智商题目 最小生成树
- hdu 4786 Fibonacci Tree(最小生成树)
- hdu 4786 Fibonacci Tree 乱搞 智商题目 最小生成树
- hdu 4786 Fibonacci Tree(最小生成树)
- HDU - 4786 Fibonacci Tree 最小生成树+最大生成树
- hdu 4786 Fibonacci Tree 乱搞 智商题目 最小生成树
- hdu 4786 Fibonacci Tree(最小生成树)
- hdu 4786 Fibonacci Tree(最小生成树)