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 ...
随机推荐
- jQuery制作视频网站的展示效果
效果:如图所示,用户可以单击左上角的左右箭头,来控制视频展示的左右滚动. 当单击向右箭头时下面的展示视频会向左滚动隐藏,同时新的视频展示会以滚动方式显示出来.向左同理. css: ;; word-br ...
- Python类,域,方法,对象,继承
类和对象: 是面向对象编程的两个主要方面,类创建一个新类型,而对象这个类的实例.. 域: 属于一个对象或类的变量被称为域.域有两种类型: 属于每个实例(类的对象)或属于类本身.它们分别被称为实例变量和 ...
- golang的验证码相关的库
识别库 https://github.com/goghcrow/capture_easy 生成验证码的库 https://github.com/hanguofeng/gocaptcha 生成图片水印 ...
- makefile学习笔记(多目录嵌套调用、变量使用)
http://blog.csdn.net/leexiang_han/article/details/9274229 学习了几天的makefile的嵌套调用编写也有一些心得,先声明,我也是初学者写文 ...
- System.Data.SqlClient.SqlException.Number的所有错误值列表
在系统数据库(master或msdb或model)的架构(sys)的视图(messages)中: SELECT [message_id] ,[language_id] ,[seve ...
- TortoiseSVN 同步分支
对比工具可以使用 winmerge 和 beyond compare ,winmerge免费小巧,beyond compare功能更强大.这两款工具都比TortoiseSVN自带的对比工具要好一些. ...
- 71. Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- 【MySQL】性能优化 之 延迟关联
[背景] 某业务数据库load 报警异常,cpu usr 达到30-40 ,居高不下.使用工具查看数据库正在执行的sql ,排在前面的大部分是: SELECT id, cu_id, name, in ...
- Spark参数配置说明
1 修改$SPARK_HOME/conf目录下的spark-defaults.conf文件 添加以下配置项 spark.sql.hive.convertMetastoreParquet ...
- HDU 1213 How Many Tables(并查集,简单)
题解:1 2,2 3,4 5,是朋友,所以可以坐一起,求最小的桌子数,那就是2个,因为1 2 3坐一桌,4 5坐一桌.简单的并查集应用,但注意题意是从1到n的,所以要减1. 代码: #include ...