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. mysql数据损坏修复方法

    1.myisamchk使用 myisamchk 必须暂时停止 MySQL 服务器.例如,我们要检修 discuz 数据库.执行以下操作:# service mysql stop (停止 MySQL ) ...

  2. QT 小票打印

    原地址: http://www.cppblog.com/biao/archive/2009/09/08/95603.html QString htmlString = QString("&l ...

  3. haproxy redirect prefix

    acl short_domain hdr(Host) -i etiantian.org redirect prefix http://www.etiantian.org code 301 if sho ...

  4. Android 绘图工具库AChartEngine

    From: http://www.oschina.net/p/achartengine AChartEngine是为android应用而设计的绘图工具库.目前该库的最新稳定版本是0.7,支持绘制以下类 ...

  5. ubuntu登陆后一闪回到登陆界面

    ubuntu登陆后一闪回到登陆界面   最后发现居然是我的环境变量配置问题........   解决方法:   先CTRL+ALT+F1   root进去, 查看nickleo用户为什么登录失败 ca ...

  6. 变形课(dfs)

    变形课 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submissi ...

  7. Android UI 使用HTML布局(直接打开server网页)

    非常多时候我们用HTML布局会更方便直接,记录一下. 我如今主要是直接调用server的网页(实际上是jsp的,仅仅是返回的是html).所以须要联网,第一步加入权限. <uses-permis ...

  8. HTML注释简介

    HTML注释简介   在编写HTML代码时,我们经常要在一些关键代码旁做一下注释,这样做的好处很多,比如:方便理解.方便查找或方便项目组里的其它程序员了解你的代码,而且可以方便以后你对自己代码进行修改 ...

  9. windows下使用vnc viewer远程连接Linux桌面(转)

    在windows下使用vnc viewer远程连接Linux桌面,主要配置步骤: Linux: 1.rpm -qa vnc //查看是否安装vnc服务,如果没有安装,可以使用yum,或者rpm进行安装 ...

  10. spring的官方文真不错

    引文不太好,但是通过别的版本的查看,发现spring的文档真心不错,内容详细明了. http://docs.spring.io/spring/docs/3.2.5.RELEASE/spring-fra ...