HDU 4786 Fibonacci Tree
Fibonacci Tree
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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, ... )
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, 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.
#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的更多相关文章
- hdu 4786 Fibonacci Tree (2013ACMICPC 成都站 F)
http://acm.hdu.edu.cn/showproblem.php?pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) ...
- HDU 4786 Fibonacci Tree(生成树,YY乱搞)
http://acm.hdu.edu.cn/showproblem.php? pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others ...
- HDU 4786 Fibonacci Tree 最小生成树
Fibonacci Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4786 Description Coach Pang is intere ...
- HDU 4786 Fibonacci Tree (2013成都1006题)
Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 4786 Fibonacci Tree(最小生成树)
Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 【HDU 4786 Fibonacci Tree】最小生成树
一个由n个顶点m条边(可能有重边)构成的无向图(可能不连通),每条边的权值不是0就是1. 给出n.m和每条边的权值,问是否存在生成树,其边权值和为fibonacci数集合{1,2,3,5,8...}中 ...
- HDU 4786 Fibonacci Tree 生成树
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4786 题意:有N个节点(1 <= N <= 10^5),M条边(0 <= M <= ...
- hdu 4786 Fibonacci Tree 乱搞 智商题目 最小生成树
首先计算图的联通情况,如果图本身不联通一定不会出现生成树,输出"NO",之后清空,加白边,看最多能加多少条,清空,加黑边,看能加多少条,即可得白边的最大值与最小值,之后判断Fibo ...
- HDU 4786 Fibonacci Tree (2013成都1006题) 最小生成树+斐波那契
题意:问生成树里能不能有符合菲波那切数的白边数量 思路:白边 黑边各优先排序求最小生成树,并统计白边在两种情况下数目,最后判断这个区间就可以.注意最初不连通就不行. #include <stdi ...
随机推荐
- js cookie操作
//写Cookie function writeCookie(name, value) { var expire = new Date(); expire.setFullYear(expire.get ...
- 在WINDOWS SERVER 上或远程桌面中使用 MUTEX
引用: http://www.cnblogs.com/fg0711/archive/2012/05/03/2480502.html 使用Mutex需要注意的两个细节 可能你已经注意到了,例子中在给Mu ...
- HackerRank "Morgan and a String"
I saw the same sub-problem in LeetCode, and there exists a O(n) neat greedy solution: for _ in range ...
- PHPnow升级PHP 5.4与Mysql 5.5
本文转载自:https://www.dadclab.com/archives/5928.jiecao 折腾开始 1.安装一下VC9的运行库,下载地址:https://www.microsoft.com ...
- Hadoop:使用原生python编写MapReduce
功能实现 功能:统计文本文件中所有单词出现的频率功能. 下面是要统计的文本文件 [/root/hadooptest/input.txt] foo foo quux labs foo bar quux ...
- Linux下nl命令的用法详解
Linux中nl命令和cat命令很像,不过nl命令会打上行号,属于比较不常用的命令,下面随小编一起来了解下这个鲜为人知的nl命令吧. nl命令在linux系统中用来计算文件中行号.nl 可以将输出的文 ...
- linux内核模块相关命令:lsmod,depmod,modprobe,modinfo,insmod,rmmod 使用说明
加载内核驱动的通常流程: 1.先将.ko文件拷贝到/lib/module/`uname -r`(内核版本号)/kernel/driver/...目录下, 根据具体用途的区别分为net.ide.scsi ...
- TortoiseSVN 同步分支
对比工具可以使用 winmerge 和 beyond compare ,winmerge免费小巧,beyond compare功能更强大.这两款工具都比TortoiseSVN自带的对比工具要好一些. ...
- 深度优化LNMP
优化前准备工作 Centos准备及配置 准备安装包及软件:http://pan.baidu.com/s/1chHQF 下载解压到U盘即可安装http://pan.baidu.com/s/15TUWf ...
- 黄聪:wordpress如何获取当前页面的URL
一行代码搞定 <? echo home_url( add_query_arg( array() ) ); ?>