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. YUI的模块化开发

    随着互联网应用越来越重,js代码越来越庞大,如何有效的去组织自己的代码,变得非常重要.我们应该学会去控制自己的代码,而不是到最后一堆bug完全不知道从哪冒出来.前端的模块化开发可以帮助我们有效的去管理 ...

  2. 工作无聊,闲来无事,自己学习 android入门

    工作无聊,闲来无事,自己学习. 最近几天看了看有关android的UI设计,布局以及android有关控件的知识,算是进一步了解了 android的相关内容. 明天就是周末了,明天及后天,我打算开始学 ...

  3. eclipse 软件的背景颜色、字体设置

    1.eclipse 背景色设置: Window->Preferences->General->Editors->Text Editors->Backgroud color ...

  4. qt 4.6.2 vs 2005 + QCreator 开发环境配置(有注册码)

    配置开发环境可真是个痛苦的过程,网上的资料参差不齐,只有自己一步步来试验一下了 本人环境 virtualbox +  xp +  vs 2005  en 1.安装vs 2005 en sp1 下载vs ...

  5. ID3算法 决策树 C++实现

    人工智能课的实验. 数据结构:多叉树 这个实验我写了好久,开始的时候从数据的读入和表示入手,写到递归建树的部分时遇到了瓶颈,更新样例集和属性集的办法过于繁琐: 于是参考网上的代码后重新写,建立决策树类 ...

  6. OpenStack cloudCompute glassary术语project,tenant,user

    1,tenantA group of users, used to isolate access to Compute resources(一组用户,用于隔离访问计算资源). An alternati ...

  7. hdu 5621 KK's Point(数学,推理题)

    题解: 在圆上点三个点时,除圆上三个交点外,圆内没有交点:在圆上点四个点时,除圆上四个交点外,圆内出现了一个交点,因此,在N个点中每四个点便可以在圆内产生一个交点,因此N个点在圆内形成的点的个数为CN ...

  8. Unity 利用NGUI2.6.3做技能冷却的CD效果

    NGUI非常强大我们今天来学习一下,如何利用NGUI做技能冷却的CD效果.先导入NGUI的插件.没有的话这里有啊NGUI2.6.3下载地址: http://vdisk.weibo.com/s/KLqn ...

  9. JS功能代码集锦

    只作 说明 逻辑用 1.模仿fade in(),fade out(). 原理:setInterval ( "opacity++透明度“函数,时间间隔) var alpha = 0; func ...

  10. 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 ...