hdoj 4786 Fibonacci Tree
来源:互联网 发布:java 正态分布随机数 编辑:程序博客网 时间:2024/05/22 00:11
Fibonacci Tree
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3583 Accepted Submission(s): 1152
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, ... )
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).
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题意:题目要求 1 表示白线连接,0 表示黑线连接!图要连通,但所构成图的白色线个数必须符合是属于fib序列!并不需要所有的线全都是白线!思路:可以根据线条的颜色,先尽量用白色线条进行图的连通,求出满足连通时白色线条的条数max ,相反,尽量用黑色线条,求出此时满足连通的白色线条的个数min,再min和max之间寻找是否有fib满足数列的数!具体看代码:#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define N 110000int set[N],n,m;int fib[1000000+2100];struct node{int start;int end;int judge;}num[N];bool cmp1(node a,node b)//按照先黑再白顺序 {return a.judge <b.judge;}bool cmp2(node a,node b)//顺序与前面相反 {return a.judge>b.judge;}void dabiao(){ fib[1]=1;fib[2]=2;for(int i=3;i<1000000+100;i++){fib[i]=fib[i-1]+fib[i-2];}}int find(int p)//找根节点 {int t;int child=p;while(p!=set[p])p=set[p];while(child!=p){t=set[child];set[child]=p;child=t;}return p;}void merge(int x,int y)//加入集合 {int fx=find(x);int fy=find(y);if(fx!=fy)set[fx]=fy;}int main(){int t,k=1;memset(fib,0,sizeof(fib));dabiao();scanf("%d",&t);while(t--){int i,j,n,m,max=0,min=0;scanf("%d%d",&n,&m);for(i=1;i<=m;i++)scanf("%d%d%d",&num[i].start ,&num[i].end ,&num[i].judge );for(i=1;i<=n;i++) set[i]=i;sort(num+1,num+m,cmp2); //尽量先用白色线条! for(i=1;i<=m;i++){if(find(num[i].start )!=find(num[i].end )){merge(num[i].start ,num[i].end );if(num[i].judge ) max++;}} for(i=1;i<=n;i++) set[i]=i;sort(num+1,num+m,cmp1);//尽量先用黑色线条! for(i=1;i<=m;i++){if(find(num[i].start )!=find(num[i].end)){merge(num[i].start ,num[i].end );if(num[i].judge )min++;}} printf("Case #%d: ",k++);int exit=0;for(i=1;i<=n;i++)//看是否连通! {if(set[i]==i) exit++; if(exit>1) break;}if(exit>1){printf("No\n");continue;}exit=0;for(i=1;fib[i]<=max;i++){if(fib[i]>=min&&fib[i]<=max){exit++;}if(exit)break;}if(exit)printf("Yes\n");elseprintf("No\n");}return 0;}
0 0
- HDOJ 4786 Fibonacci Tree
- hdoj 4786 Fibonacci Tree
- Hdoj 4786 Fibonacci Tree
- HDOJ 4786 - Fibonacci Tree
- Hdoj 4786 Fibonacci Tree 【kruskal】
- HDOJ 4786 - Fibonacci Tree 理解生成树
- HDOJ--4786--Fibonacci Tree【生成树】
- hdoj 4786 Fibonacci Tree 【生成树+想法】
- 【并查集】 HDOJ 4786 Fibonacci Tree
- hdoj 4786 Fibonacci Tree (并查集)
- HDOJ 题目4786 Fibonacci Tree(克鲁斯卡尔)
- HDOJ 4786 Fibonacci Tree(最小生成树--kruskul)
- hdoj 4786 Fibonacci Tree 【最小生成树&&最大生成树】
- hdoj 4786 Fibonacci Tree 【最小生成树&&最大生成树】
- HDU 4786 Fibonacci Tree
- hdu 4786 Fibonacci Tree
- hdu 4786 Fibonacci Tree
- HDU 4786 Fibonacci Tree
- hibernate N+1问题
- 判断是手机端还是PC端的验证
- 允许VM拥有多个IP/MAC
- coreseek/sphinx文档摘要
- 观察者模式
- hdoj 4786 Fibonacci Tree
- VS 2010 opengl环境配置
- pg9.4利用pgAgent创建定时任务
- CityEngine CGA语法之偏移函数 offset
- Delete Node in a Linked List
- hdoj Another Sum Problem 2576 (数学题)
- 控制寄存器:指令指针寄存器IP:存放下一条指令的地址的偏移量,标志寄存器FR(16):
- OpenJudge_P8208 切分矩形组
- 【FastDev4Android框架开发】CardView完全解析与RecyclerView结合使用(三十二)