Fibonacci Tree

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=4786

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

Hint

题意

给你一个由白边和黑边组成的图,问你能不能找到一个生成树,使得白边的个数是Fibonacci数

题解:

考虑白边最多情况的生成树时候白边数量为Max,最少的时候为Min

那么[Min,Max]这个区间内的白边数量都可以取到

所以求出Min和Max即可

坑点:图不联通

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+7;
struct node{
int x,y,z;
}p[maxn];
bool cmp1(node A,node B){
return A.z<B.z;
}
bool cmp2(node A,node B){
return A.z>B.z;
}
int fa[maxn];
int fi(int x){
return x==fa[x]?x:fa[x]=fi(fa[x]);
}
void uni(int x,int y){
x=fi(x),y=fi(y);
if(x==y)return;
else fa[x]=fa[y];
}
void solve(int Cas){
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
fa[i]=i;
for(int i=1;i<=m;i++)
scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].z);
sort(p+1,p+1+m,cmp1);
int x1=0,x2=0;
int D = 0;
for(int i=1;i<=m;i++){
if(fi(p[i].x)!=fi(p[i].y)){
x1+=p[i].z;
uni(p[i].x,p[i].y);
D = D + 1;
}
}
if(D!=n-1){
printf("Case #%d: No\n",Cas);
return;
}
for(int i=1;i<=n;i++)
fa[i]=i;
sort(p+1,p+1+m,cmp2);
for(int i=1;i<=m;i++){
if(fi(p[i].x)!=fi(p[i].y)){
x2+=p[i].z;
uni(p[i].x,p[i].y);
}
}
long long a=1,b=1;
int flag = 0;
while(a<=x2||b<=x2){
if(a>=x1&&a<=x2)
flag=1;
if(b>=x1&&b<=x2)
flag=1;
a=a+b;
if(a>b)swap(a,b);
}
if(a>=x1&&a<=x2)
flag=1;
if(b>=x1&&b<=x2)
flag=1;
if(flag)printf("Case #%d: Yes\n",Cas);
else printf("Case #%d: No\n",Cas);
}
int main(){
int t;
scanf("%d",&t);
for(int cas=1;cas<=t;cas++)solve(cas);
}

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 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  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) P ...

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

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

  7. HDU 4786 Fibonacci Tree (2013成都1006题) 最小生成树+斐波那契

    题意:问生成树里能不能有符合菲波那切数的白边数量 思路:白边 黑边各优先排序求最小生成树,并统计白边在两种情况下数目,最后判断这个区间就可以.注意最初不连通就不行. #include <stdi ...

  8. hdu 4786 Fibonacci Tree 乱搞 智商题目 最小生成树

    首先计算图的联通情况,如果图本身不联通一定不会出现生成树,输出"NO",之后清空,加白边,看最多能加多少条,清空,加黑边,看能加多少条,即可得白边的最大值与最小值,之后判断Fibo ...

  9. HDU 4786 Fibonacci Tree 生成树

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

随机推荐

  1. CentOS挂Windows的NFS备忘

    Windows NFS 安装和配置  注:需要将名称为“所有计算机”的访问类型改为“无访问权限”,再将可访问IP的访问类型改为“读写”,并勾选“允许根目录访问” ,如WINDOWS有防火墙开放“204 ...

  2. @transactional注解下失效

    这几天在项目里面发现我使用@Transactional注解事务之后,抛了异常居然不回滚.后来终于找到了原因. 如果你也出现了这种情况,可以从下面开始排查. 一.特性 先来了解一下@Transactio ...

  3. element-ui MessageBox的bug

    通过 use引用messageBox有bug Vue.use(MessageBox) 页面一开始会有一个弹窗,内容空白 Vue.component(MessageBox.name, MessageBo ...

  4. react native初始化项目

    打开命令行窗口,进入我们想要创建项目的父目录,输入命令: npm install -g yarn react-native-cli react-native init 项目名 进入新建的项目目录,执行 ...

  5. Introduction to boundary integral equations in BEM

    Boundary element method (BEM) is an effective tool compared to finite element method (FEM) for resol ...

  6. 分布式一致性算法——paxos

    一.什么是paxos算法 Paxos 算法是分布式一致性算法用来解决一个分布式系统如何就某个值(决议)达成一致的问题. 人们在理解paxos算法是会遇到一些困境,那么接下来,我们带着以下几个问题来学习 ...

  7. ELK使用3-Logstash

    一.命令行输入输出操作 1.命令行输出: /application/elk/logstash/bin/logstash -e 'input { stdin{} } output { stdout{} ...

  8. JDK生成证书:

    JDK生成证书: 在此之前确保本地已安装JDK1.6 1.生成客户端的私钥,客户端的证书 1)keytool -genkey -alias clientkey -keystore kclient.ke ...

  9. unicode utf-8 ascll编码比较

    1.字符编码     因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit)作为一个字节(byte), 所以,一个字节能表示的最大的整数就 ...

  10. ERROR 1215 (HY000): Cannot add foreign key constraint

    MySQL中在为一个varchar类型数据列添加外键时,会发生上面所示的错误,这里我google了一下,感觉它们碰到的问题跟我这个说的有点不相干,尝试了多种方式后来才发现是:主表(table1)所对应 ...