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. iOS8 iPad Warning: Attempt to present <UIImagePickerController:xxxx > on xxxx which is already presenting (null)

    解决方法: /* I think this is because in iOS 8, alert views and action sheets are actually presented view ...

  2. DevExpress XtraGrid RepositoryItemCheckEdit 复选框多选的解决方法

    1. RepositoryItemCheckEdit默认有三种状态,选中状态.未选中状态和半选中状态(半选中状态通常用在TreeList中如果父节点下的子节点有选中的有未选中的,则父节点状态为半选中状 ...

  3. ios专题 - sandbox机制

    [原创]http://www.cnblogs.com/luoguoqiang1985 ios在安装APP时,把APP的偏好设置与数据放在sandbox里.sandbox通过一系列细颗粒度控制APP访问 ...

  4. 中文翻译:pjsip教程(三)之ICE stream transport的使用

    1:pjsip教程(一)之PJNATH简介 2:pjsip教程(二)之ICE穿越打洞:Interactive Connectivity Establishment简介 3:pjsip教程(三)之ICE ...

  5. Firebug中命令行栏(Commandlinie)的使用介绍和总结

    Commandlinie是Firebug中总有用的一个特性.如果你有Microsoft Visual Studio的使用经验,你就会知道“Immediate Window” 和“Watch Windo ...

  6. quick-x 触摸事件的新方法

    --[[ local function onTouch(event, x, y) print(event, x, y) if event == "began" then retur ...

  7. MVC描述对象的类关系图/调用关系图【学习笔记】

  8. winfrom获得鼠标的坐标

    Point mouse = this.PointToScreen(Control.MousePosition);label1.Text = mouse.X.ToString() + ":&q ...

  9. pageControl设置不居中显示,居左或居右

    UIPageControl控件,默认是居中显示的,如下图: 在很多的APP中,会看到pageControl是居左或居右显示的,如下图:   如何控制pageControl的位置显示呢? 设置为居右的代 ...

  10. BZOJ 4013 实验比较

    Description 小D被邀请到实验室,做一个跟图片质量评价相关的主观实验.实验用到的图片集一共有\(N\)张图片,编号为\(1\)到\(N\).实验分若干轮进行,在每轮实验中,小\(D\)会被要 ...