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)黑边可以由白边替换。
 
  1. #include <iostream>
  2. #include <string>
  3. #include <string.h>
  4. #include <algorithm>
  5. #include <stdio.h>
  6. using namespace std;
  7. const int Max_N = ;
  8. struct Edge{
  9. int u ;
  10. int v ;
  11. int w ;
  12. } ;
  13. Edge edge[Max_N] ;
  14. int N , M;
  15.  
  16. bool cmp1(Edge A ,Edge B){
  17. return A.w < B.w ;
  18. }
  19.  
  20. bool cmp2(Edge A ,Edge B){
  21. return A.w > B.w ;
  22. }
  23.  
  24. int father[Max_N] ;
  25.  
  26. int find_father(int x){
  27. if(x == father[x])
  28. return x ;
  29. else
  30. return father[x] = find_father(father[x]) ;
  31. }
  32.  
  33. int gao(){
  34. int sum = ,brige = ;
  35. for(int i = ; i <= N ; i++)
  36. father[i] = i ;
  37. for(int i = ; i <= M ; i++){
  38. int f_u = find_father(edge[i].u) ;
  39. int f_v = find_father(edge[i].v) ;
  40. if(f_u != f_v){
  41. brige ++ ;
  42. sum += edge[i].w ;
  43. father[f_u] = f_v ;
  44. }
  45. if(brige == N-)
  46. break ;
  47. }
  48. return brige == N- ? sum : - ;
  49. }
  50.  
  51. int fibo[] ;
  52.  
  53. void init_fibo(){
  54. fibo[] = ;
  55. fibo[] = ;
  56. for(int i = ; i <= ; i++)
  57. fibo[i] = fibo[i-] + fibo[i-] ;
  58. }
  59.  
  60. int judge(){
  61. int L , R ;
  62. sort(edge+ ,edge++M, cmp1) ;
  63. L = gao() ;
  64. sort(edge+ ,edge++M ,cmp2) ;
  65. R = gao() ;
  66. if(L == -)
  67. return ;
  68. for(int i = ;i < ;i++){
  69. if(L <= fibo[i] && fibo[i] <= R)
  70. return ;
  71. }
  72. return ;
  73. }
  74.  
  75. int main(){
  76. init_fibo() ;
  77. int T ;
  78. scanf("%d",&T) ;
  79. for(int cas = ;cas <= T; cas++){
  80. scanf("%d%d",&N,&M) ;
  81. for(int i = ;i <= M ;i++)
  82. scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w) ;
  83. printf("Case #%d: %s\n",cas,judge()? "Yes" : "No") ;
  84. }
  85. return ;
  86. }

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. jQuery制作视频网站的展示效果

    效果:如图所示,用户可以单击左上角的左右箭头,来控制视频展示的左右滚动. 当单击向右箭头时下面的展示视频会向左滚动隐藏,同时新的视频展示会以滚动方式显示出来.向左同理. css: ;; word-br ...

  2. Python类,域,方法,对象,继承

    类和对象: 是面向对象编程的两个主要方面,类创建一个新类型,而对象这个类的实例.. 域: 属于一个对象或类的变量被称为域.域有两种类型: 属于每个实例(类的对象)或属于类本身.它们分别被称为实例变量和 ...

  3. golang的验证码相关的库

    识别库 https://github.com/goghcrow/capture_easy 生成验证码的库 https://github.com/hanguofeng/gocaptcha 生成图片水印 ...

  4. makefile学习笔记(多目录嵌套调用、变量使用)

    http://blog.csdn.net/leexiang_han/article/details/9274229   学习了几天的makefile的嵌套调用编写也有一些心得,先声明,我也是初学者写文 ...

  5. System.Data.SqlClient.SqlException.Number的所有错误值列表

    在系统数据库(master或msdb或model)的架构(sys)的视图(messages)中: SELECT [message_id]      ,[language_id]      ,[seve ...

  6. TortoiseSVN 同步分支

    对比工具可以使用 winmerge 和 beyond compare ,winmerge免费小巧,beyond compare功能更强大.这两款工具都比TortoiseSVN自带的对比工具要好一些. ...

  7. 71. Simplify Path

    Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...

  8. 【MySQL】性能优化 之 延迟关联

    [背景]  某业务数据库load 报警异常,cpu usr 达到30-40 ,居高不下.使用工具查看数据库正在执行的sql ,排在前面的大部分是: SELECT id, cu_id, name, in ...

  9. Spark参数配置说明

    1  修改$SPARK_HOME/conf目录下的spark-defaults.conf文件 添加以下配置项 spark.sql.hive.convertMetastoreParquet       ...

  10. HDU 1213 How Many Tables(并查集,简单)

    题解:1 2,2 3,4 5,是朋友,所以可以坐一起,求最小的桌子数,那就是2个,因为1 2 3坐一桌,4 5坐一桌.简单的并查集应用,但注意题意是从1到n的,所以要减1. 代码: #include ...