Fibonacci Tree

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2952    Accepted Submission(s): 947

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
 
Source

2013 Asia Chengdu Regional Contest

有n个点,m条边。有黑白之分,问连通全部点时的边中,白边的个数能不能是斐波那契数列中的一个数

思路:先用白边连图。求出白边的个数,再先用黑边连图,求出白边另个值,。这两个值就是白边个数的取值范围 

2015,7,30

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define M 100000+10
struct node{
int s,e,val;
}sd[M];
int x[M];
int a[55]; bool cmp1(node a,node b){
return a.val>b.val;
}
bool cmp2(node a,node b){
return a.val<b.val;
}
void init()
{
for(int i=0;i<M;i++)
x[i]=i;
}
int find(int k)
{
if(x[k]==k) return k;
x[k]=find(x[k]);
return x[k];
}
int main()
{
int t,m,n,v=1,i;
int num,start,end; a[1]=1; a[2]=2;
for(i=3;i<55;i++)//由于边最多有100000条,所以斐波那契数大于这个数时就能够了,55足够了
a[i]=a[i-1]+a[i-2]; scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(i=0;i<m;i++)
scanf("%d%d%d",&sd[i].s,&sd[i].e,&sd[i].val); sort(sd,sd+m,cmp2);
num=0; init();
for(i=0;i<m;i++){
int fa=find(sd[i].s);
int fb=find(sd[i].e);
if(fa!=fb){
x[fa]=fb;
if(sd[i].val==1)
num++;
}
}
start=num; sort(sd,sd+m,cmp1);
num=0; init();
for(i=0;i<m;i++){
int fa=find(sd[i].s);
int fb=find(sd[i].e);
if(fa!=fb){
x[fa]=fb;
if(sd[i].val==1)
num++;
}
}
end=num; int ok=0;
for(i=1;i<=n;i++){
if(find(i)!=find(1)){
ok=1;
break;
}
}
printf("Case #%d: ",v++);
if(ok) printf("No\n");//假设没有连通全部点直接输出No
else{
for(i=1;i<50;i++){//注意这个i要从1開始,由于输入1个点0条边时要输出No。。我找了半天错= =+
if(a[i]>= start && a[i]<=end){
ok=1;
break;
}
}
if(ok) printf("Yes\n");
else printf("No\n");
}
}
return 0;
}

hdu 4786 Fibonacci Tree(最小生成树)的更多相关文章

  1. HDU 4786 Fibonacci Tree 最小生成树

    Fibonacci Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4786 Description Coach Pang is intere ...

  2. hdu 4786 Fibonacci Tree (2013ACMICPC 成都站 F)

    http://acm.hdu.edu.cn/showproblem.php?pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) ...

  3. HDU 4786 Fibonacci Tree(生成树,YY乱搞)

    http://acm.hdu.edu.cn/showproblem.php? pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others ...

  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. 推荐一些不错的开源免费易上手的web前端框架

    1. bui 2.Semantic UI 3.oniui

  2. HTTPS.SYS怎样使用HTTPS

    HTTPS.SYS怎样使用HTTPS 参考了MORMOT的官方文档:http://blog.synopse.info/post/2013/09/04/HTTPS-communication-in-mO ...

  3. 【spring data jpa】 spring data jpa 中 时间格式设置between and 查询

    实例代码: //举报时间 Date createDate = entity.getCreateDate(); if (createDate != null){ predicates.add(cb.be ...

  4. hibernate CascadeType属性

    CascadeType.PERSIST 只有A类新增时,会级联B对象新增.若B对象在数据库存(跟新)在则抛异常(让B变为持久态) : 级联保存,当调用了Persist() 方法,会级联保存相应的数据 ...

  5. 2015年Ubuntu最新Redmine的安装和配置

    近期须要在公司内部搭建一个项目管理平台Redmine,在摸索了一天之后.最终配置成功,在这里分享给大家. 公司server的系统是Ubuntu14.04,要安装的是最新的Redmine3.0. 因为R ...

  6. LVM分区管理和扩展

    一.LVM简介 LVM是 Logical Volume Manager(逻辑卷管理)的简写.LVM将一个或多个硬盘的分区在逻辑上集合,相当于一个大硬盘来使用,当硬盘的空间不够使用的时候,可以继续将其它 ...

  7. 阿里云RDS(云数据库)之产品简介

    参考阿里产品文档:https://docs.aliyun.com/?spm=5176.100054.3.1.ywnrMX#/pub/rds/product-introduce/overview& ...

  8. 【angularJS】前后台分离,angularJS使用Token认证

    参考资料: [AngularJS系列(4)] 那伤不起的provider们啊~ (Provider, Value, Constant, Service, Factory, Decorator):htt ...

  9. 使用hadoop平台进行小型网站日志分析

    0.上传日志文件到linux中,通过flume将文件收集到hdfs中. 执行命令/home/cloud/flume/bin/flume-ng agent -n a4 -c conf -f /home/ ...

  10. nginx出现的403错误

    参考这篇文章: http://www.server110.com/nginx/201309/1792.html 我是这样解决的: 为了保证文件能正确执行,nginx既需要文件的读权限,又需要文件所有父 ...