Fibonacci Tree

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

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
题意:两条边之间1代表是白边,0代表是黑边,求是否存在一棵最小树使它的边中有Fibonacci 数列( 1, 2, 3, 5, 8, ... )中
        数条白边(最小树中边可有白边可有黑边)
 
题解:利用打表将Fibonacci 数列存在数组fib[]中先将边按照由白到黑排序求出生成一棵最小树最多需要白边多少条max;再将边按
         照有黑到白排序求出生成一棵最小树最少需要白边多少条min,如果存在Fibonacci 数列中一个数使min<=fib[i]<=max则输
         出yes否则输出no(如果无法生成一棵树也输出no)
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAX 100010
using namespace std;
struct recode
{
int beg;
int end;
int bian;
}s[MAX];
bool cmp1(recode a,recode b)
{
return a.bian>b.bian;
}
bool cmp2(recode a,recode b)
{
return a.bian<b.bian;
}
int set[MAX];
int fib[MAX];
void biao()
{
int i,j;
fib[1]=1;
fib[2]=2;
for(i=3;fib[i]<MAX;i++)
{
fib[i]=fib[i-1]+fib[i-2];
}
}
int find(int fa)
{
int t;
int ch=fa;
while(fa!=set[fa])
fa=set[fa];
while(ch!=fa)
{
t=set[ch];
set[ch]=fa;
ch=t;
}
return fa;
}
void mix(int x,int y)
{
int fx,fy;
fx=find(x);
fy=find(y);
if(fx!=fy)
set[fx]=fy;
}
int main()
{
int n,m,j,i,t;
scanf("%d",&t);
int k=1;
biao();
while(t--)
{
scanf("%d%d",&n,&m);
int sum=0;
for(i=0;i<m;i++)
scanf("%d%d%d",&s[i].beg,&s[i].end,&s[i].bian);
int min=0,max=0;
for(i=0;i<=n;i++)
set[i]=i;
sort(s,s+m,cmp1);
for(i=0;i<m;i++)
{
//printf("%d %d # ",s[i].beg,s[i].end);
if(find(s[i].beg)!=find(s[i].end))
{
mix(s[i].beg,s[i].end);
if(s[i].bian==1)
max++;
}
}
// printf("\n");
// printf("%d \n",max);
for(i=0;i<=n;i++)
set[i]=i;
sort(s,s+m,cmp2);
for(i=0;i<m;i++)
{
// printf("%d %d # ",s[i].beg,s[i].end);
if(find(s[i].beg)!=find(s[i].end))
{
mix(s[i].beg,s[i].end);
if(s[i].bian==1)
min++;
}
}
// printf("\n");
// printf("%d \n",min);
printf("Case #%d: ",k++);
int wrong=0;
int mis=0;
for(i=1;i<=n;i++)
{
if(set[i]==i)
wrong++;
if(wrong>1)
{
mis=1;
break;
}
}
if(mis)
{
printf("No\n");
continue;
}
int ok=0;
for(i=1;fib[i]<=m;i++)
{
if(fib[i]>=min&&fib[i]<=max)
{
printf("Yes\n");
ok=1;
break;
}
}
if(!ok)
printf("No\n");
}
return 0;
}

  

hdoj 4786 Fibonacci Tree【并查集+最小生成树(kruskal算法)】的更多相关文章

  1. 最小生成数(并查集)Kruskal算法

    并查集:使用并查集可以把每个连通分量看作一个集合,该集合包含连通分量的所有点.这两两连通而具体的连通方式无关紧要,就好比集合中的元素没有先后顺序之分,只有属于和不属于的区别.#define N 100 ...

  2. 并查集 & 最小生成树详细讲解

    并查集 & 最小生成树 并查集 Disjoint Sets 什么是并查集?     并查集,在一些有N个元素的集合应用问题中,我们通常是在开始时让每个元素构成一个单元素的集合,然后按一定顺序将 ...

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

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

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

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

  5. 【转】最小生成树——Kruskal算法

    [转]最小生成树--Kruskal算法 标签(空格分隔): 算法 本文是转载,原文在最小生成树-Prim算法和Kruskal算法,因为复试的时候只用到Kruskal算法即可,故这里不再涉及Prim算法 ...

  6. HDU 4786 Fibonacci Tree 最小生成树

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

  7. hdu 4786 Fibonacci Tree(最小生成树)

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

  8. CodeForces892E 可撤销并查集/最小生成树

    http://codeforces.com/problemset/problem/892/E 题意:给出一个 n 个点 m 条边的无向图,每条边有边权,共 Q 次询问,每次给出 ki​ 条边,问这些边 ...

  9. CodeForces - 891C: Envy(可撤销的并查集&最小生成树)

    For a connected undirected weighted graph G, MST (minimum spanning tree) is a subgraph of G that con ...

随机推荐

  1. tar 解压缩

    解压 tar –xvf file.tar //解压 tar包 tar -xzvf file.tar.gz //解压tar.gz tar -xjvf file.tar.bz2   //解压 tar.bz ...

  2. python学习笔记--随时更新

    # coding=GBK score = 90 if score >= 80: print("好") elif score >= 60: print("及格& ...

  3. php中如何实现网上商城用户历史浏览记录的代码

    /如是COOKIE 里面不为空,则往里面增加一个商品ID if (!empty($_COOKIE['SHOP']['history'])){ //取得COOKIE里面的值,并用逗号把它切割成一个数组 ...

  4. jQuery慢慢啃之回调(十三)

    1.callbacks.add(callbacks)//回调列表中添加一个回调或回调的集合 // a sample logging function to be added to a callback ...

  5. Ubuntu系统、开发环境配置

    在VMware10下安装成功了Ubuntu 13.10桌面版,刚安装完需要配置很多内容,下面为记录: 1. 更新源: 想了解更新地址的可以查看apt-get的源列表文件 $ sudo gedit /e ...

  6. 设置(TableViewController)通用框架

    本文学习于传播播客.李明杰老师.感谢

  7. Python深入学习笔记(二)

    计数器Counter Counter类是自Python2.7起增加的,属于字典类的子类,是一个容器对象,主要用来统计散列对象,支持集合操作+.-.&.|,其中后两项分别返回两个Counter对 ...

  8. Python中使用中文

    python的中文问题一直是困扰新手的头疼问题,这篇文章将给你详细地讲解一下这方面的知识.当然,几乎可以确定的是,在将来的版本中,python会彻底解决此问题,不用我们这么麻烦了. 先来看看pytho ...

  9. 链表与Hash检索实测

    测试环境: Win7 SP1.8G内存.3.4GHz 4核 测试代码: using System; using System.Collections.Generic; using System.Lin ...

  10. C#委托(Delegate)学习日记

    在.NET平台下,委托类型用来定义和响应应用程序中的回调.事实上,.NET委托类型是一个类型安全的对象,指向可以以后调用的其他方法.和传统的C++函数指针不同,.NET委托是内置支持多路广播和异步方法 ...