Fibonacci Tree

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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
 
    生成树的深入理解,也就是说:
  (1)白边的最小条数(L)与最大条数(R)是一个定值 ;
  (2)黑边可以由白边替换。
 
#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
#include <stdio.h>
using namespace std;
const int Max_N = ;
struct Edge{
int u ;
int v ;
int w ;
} ;
Edge edge[Max_N] ;
int N , M; bool cmp1(Edge A ,Edge B){
return A.w < B.w ;
} bool cmp2(Edge A ,Edge B){
return A.w > B.w ;
} int father[Max_N] ; int find_father(int x){
if(x == father[x])
return x ;
else
return father[x] = find_father(father[x]) ;
} int gao(){
int sum = ,brige = ;
for(int i = ; i <= N ; i++)
father[i] = i ;
for(int i = ; i <= M ; i++){
int f_u = find_father(edge[i].u) ;
int f_v = find_father(edge[i].v) ;
if(f_u != f_v){
brige ++ ;
sum += edge[i].w ;
father[f_u] = f_v ;
}
if(brige == N-)
break ;
}
return brige == N- ? sum : - ;
} int fibo[] ; void init_fibo(){
fibo[] = ;
fibo[] = ;
for(int i = ; i <= ; i++)
fibo[i] = fibo[i-] + fibo[i-] ;
} int judge(){
int L , R ;
sort(edge+ ,edge++M, cmp1) ;
L = gao() ;
sort(edge+ ,edge++M ,cmp2) ;
R = gao() ;
if(L == -)
return ;
for(int i = ;i < ;i++){
if(L <= fibo[i] && fibo[i] <= R)
return ;
}
return ;
} int main(){
init_fibo() ;
int T ;
scanf("%d",&T) ;
for(int cas = ;cas <= T; cas++){
scanf("%d%d",&N,&M) ;
for(int i = ;i <= M ;i++)
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w) ;
printf("Case #%d: %s\n",cas,judge()? "Yes" : "No") ;
}
return ;
}

HDU 4786 Fibonacci Tree的更多相关文章

  1. hdu 4786 Fibonacci Tree (2013ACMICPC 成都站 F)

    http://acm.hdu.edu.cn/showproblem.php?pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) ...

  2. HDU 4786 Fibonacci Tree(生成树,YY乱搞)

    http://acm.hdu.edu.cn/showproblem.php? pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others ...

  3. HDU 4786 Fibonacci Tree 最小生成树

    Fibonacci Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4786 Description Coach Pang is intere ...

  4. HDU 4786 Fibonacci Tree (2013成都1006题)

    Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  5. hdu 4786 Fibonacci Tree(最小生成树)

    Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  6. 【HDU 4786 Fibonacci Tree】最小生成树

    一个由n个顶点m条边(可能有重边)构成的无向图(可能不连通),每条边的权值不是0就是1. 给出n.m和每条边的权值,问是否存在生成树,其边权值和为fibonacci数集合{1,2,3,5,8...}中 ...

  7. HDU 4786 Fibonacci Tree 生成树

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4786 题意:有N个节点(1 <= N <= 10^5),M条边(0 <= M <= ...

  8. hdu 4786 Fibonacci Tree 乱搞 智商题目 最小生成树

    首先计算图的联通情况,如果图本身不联通一定不会出现生成树,输出"NO",之后清空,加白边,看最多能加多少条,清空,加黑边,看能加多少条,即可得白边的最大值与最小值,之后判断Fibo ...

  9. HDU 4786 Fibonacci Tree (2013成都1006题) 最小生成树+斐波那契

    题意:问生成树里能不能有符合菲波那切数的白边数量 思路:白边 黑边各优先排序求最小生成树,并统计白边在两种情况下数目,最后判断这个区间就可以.注意最初不连通就不行. #include <stdi ...

随机推荐

  1. beeframework开发笔记1

    1.商品页弹出规格去掉(null) wzmzy/shop/model/extensions/GOOD_SPEC_VALUE+TagList.m 2.

  2. MVC4 WebAPI(二)——Web API工作方式

    http://www.cnblogs.com/wk1234/archive/2012/05/07/2486872.html 在上篇文章中和大家一起学习了建立基本的WebAPI应用,立刻就有人想到了一些 ...

  3. 如何将NetBeans转换为英文版(图)

    很郁闷,又是一个系统语言的问题. NetBeans下载下来之后,怎么装都是个中文版,或者半中文半英文,不伦不类的.上网找了一个圈,很多都讲得不清楚,最后终于有了一个答案: 在NetBeans的配置文件 ...

  4. Maven修改镜像仓库地址

    修改maven根目录下的conf文件夹中的setting.xml文件,如果你修改了默认仓库的存储位置,即.m2文件夹下没有本地仓库,但是有个setting.xml文件,那就修改这个文件就可以. 具体内 ...

  5. bzoj3743 Kamp

    Description 一颗树n个点,n-1条边,经过每条边都要花费一定的时间,任意两个点都是联通的. 有K个人(分布在K个不同的点)要集中到一个点举行聚会. 聚会结束后需要一辆车从举行聚会的这点出发 ...

  6. VS合集/6.0/2005/2008/2010/2012/2013 绿色版精简版

    VS合集/6.0/2005/2008/2010/2012/2013 绿色版精简版 找到这里的都是老司机,别的不多说了 链接: http://pan.baidu.com/s/1i5IyYZb       ...

  7. activiti自定义流程之整合(一):整体环境配置

    结合之前所说的自定义流程的思路,分别是后台.前台.整合,之前的内容也分别进行了相关的练习和尝试,现在就该到了最后的整合了,依旧是以实现功能为目的,细节暂且不去管他. 因为我们实际项目后端用的是spri ...

  8. storm的作业单元:Topology

    Storm系统的数据处理应用单元,是被打包的被称为Topology的作业. 它是由多个数据处理阶段组合而成的,而每个处理阶段在构造时被称为组件(Component),在运行时被称为任务. 那么,组件根 ...

  9. IGS_学习笔记10_IREP监控SOA Integration和日志设定(案例)

    20150506 Created By BaoXinjian

  10. HDU 2089 不要62(数位dp入门)

    题意:统计区间 [a,b] 中不含 4 和 62 的数字有多少个. 题解:这是数位DP的入门题了,首先要理解数DP的原理,DP[i][j]:代表第i位的第j值,举个栗子:如4715   数位数是从右向 ...