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. Facebook的React Native之所以能打败谷歌的原因有7个(ReactNative vs Flutter)

    https://baijiahao.baidu.com/s?id=1611028483072699113&wfr=spider&for=pc 如果你喜欢用(或希望能够用)模板搭建应用, ...

  2. [转] css选择器中:first-child与:first-of-type的区别

    :first-child选择器是css2中定义的选择器,从字面意思上来看也很好理解,就是第一个子元素.比如有段代码: p:first-child  匹配到的是p元素,因为p元素是div的第一个子元素: ...

  3. 移动端根据不同DPR加载大小不同的图片

    1.首先创建mixin.styl文件代码如下: bg-image($url) // 创建bg-image($url)函数 background-image: url($url + "@2x. ...

  4. UIActionSheet的常用方法

    enum UIActionSheetStyle : Int { case Automatic // take appearance from toolbar style otherwise uses ...

  5. 基于jquery的页面分屏切换模板

    闲来无事,搞了个页面的分屏效果,先来看下效果: 出于可自定义宽高的目的,屏幕分块由CSS控制,由js控制估计等分模块效果一般. 程序相关说明: HTML结构: <div class=" ...

  6. 3. 深入研究 UCenter API 之 加密与解密(转载)

    1.  深入研究 UCenter API 之 开篇 (转载) 2.  深入研究 UCenter API 之 通讯原理(转载) 3.  深入研究 UCenter API 之 加密与解密(转载) 4.  ...

  7. Failed to create Accelerated Display. Please check the display hardware and drivers meet the minimum requirements.

    ArcGIS Runtime for WPF开发中Map设置了属性UseAcceleratedDisplay="True",报错: Sample: LocalMap Error: ...

  8. Flink--DateSet的Transformation简单操作

    flatMap函数 //初始化执行环境 val env: ExecutionEnvironment = ExecutionEnvironment.getExecutionEnvironment //加 ...

  9. net core体系-web应用程序-3项目结构、配置文件详解

    一.应用程序文件结构 如下图所示,相比于Asp.Net项目,在新建的Asp.Net Core项目中,没有了Global.asax以及Web.config这样的文件,但多了几个其他主要的文件,它们分别为 ...

  10. sql重点题

    --https://blog.csdn.net/weixin_39718665/article/details/78161013/*1.用系统管理员登陆,我这里用户名是system,密码是manage ...