Fibonacci Tree

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3006    Accepted Submission(s): 966

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
 给了一个无向图..每个边要么是白的.要么是黑的..问能否构造一个生成树..让白边在生成树的个数为fibonacci数...
题解:
这个题就是求一遍最小的生成树,

求一遍最大的生成树
两个中间是不是有斐波那契数
最后还有判一下联通;
kruskal;
代码:
 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int MAXN=;
struct Node {
int s,e,c;
};
Node dt[MAXN];
int pre[MAXN];
int M,t1,N;
int cmp1(Node a,Node b){
return a.c<b.c;
}
int cmp2(Node a,Node b){
return a.c>b.c;
}
/*int cmp1(const void *a,const void *b){
if((*(Node *)a).c<(*(Node *)b).c)return -1;
else return 1;
}
int cmp2(const void *a,const void *b){
if((*(Node *)a).c>(*(Node *)b).c)return -1;
else return 1;
}*/
int find(int x){
return pre[x]= x==pre[x]?x:find(pre[x]);
}
bool merge(Node a){
if(!pre[a.s])pre[a.s]=a.s;
if(!pre[a.e])pre[a.e]=a.e;
int f1,f2;
f1=find(a.s);f2=find(a.e);
if(f1!=f2){
pre[f1]=f2;
t1++;
if(a.c)return true;
}
return false;
}
int kruskal(){int tot=;
t1=;
for(int i=;i<M;i++){
if(merge(dt[i]))tot++;
}
if(t1==N)return tot;
else return -;
}
bool fp[MAXN];
void gf(){
int a,b,c=;
memset(fp,false,sizeof(fp));
a=;b=;
fp[a]=fp[b]=true;
while(c<MAXN){
c=a+b;
fp[c]=true;
a=b;
b=c;
}
}
int main(){
int T,s1,s2,ans,flot=;
scanf("%d",&T);
while(T--){
flot++;
memset(pre,,sizeof(pre));
scanf("%d%d",&N,&M);
for(int i=;i<M;i++){
scanf("%d%d%d",&dt[i].s,&dt[i].e,&dt[i].c);
}
// qsort(dt,M,sizeof(dt[0]),cmp1);
sort(dt,dt+M,cmp1);
s1=kruskal();
//qsort(dt,M,sizeof(dt[0]),cmp2);
sort(dt,dt+M,cmp2);
memset(pre,,sizeof(pre));
s2=kruskal();
//printf("%d %d\n",s1,s2);
gf();
ans=;
if(s1<||s2<){
printf("Case #%d: No\n",flot);
continue;
}
//for(int i=0;i<100;i++)printf("fp[%d]=%d ",i,fp[i]);puts("");
if(s1>s2){
int q=s1;
s1=s2;
s2=q;
}
for(int i=s1;i<=s2;i++){
if(fp[i])ans=;
}
if(ans)printf("Case #%d: Yes\n",flot);
else printf("Case #%d: No\n",flot);
}
return ;
}

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

  1. HDU 4786 Fibonacci Tree 最小生成树

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

  2. hdu4786 Fibonacci Tree[最小生成树]【结论题】

    一道结论题:如果最小生成树和最大生成树之间存在fib数,成立.不存在或者不连通则不成立.由于是01图,所以这个区间内的任何生成树都存在. 证明:数学归纳?如果一棵树没有办法再用非树边0边替代1边了,那 ...

  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. hdoj 4786 Fibonacci Tree【并查集+最小生成树(kruskal算法)】

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

  6. HDU 4786 Fibonacci Tree

    Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) P ...

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

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

  8. HDU 4786 Fibonacci Tree (2013成都1006题)

    Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  9. POJ 4786 Fibonacci Tree

    Fibonacci Tree Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ...

随机推荐

  1. 抛弃QP

        随着软件的慢慢进行发现QP根本无法建立多个实例,也就是无法在多个任务中同时使用QP的事件回调 架构,这点同libevent不同,最终决定放弃之,乖乖的用freeRTOS多任务方案,workin ...

  2. 我所不知道的 Chrome 开发者工具

    http://www.oschina.net/translate/things-i-didnt-know-about-chrome-devtools 自打我开始进行Web开发后,我就一直将Firebu ...

  3. 站在巨人的肩膀上,C++开源库大全

    程序员要站在巨人的肩膀上,C++拥有丰富的开源库,这里包括:标准库.Web应用框架.人工智能.数据库.图片处理.机器学习.日志.代码分析等. 标准库 C++ Standard Library:是一系列 ...

  4. 优秀的目录文档内容查找,替换工具,可以飞快的帮助你查询大IIS日志哟。

    这,是一款飞速的目录文档中内容查找的工具. 它,飞快精准的帮助你查询到你想搜索的文档中的内容. 它,是一款由非常牛B,我都不晓得姓名的作者开发的,冒失是C++的windows应用. 你,非常需要他. ...

  5. 查看linux/AIX系统内存及CPU占用百分比

    1.linux下查看CPU及内存占用情况 查看内存占用百分比: [root@rusky ~]# free -m | sed -n '2p' | awk '{print "used mem i ...

  6. Import MySQL Dumpfile, SQL Datafile Into My Database

    How can I import a MySQL dumpfile into my database? I'm using CentOS Linux 5 server. My old hosting ...

  7. SqlServer2008(R2) 数据库使用外网IP实例连接服务器

    1.打开sql2008,使用windows身份登录 2.登录后,右键选择"属性".左侧选择"安全性",选中右侧的"SQL Server 和 Windo ...

  8. HasMap

    您还未登录 ! 登录 注册 论坛首页 → Java企业应用论坛 → 深入理解HashMap 全部 Hibernate Spring Struts iBATIS 企业应用 Lucene SOA Java ...

  9. 解决VS2010批量替换时经常由于内存较低而导致VS2010自动关闭的问题

    尊重原著作:本文转载自http://www.cnblogs.com/Sharping/p/3165527.html 情况描述 在使用VS2010 开发Web应用程序的时候,批量替换时经常卡死关闭. 一 ...

  10. OleDbHelper

    using System; using System.Collections.Generic; using System.Text; using System.Data; using System.D ...