Fibonacci Tree

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2340    Accepted Submission(s): 748

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
 
Source





题意:问构成的生成树当中是否存在黑色边(边为1)数为斐波那契数
思路:求出生成树中最小包括的黑色边数。和最多黑色边数,假设有斐波那契数在两者之间,则能够构成。由于黑白边能够搭配使用
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int n,m;
int fibo[50];
int f[100010];
struct node
{
int u,v,c;
} s[100010]; bool cmp1(node x , node y)
{
return x.c < y.c;
} bool cmp2(node x, node y)
{
return x.c > y.c;
} int find(int x)
{
return x == f[x] ? x : f[x] = find(f[x]);
} void Union(int x ,int y)
{
int fx = find(x);
int fy = find(y); if(fx != fy)
{
f[fx] = fy;
}
} int main()
{
#ifdef xxz
freopen("in.txt","r",stdin);
#endif
fibo[1] = 1;
fibo[2] = 2;
for(int i = 3; ; i++)
{
fibo[i] = fibo[i-1] + fibo[i-2];
if(fibo[i] >= 100000) break;
} int T,Case = 1;;
scanf("%d",&T); while(T--)
{ scanf("%d%d",&n,&m);
for(int i = 1; i <= n; i++) f[i] = i; for(int i = 0; i < m; i++)
{ scanf("%d%d%d",&s[i].u,&s[i].v,&s[i].c);
Union(s[i].u,s[i].v);
}
int cent = 0;
int bl = 0, bh = 0;
int root = 0,size = 0; for(int i = 1; i <= n; i++)
{
if(f[i] == i)
{
cent++;
root = i;
}
} printf("Case #%d: ",Case++);
if(cent >= 2) cout<<"No"<<endl;//首先要推断能否构成一个生成树。推断根节点个数是否为1即可
else
{
sort(s,s+m,cmp1);
for(int i = 1; i <= n; i++) f[i] = i; for(int i = 0; i < m; i++)
{
int fu = find(s[i].u);
int fv = find(s[i].v);
if(fu == fv) continue; bl += s[i].c;
size++;
Union(s[i].u,s[i].v);
if(size == n-1) break;
} size = 0;
sort(s,s+m,cmp2);
for(int i = 1; i <= n; i++) f[i] = i; for(int i = 0; i < m; i++)
{
int fu = find(s[i].u);
int fv = find(s[i].v);
if(fu == fv) continue; bh += s[i].c;
size++;
Union(s[i].u,s[i].v);
if(size == n-1) break;
} int flag = 0;
for(int i =1; fibo[i] <= 100000 ; i++ )
{
if(fibo[i] >= bl && fibo[i] <= bh)
{
flag = 1;
break;
}
}
if(flag) printf("Yes\n");
else printf("No\n"); } }
}

Hdu4786的更多相关文章

  1. 【最小生成树】【kruscal】hdu4786 Fibonacci Tree

    假设这张图能够形成具有k条白边的生成树, 则易证k一定形成一个连续的区间[a,b],中间一定不会断开.要是断开……tm怎么可能. 所以求出a,b就好啦,人家都给你把白边赋成1了,直接跑一下最小生成树, ...

  2. hdu4786 Fibonacci Tree (最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4786 题意:给定图的n个点和m条双向边,告诉你每条边的权值.权值为1表示该边是白边,权值为0表示该边为 ...

  3. hdu4786 Fibonacci Tree[最小生成树]【结论题】

    一道结论题:如果最小生成树和最大生成树之间存在fib数,成立.不存在或者不连通则不成立.由于是01图,所以这个区间内的任何生成树都存在. 证明:数学归纳?如果一棵树没有办法再用非树边0边替代1边了,那 ...

  4. 最小生成树练习2(Kruskal)

    两个BUG鸣翠柳,一行代码上西天... hdu4786 Fibonacci Tree(生成树)问能否用白边和黑边构成一棵生成树,并且白边数量是斐波那契数. 题解:分别优先加入白边和黑边,求出生成树能包 ...

  5. Fibonacci Tree

    hdu4786:http://acm.hdu.edu.cn/showproblem.php?pid=4786 题意:给你一个无向图,然后其中有的边是白色的有的边是黑色的.然后问你是否存在一棵生成树,在 ...

  6. The 2013 ACMICPC Asia Regional Chengdu

    还有19天出发北京站,今年北京站的出题方是上交,去年他们出的成都现场的赛题,首先复盘一下. 去年的成都是我经历的第一次现场赛,也是近距离第一次见到了CLJ的真人,最后也是被虐惨了,那时候是声闻大神带着 ...

随机推荐

  1. java有参无参构造器的的执行顺序

    这里拿了用数组构造栈的一段代码说明一下 public class StackArray<E> { private Object[] data = null; private int max ...

  2. spring mvc过滤器filter

    SpringMVC 过滤器Filter使用解析 1.如上所示的spring-web.jar包结构所示, Spring的web包中中提供有很多过滤器,这些过滤器位于org.springframework ...

  3. [using_microsoft_infopath_2010]Chapter2 表单需求,使用表决矩阵

    本章概要 1.从模板创建表单 2.从创建表单收集需求 3.使用全部表单决策 4.决定需要创建哪种表单

  4. POJ 1721

    好像不需要用到开方什么的... 可以知道,一副牌即是一个循环,那么,由于GCD(L,K)=1,所以一次洗牌后,亦是一个循环.其实,K次洗牌等于是T^(2^K)了.既然是循环,必定有周期.那么,周期是多 ...

  5. map和multimap映射容器

    map容器 map所处理的数据与数据库表具有键值的记录非常相似,在键值与映射数据之间,建立一个数学上的映射关系.map容器的数据结构仍然採用红黑树进行管理.插入的元素键值不同意反复,所使用的结点元素的 ...

  6. Swift的构造和析构过程

    构造过程 Swift的构造过程通过定义构造器来实现. 只是与Objective-C不同的是,Swift的构造器不须要返回值,相同也不须要表明Func. 另外值得提的是,当构造器中为存储型属性赋值时.不 ...

  7. NYOJ 815 三角形【海伦公式】

    /* 关键点:海伦公式 解题人:lingnichong 解题时间:2014-10-04 21:48:47 解题体会:海伦公式的使用 */ 三角形 时间限制:1000 ms  |  内存限制:65535 ...

  8. Android 启动界面的制作

    直接看实例吧 package com.example.textview; import android.app.Activity; import android.content.Intent; imp ...

  9. QMutex“A mutex must be unlocked in the same thread that locked it”解决(在run里创建对象是不二法宝)

    多线程时出现如下警告信息: A mutex must be unlocked in the same thread that locked it: 原因可能有二: 1.创建QMutex不在当前线程: ...

  10. nyoj--55--懒省事的小明(STL优先队列)

    懒省事的小明 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述       小明很想吃果子,正好果园果子熟了.在果园里,小明已经将所有的果子打了下来,而且按果子的不同种类分 ...