HDU 4786 Fibonacci Tree 最小生成树
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 最小生成树的更多相关文章
- 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 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 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) P ...
- 【HDU 4786 Fibonacci Tree】最小生成树
一个由n个顶点m条边(可能有重边)构成的无向图(可能不连通),每条边的权值不是0就是1. 给出n.m和每条边的权值,问是否存在生成树,其边权值和为fibonacci数集合{1,2,3,5,8...}中 ...
- HDU 4786 Fibonacci Tree (2013成都1006题) 最小生成树+斐波那契
题意:问生成树里能不能有符合菲波那切数的白边数量 思路:白边 黑边各优先排序求最小生成树,并统计白边在两种情况下数目,最后判断这个区间就可以.注意最初不连通就不行. #include <stdi ...
- hdu 4786 Fibonacci Tree 乱搞 智商题目 最小生成树
首先计算图的联通情况,如果图本身不联通一定不会出现生成树,输出"NO",之后清空,加白边,看最多能加多少条,清空,加黑边,看能加多少条,即可得白边的最大值与最小值,之后判断Fibo ...
- HDU 4786 Fibonacci Tree 生成树
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4786 题意:有N个节点(1 <= N <= 10^5),M条边(0 <= M <= ...
随机推荐
- Lambda表达式树解析(下)
概述 前面章节,总结了Lambda树的构建,那么怎么解析Lambda表达式树那?Lambda表达式是一种委托构造而成,如果能够清晰的解析Lambda表达式树,那么就能够理解Lambda表达式要传递的正 ...
- Android抓包方法(转)
Android抓包方法(转) 作者:Findyou 地址:http://www.cnblogs.com/findyou/p/3491014.html 前言: 做前端测试,基本要求会抓包,会分析请求数据 ...
- JMeter监控Slave机器是否执行
netstat -anp | grep 192.168.1.161 | grep 19091 | wc -l http://www.linuxidc.com/Linux/2014-09/106497. ...
- net core体系-3再次认识net core
1 什么是ASP.NET Core ASP.NET Core 是一个全新的开源.跨平台框架,可以用它来构建基于网络连接的现代云应用程序,比如:Web 应用,IoT(Internet Of Things ...
- ansible基础命令实例
参考:https://www.cnblogs.com/ilurker/p/6421624.html 1. 使用自定义的hosts 格式: ansible 组机匹配 -i 自定义的hosts - ...
- phpMyAdmin 安装教程全攻略
管理MYSQL数据库的最好工具是PHPmyAdmin,现在最新版本是phpMyAdmin 2.9.0.2,这是一个国际上开源的软件,一直在更新版本,你可以从 http://www.phpmyadmin ...
- 062 SparkStream内部原理
1.DStream 内部是一系列的RDD组成的,每个RDD与RDD的产生时间形成一个pair保存在内存中(下面有) RDD包含了对应时间段的所有block数据. 2.DStream下的方法 /** T ...
- Python中type和object
type 所有类是type生成的 a = 1 b = "abc" print("type a:{}".format(type(a))) print(" ...
- 页面滚动到指定class样式位置
var winTop =$(window).scrollTop(); var top = document.getElementsByClassName('is-danger')[0].getBoun ...
- Kafka命令行操作及常用API
一.Kafka命令行操作 1.查看当前集群已存在的主题 bin/kafka-topic.sh --zookeeper hd09-01:2181 --list 2.创建主题 bin/kafka-topi ...