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 <= ...
随机推荐
- CentOS下配置SFTP操作日志
1.修改ssh的配置 vi /etc/ssh/sshd_config 在36行左右修改如下配置 Subsystem sftp /usr/libexec/openssh/sftp-server -l I ...
- linux改权限
改变文件夹本身权限,不改动子文件(夹) chmod 600 my/ 改变文件夹及子目录下所有文件(夹)权限 chmod -R 777 my/ 统一修改 cd my 修改文件夹权限为755 find - ...
- redcontrol for SL 中文化及样式选择
app.xaml.cs public partial class App: Application { public App() { //指定t ...
- net core体系-Xamarin-1概要
大家在开发领域都知道Java是跨平台的,但是很多人认为.NET是只能在Windows下运行,不具有跨平台的特性,这种说法其实是不妥的. .NET其实在设计之初也是考虑像Java一样跨平台的,我们知道. ...
- spark学习之路1--用IDEA编写第一个基于java的程序打包,放standalone集群,client和cluster模式上运行
1,首先确保hadoop和spark已经运行.(如果是基于yarn,hdfs的需要启动hadoop,否则hadoop不需要启动). 2.打开idea,创建maven工程.编辑pom.xml文件.增加d ...
- HTML中鼠标滚轮事件onmousewheel
IE/Opera属于同一类型,使用attachEvent即可添加滚轮事件. /*IE注册事件*/ if(document.attachEvent){ document.attachEvent('onm ...
- 2018牛客网暑假ACM多校训练赛(第十场)D Rikka with Prefix Sum 组合数学
原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round10-D.html 题目传送门 - https://www.n ...
- MySQL高级01
MySQL支持大型数据库,支持5000万条记录的数据仓库,32位系统表文件最大可支持4GB,64位系统支持最大的表文件为8TB. 官网下载地址:http://dev.mysql.com/downloa ...
- 工具Maven
Maven 1.使用Maven后每个jar包只在本地仓库中保存一份,需要jar包的工程只需要维护一个文本形式的jar包的引用——我们称之为“坐标”. 2.Maven就可以替我们自动的将当前jar包所依 ...
- spring mvc中的service和controller中读取不到properties值
根据web.xml读取配置文件中的顺序来看 controller层和service层来自于spring mvc.xml中读取,所以必须要在spring mvc.xml中配置读取资源文件夹方式