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. Python中的图形库

    Python中的图形库 根据Python 2.x的官网文档的解释: Graphical User Interfaces with Tk 和 Other Graphical User Interface ...

  2. libevent evbuffer bug

    今天发现 libevent 2.0.22 一个坑爹的bug,导致消息混乱.查找问题浪费一天,复现代码如下 #include <event2/buffer.h> #include <s ...

  3. C Statements

    1,while((ch = getchar()) != EOF){    putchar(ch);}2,while((ch=getchar()) != EOF){    if(ch < '0' ...

  4. ALLEN-XIE

    ALLEN-XIE ABOUT   Allen Xie是一家坚持理念至上的西装定制店.我们的价值观渗透于我们所做的每一件事中,从而确保始终遵循自己的风格.我们坚持用最高标准要求自己,因此,在制衣过程中 ...

  5. 借贷宝推广得现金是真的_注册就送人民币20元_邀请码CRJYQTK

    动动手指,20元立即到手.即优步uber打车和滴滴专车豪投数亿元争夺专车市场之后,一款名为借贷宝的APP推广在网上流传开来,目前主要看重的就是它的推广力度,豪投20亿让大众来推广.简单流程:下载借贷宝 ...

  6. 网易云课堂_C++开发入门到精通_章节2:引用和函数的高级用法

    课时6函数重载 函数重载 在C语言头文件中的extern "C" //常见的C语言头文件格式 #ifndef _FUNC_ #define _FUNC_ #ifdef __cplu ...

  7. Lucene 4.4 依据Int类型字段删除索引

    1.方法一,通过Term删除 Term构造中没有.Int类型须要转换成Lucene自带的类BytesRef . /** * 依据商品ID删除索引文件 * @param id */ public voi ...

  8. 设置TextView水平居中显示

    1.让TextView里的内容水平居中 android:gravity="center_horizontal" 2.让TextView控件在它的父布局里水平居中 android:l ...

  9. js 将网页内容生成图片

    $(function () { $("#saveimg_btn").on("click",function (event) { event.preventDef ...

  10. DevExpress GridView使用技巧之列标题点击事件

    在这里使用GridView的MouseDown事件.这里同样使用的是GridHitInfo来获取点击位置的信息,来判断是否在列标题上.GridHitInfo根据鼠标点击的x.y坐标获取该点的相关信息, ...