Fibonacci Tree

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4028    Accepted Submission(s): 1252

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
题意:n个点m条边,其中有一些边是白边,一些边是黑边,问是否存在一棵树上的白边数量是斐波拉契数列里面的某个数.
题解:很巧妙的思想,先按白边排序将白边最多的树选出来,然后黑边排序将白边最少的树选出来。然后如果有斐波拉契数在两棵树的大小中间(因为如果存在的话,是可以通过删边得到的),如果存在,就Ok,还要判一下连通分量。

  1. #include <stdio.h>
  2. #include <algorithm>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <queue>
  6. using namespace std;
  7. const int N = ;
  8. int father[N];
  9. struct Edge{
  10. int u,v,color;
  11. }edge[N];
  12. int _find(int x){
  13. if(x!=father[x]) {
  14. father[x] = _find(father[x]);
  15. }
  16. return father[x];
  17. }
  18.  
  19. int n,m;
  20. int cmp(Edge a,Edge b){
  21. return a.color>b.color;
  22. }
  23. int cmp1(Edge a,Edge b){
  24. return a.color<b.color;
  25. }
  26. int kruskal(){
  27. int cost=;
  28. for(int i=;i<m;i++){
  29. int x=_find(edge[i].u);
  30. int y=_find(edge[i].v);
  31. if(x!=y){
  32. father[x] = y;
  33. cost+=edge[i].color;
  34. }
  35. }
  36. return cost;
  37. }
  38. bool vis[N];
  39. void init(){
  40. memset(vis,false,sizeof(vis));
  41. int a=,b=;
  42. vis[]=true,vis[] =true;
  43. while(a+b<N){
  44. vis[a+b]=true;
  45. swap(a,b);
  46. b = a+b;
  47. }
  48. }
  49. int main()
  50. {
  51. int tcase;
  52. scanf("%d",&tcase);
  53. int t = ;
  54. init();
  55. while(tcase--){
  56. scanf("%d%d",&n,&m);
  57. for(int i=;i<m;i++){
  58. scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].color);
  59. }
  60. for(int i=;i<=n;i++) father[i] = i;
  61. sort(edge,edge+m,cmp);
  62. int maxn = kruskal();
  63. int ans = ;
  64. for(int i=;i<=n;i++){
  65. if(father[i]==i) ans++;
  66. if(ans>) break;
  67. }
  68. if(ans>) {
  69. printf("Case #%d: No\n",t++);
  70. continue;
  71. }
  72. for(int i=;i<=n;i++) father[i] = i;
  73. sort(edge,edge+m,cmp1);
  74. int minn = kruskal();
  75. ans = ;
  76. for(int i=;i<=n;i++){
  77. if(father[i]==i) ans++;
  78. if(ans>) break;
  79. }
  80. if(ans>) {
  81. printf("Case #%d: No\n",t++);
  82. continue;
  83. }
  84. //printf("%d %d\n",minn,maxn);
  85. bool flag = false;
  86. for(int i=minn;i<=maxn;i++){
  87. if(vis[i]){
  88. flag = true;
  89. break;
  90. }
  91. }
  92. if(flag) printf("Case #%d: Yes\n",t++);
  93. else printf("Case #%d: No\n",t++);
  94. }
  95. return ;
  96. }

hdu 4786(生成树)的更多相关文章

  1. HDU 4786 生成树 并查集+极大极小值 黑白边 确定选择白边的数量

    题意: 给定一个无向图 n 个点 m条无向边 u v val val == 1 表示边(u, v) 为白边 问能否找到n个点的生成树, 使得白边数为斐波那契数 思路: 并查集求图是否连通( 是否存在生 ...

  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 生成树

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

  4. hdu 4786 最小生成树与最大生成树

    /* 题意 :有一些边权值为1和0,判断是否存在一个生成树使得他的总权值为一个斐波那契数. 解法:建立一个最小生成树向里面加权值为1的边替换为0的边,保证原来的联通.因为权值为1,可直接求出最大生成树 ...

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

    题意: N个点,M条边.每条边连接两个点u,v,且有一个权值c,c非零即一. 问能否将N个点形成一个生成树,并且这棵树的边权值和是一个fibonacii数. (fibonacii数=1,2,3,5,8 ...

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

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

  7. HDU 4786 Fibonacci Tree 最小生成树

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

  8. HDU 4786(最小生成树 kruskal)

    题目链接:pid=4786" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=4786 Prob ...

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

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

随机推荐

  1. 第39-43课 thinkphp5完成商品会员价格功能(后置勾子afterInsert)

    目录 功能一:利用后置勾子,处理好商品主键id,会员的价格,再插入member_price表里. 要实现的功能: 思路: html里 控制器里 模型里的后置勾子afterInsert() 功能二:利用 ...

  2. Codeforces Round #459 (Div. 2):B. Radio Station

    B. Radio Station time limit per test2 seconds memory limit per test256 megabytes Problem Dsecription ...

  3. 8、python中的集合

    集合是python中无序.可变的数据结构.集合与字典类似,集合中的元素必须是可哈希的(等同于字典中的键),也就是说集合中的元素是唯一.不可变的数据类型.这里前面说集合可变,后面又说集合中的元素不可变是 ...

  4. A JavaScript Image Gallery

    childNodes property:  The childNodes property is a way of getting information about the children of ...

  5. SpringMvc路径参数和url的两种实现方式

    我们经常采用的SpringMvc路径参数经常的操作是在url后面采用?参数名=值1&参数名2=值2这种方式实现 RequestMapping的作用: 1)当作用在controller时,我们通 ...

  6. Install ADDS on Windows Server 2012 R2 with PowerShell

    Install ADDS on Windows Server 2012 R2 with PowerShell Posted by ethernuno on 20/04/2014 In this tut ...

  7. error C2011: “Picture”:“struct”类型重定义

    今天引用外来库时出现问题,也许是版本问题. 错误如下: .....\oursun\cincludes\quickdraw.h(309): error C2011: “Picture”:“struct” ...

  8. oracle常用关键字和函数

    数据库的增删改查: 增:insert into ... values(); 例:insert into p_emp values(sq_emp.nextval,,sysdate,,null,,); c ...

  9. Leetcode 611.有效三角形的个数

    有效三角形的个数 给定一个包含非负整数的数组,你的任务是统计其中可以组成三角形三条边的三元组个数. 示例 1: 输入: [2,2,3,4] 输出: 3 解释: 有效的组合是: 2,3,4 (使用第一个 ...

  10. 理解点击屏幕的事件响应--->对UIView的hitTest: withEvent: 方法的理解

    要理解这两个方法.先了解一下用户触摸屏幕后的事件传递过程. 当用户点击屏幕后,UIApplication 先响应事件,然后传递给UIWindow.如果window可以响应.就开始遍历window的su ...