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. Spark(二): 内存管理

    Spark 作为一个以擅长内存计算为优势的计算引擎,内存管理方案是其非常重要的模块: Spark的内存可以大体归为两类:execution和storage,前者包括shuffles.joins.sor ...

  2. 安装Android sdk 4.4(19)出现问题的解决方案

    刚更新了Android sdk 19,但是出现以下两个问题,浪费我2个小时的时间,现在将我遇到的问题和解决方法总结如下: 问题1:打开eclipse点更新后,出现This Android SDK re ...

  3. 用HTML5构建一个流程图绘制工具

    在我们的开发工程中经常会使用到各种图,所谓的图就是由节点和节点之间的连接所形成的系统,数学上专门有一个分支叫图论(Graph Theroy).利用图我们可以做很多工具,比如思维导图,流程图,状态机,组 ...

  4. Android的onCreateOptionsMenu()创建菜单Menu详解

    Android一共有三种形式的菜单:            1.选项菜单(optinosMenu)            2.上下文菜单(ContextMenu)            3.子菜单(s ...

  5. [转]PO和VO、关于延迟加载(lazy)和强制加载(Hibernate.initialize(Object proxy) )

    摘自http://www.cnblogs.com/kelin1314/archive/2009/11/13/1602778.html PO和VO PO 即Persistence Object VO 即 ...

  6. 【shell】变量的配置文件

    (1)/etc/profile   登录时,会执行.全局(公有)配置,不管是哪个用户,登录时都会读取该文件.(2)/ect/bashrc   Ubuntu没有此文件,与之对应的是/ect/bash.b ...

  7. Neutron LBaaS Service(1)—— Neutron LBaaS Service基本知识

    在OpenStack Grizzly版本中,Quantum组件引入了一个新的网络服务:LoadBalancer(LBaaS),服务的架构遵从Service Insertion框架.LoadBalanc ...

  8. 关于checkbox的attr无效的问题

    jq用了10版本的,一直发现attr无效,查找良久,由同事帮忙解决该问题,感谢. 特记录下该问题. 由于 新版本attr换成了prop的问题. $("input[name='delIds'] ...

  9. Redis安装创建

    安装 下载,解压和安装: $ wget http://download.redis.io/releases/redis-2.8.17.tar.gz $ tar xzf redis-2.8.17.tar ...

  10. Python之Rpyc模块

    简介 rpyc (Remote Python Call)为分布式计算环境提供了优良的基础平台.使用rpyc编写c/s结构程序,完全不用考虑老式的socket编程,现在只用编写简单的3.5行代码即可完成 ...