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. VMware虚拟机中如何安装VMWare-Tools详解

    VMware虚拟机中如何安装VMWare-Tools详解 好处:可以支持图形界面,可以支持共享文件功能等 VMware虚拟机中如何配置显 VMware作为一款虚拟机利器,很多人都利用它来实现Linux ...

  2. How To Read a Paper.md

    @ Titile How To Read a Paper.md @ author Keshav, 译 uuplusu   # 1. Intro    1. 读论文重要    2. 没有人教    3. ...

  3. javascript判断设备类型-手机(mobile)、安卓(android)、电脑(pc)、其他(ipad/iPod/Windows)等

    使用device.js检测设备并实现不同设备展示不同网页 html代码: <!doctype html> <html> <head> <meta charse ...

  4. Object之魔术函数__call() 处理错误调用

    在提到__call之前,先来看一个实例的测试结果,以便更好地去了解__call方法的作用.上代码: <?php class Person{ function say(){ echo " ...

  5. ubuntu 安装apache2,mysql,php5,phpmyadmin等软件

    1.安装apache2  sudo apt-get install apache2  输入Y回车  apache2 安装完成  检测:在浏览器输入localhost 出现It works则成功. 2. ...

  6. C#快递单号查询源码

    源码本人测试过,没有啥问题,能查询快递单号,支持的快递还挺多,圆通快递.申通快递.韵达快递的都支持单号查询的,程序是通过向爱快递(www.aikuaidi.cn)接口传输参数来查询快递单号,我直接把代 ...

  7. PM【terminal】

    More Knowledge More Performance More Time 资料模组化 以知识管理为基础的项目管理 规范:ethic

  8. 4542: [Hnoi2016]大数

    Description 小 B 有一个很大的数 S,长度达到了 N 位:这个数可以看成是一个串,它可能有前导 0,例如00009312345.小B还有一个素数P.现在,小 B 提出了 M 个询问,每个 ...

  9. Scut 上线后遇到的问题

    1. 上线后的大并发问题: var sem = new Semaphore(_accepts, _accepts); while (true) { sem.WaitOne(); #pragma war ...

  10. shell抓取

    #!/bin/sh ` configDir="$dir/config" ipport="$configDir/ip_port" url="http:/ ...