Fibonacci Tree

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

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
题意:n个点m条边,其中有一些边是白边,一些边是黑边,问是否存在一棵树上的白边数量是斐波拉契数列里面的某个数.
题解:很巧妙的思想,先按白边排序将白边最多的树选出来,然后黑边排序将白边最少的树选出来。然后如果有斐波拉契数在两棵树的大小中间(因为如果存在的话,是可以通过删边得到的),如果存在,就Ok,还要判一下连通分量。

#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <queue>
using namespace std;
const int N = ;
int father[N];
struct Edge{
int u,v,color;
}edge[N];
int _find(int x){
if(x!=father[x]) {
father[x] = _find(father[x]);
}
return father[x];
} int n,m;
int cmp(Edge a,Edge b){
return a.color>b.color;
}
int cmp1(Edge a,Edge b){
return a.color<b.color;
}
int kruskal(){
int cost=;
for(int i=;i<m;i++){
int x=_find(edge[i].u);
int y=_find(edge[i].v);
if(x!=y){
father[x] = y;
cost+=edge[i].color;
}
}
return cost;
}
bool vis[N];
void init(){
memset(vis,false,sizeof(vis));
int a=,b=;
vis[]=true,vis[] =true;
while(a+b<N){
vis[a+b]=true;
swap(a,b);
b = a+b;
}
}
int main()
{
int tcase;
scanf("%d",&tcase);
int t = ;
init();
while(tcase--){
scanf("%d%d",&n,&m);
for(int i=;i<m;i++){
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].color);
}
for(int i=;i<=n;i++) father[i] = i;
sort(edge,edge+m,cmp);
int maxn = kruskal();
int ans = ;
for(int i=;i<=n;i++){
if(father[i]==i) ans++;
if(ans>) break;
}
if(ans>) {
printf("Case #%d: No\n",t++);
continue;
}
for(int i=;i<=n;i++) father[i] = i;
sort(edge,edge+m,cmp1);
int minn = kruskal();
ans = ;
for(int i=;i<=n;i++){
if(father[i]==i) ans++;
if(ans>) break;
}
if(ans>) {
printf("Case #%d: No\n",t++);
continue;
}
//printf("%d %d\n",minn,maxn);
bool flag = false;
for(int i=minn;i<=maxn;i++){
if(vis[i]){
flag = true;
break;
}
}
if(flag) printf("Case #%d: Yes\n",t++);
else printf("Case #%d: No\n",t++);
}
return ;
}

hdu 4786(生成树)的更多相关文章

  1. HDU 4786 生成树 并查集+极大极小值 黑白边 确定选择白边的数量

    题意: 给定一个无向图 n 个点 m条无向边 u v val val == 1 表示边(u, v) 为白边 问能否找到n个点的生成树, 使得白边数为斐波那契数 思路: 并查集求图是否连通( 是否存在生 ...

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

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

  3. HDU 4786 Fibonacci Tree 生成树

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4786 题意:有N个节点(1 <= N <= 10^5),M条边(0 <= M <= ...

  4. hdu 4786 最小生成树与最大生成树

    /* 题意 :有一些边权值为1和0,判断是否存在一个生成树使得他的总权值为一个斐波那契数. 解法:建立一个最小生成树向里面加权值为1的边替换为0的边,保证原来的联通.因为权值为1,可直接求出最大生成树 ...

  5. hdu 4786 Fibonacci Tree (最小、最大生成树)

    题意: N个点,M条边.每条边连接两个点u,v,且有一个权值c,c非零即一. 问能否将N个点形成一个生成树,并且这棵树的边权值和是一个fibonacii数. (fibonacii数=1,2,3,5,8 ...

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

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

  7. HDU 4786 Fibonacci Tree 最小生成树

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

  8. HDU 4786(最小生成树 kruskal)

    题目链接:pid=4786" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=4786 Prob ...

  9. 【HDU 4786 Fibonacci Tree】最小生成树

    一个由n个顶点m条边(可能有重边)构成的无向图(可能不连通),每条边的权值不是0就是1. 给出n.m和每条边的权值,问是否存在生成树,其边权值和为fibonacci数集合{1,2,3,5,8...}中 ...

随机推荐

  1. BootCDN 开源项目免费 CDN 加速服务,Jquery插件库

    2017-11-17  19:38:32 免费好用的在线 css js 文件引用 BootCDN - Bootstrap 中文网开源项目免费 CDN 加速服务 Jquery插件库 .

  2. python 2.7版本解决TypeError: 'encoding' is an invalid keyword argument for this function

    今天在用yaml处理数据时,由于yaml.load可接收一个byte字符串,unicode字符串,打开的二进制文件或文本文件对象,但字节字符串和文件必须是utf-8,utf-16-be或utf-16- ...

  3. STM32串口——中断方式的一般配置方法

    #include "stm32f10x.h" /************************************************ 该程序讲解串口程序的一般配置方法: ...

  4. mysql进阶三四五六

    排序查询 一.语法 select 查询表 from 表 where 筛选条件 order by 排序列表[asc / desc] 特点: 1.asc:升序 desc:降序 2.排序列表之中支持单字段, ...

  5. python中subprocess.Popen的args和shell参数的使用

    subprocess模块定义了一个类: Popen class subprocess.Popen( args,        bufsize=0,        executable=None,    ...

  6. Python+Selenium练习篇之3-利用tag name定位元素

    前一篇文章介绍了如何通过元素的id值来定位web元素,本文介绍如何通过tag name来定位元素.个人认为,通过tag name来定位还是有很大缺陷,定位不够精确.主要是tag name有很多重复的, ...

  7. var、let、const与JavaScript变量/常量的定义

    早期的JavaScript中,声明变量只能使用var关键字定义变量,并没有定义常量的功能.通过var关键字定义的变量,其作用域只能函数级或是全局作用域,并没有块级作用域.ES6(ECMAScript ...

  8. python 浮点数问题

    为什么 输入:0.2 + 0.1 得到的是:0.30000000000000004???? 0.1 * 3 = 0.30000000000000004????

  9. [python][django学习篇][11]后台admin用户登录博客,添加文章---这一章和博客首页设计没有关系

    1 如果没有创建超级管理员账号,先要创建python manage.py createsuperuser 2 在admin后台注册模型(如果没有这一步,登录http://127.0.0.1:8000/ ...

  10. easyui datagrid 学习 (一)

    注意:当使用谷歌浏览器时!需要 设置style="overflow:hidden",这样则可以去掉滚动条!(该样式添加到layout上!) fit:属性 自动填父容器, borde ...