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. [原]Linux系统管理使用技巧总结

    一.磁盘管理 1.查看磁盘空间大小 df可以查看一级文件夹大小.使用比例.档案系统及其挂入点,但对文件却无能为力. du可以查看文件及文件夹的大小.如果不带其他参数(-h表示human-readabl ...

  2. golang一个深复制的库

    https://github.com/mitchellh/copystructure

  3. 报错:Caused by: java.io.FileNotFoundException: d:\youTemprepository\upload_77faffc1_1580a9240ca__8000_00000001.tmp (系统找不到指定的路径。)

    org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-dat ...

  4. -all_load,-ObjC,-force_load三者的区别

    参考:https://developer.apple.com/library/mac/qa/qa1490/_index.html,http://www.cnblogs.com/YouXianMing/ ...

  5. 黄聪:WordPress图片插件:Auto Highslide修改版(转)

    一直以来很多人都很喜欢我博客使用的图片插件,因为我用的跟原版是有些不同的,效果比原版的要好,他有白色遮罩层,可以直观的知道上下翻图片和幻灯片放映模式.很多人使用原版之后发现我用的更加帅一些,于是很多人 ...

  6. 8. redis的主从复制和sentinal

    一. redis主从复制(读写分离) redis的主从复制分为两类节点:1个master和多个slave,master进行读写操作,slav进行只读操作 启动步骤: 主节点照常启动,slave节点启动 ...

  7. DBA_Oracle Erp中某个Form需进行升级Patch详解(案例)

    2014-06-21 Created By BaoXinjian

  8. 更改EGit的user settings中默认的location

    在系统的环境变量中添加变量HOME,值为C:\Users\Kane.Sun\ 记得要讲users改为首字母大写,不然可能会有问题.

  9. Android之BroadcastReceiver 监听系统广播

    绑定广播有两种方式 一.配置文件绑定,在程序未启动也能监听 二.代码方式绑定,在程序启动后才能监听 1.绑定和取消绑定广播 public class MainActivity extends Acti ...

  10. http示例代码

    //下载文件 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletEx ...