Fibonacci Tree

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

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
 

只要白边优先和黑边优先两种顺序做两次最小生成树。

得到白边数量的区间,然后枚举斐波那契数列就可以了。

注意如果一开始是非连通的,输出NO

 /* ***********************************************
Author :kuangbin
Created Time :2013-11-16 14:14:50
File Name :E:\2013ACM\专题强化训练\区域赛\2013成都\1006.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; int f[]; struct Edge
{
int u,v,c;
}edge[];
int F[];
int find(int x)
{
if(F[x] == -)return x;
return F[x] = find(F[x]);
} bool cmp1(Edge a,Edge b)
{
return a.c < b.c;
}
bool cmp2(Edge a,Edge b)
{
return a.c > b.c;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int tot = ;
f[] = ;
f[] = ;
while(f[tot] <= )
{
f[tot+] = f[tot] + f[tot-];
tot++;
}
int T;
int iCase = ;
int n,m;
scanf("%d",&T);
while(T--)
{
iCase++;
scanf("%d%d",&n,&m);
for(int i = ;i < m;i++)
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].c);
sort(edge,edge+m,cmp1);
memset(F,-,sizeof(F));
int cnt = ;
for(int i = ;i < m;i++)
{
int t1 = find(edge[i].u);
int t2 = find(edge[i].v);
if(t1 != t2)
{
F[t1] = t2;
if(edge[i].c == )cnt++;
}
}
int Low = cnt;
memset(F,-,sizeof(F));
sort(edge,edge+m,cmp2);
cnt = ;
for(int i = ;i < m;i++)
{
int t1 = find(edge[i].u);
int t2 = find(edge[i].v);
if(t1 != t2)
{
F[t1] = t2;
if(edge[i].c == )cnt++;
}
}
int High = cnt;
bool ff = true;
for(int i = ;i <= n;i++)
if(find(i) != find())
{
ff = false;
break;
}
if(!ff)
{
printf("Case #%d: No\n",iCase);
continue;
}
bool flag = false;
for(int i = ;i <= tot;i++)
if(f[i] >= Low && f[i] <= High)
flag = true;
if(flag)
printf("Case #%d: Yes\n",iCase);
else printf("Case #%d: No\n",iCase); }
return ;
}

HDU 4786 Fibonacci Tree (2013成都1006题)的更多相关文章

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

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

  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 最小生成树

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

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

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

  5. HDU 4786 Fibonacci Tree

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

  6. HDU 4786 Fibonacci Tree (2013成都1006题) 最小生成树+斐波那契

    题意:问生成树里能不能有符合菲波那切数的白边数量 思路:白边 黑边各优先排序求最小生成树,并统计白边在两种情况下数目,最后判断这个区间就可以.注意最初不连通就不行. #include <stdi ...

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

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

  8. HDU 4786 Fibonacci Tree 生成树

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

  9. hdu 4786 Fibonacci Tree 乱搞 智商题目 最小生成树

    首先计算图的联通情况,如果图本身不联通一定不会出现生成树,输出"NO",之后清空,加白边,看最多能加多少条,清空,加黑边,看能加多少条,即可得白边的最大值与最小值,之后判断Fibo ...

随机推荐

  1. Myeclipse/STS 首次在本地部署配置一个Spring MVC 项目 (十二)

    1. 在本地新创建一个文件夹 ,做为项目工作空间; 2. 用 Myeclipse 或 STS 进入该文件夹,该文件夹就成为项目的工作空间: 3. 就要进 窗口-首选项,配置: 环境默认编码: 1> ...

  2. 【转】在Mac OS X 10.8中配置Apache + PHP + MySQL

    CHENYILONG Blog 在Mac OS X 10.8中配置Apache + PHP + MySQL 在Mac OS X 10.8中配置Apache+PHP+MySQL的内容包括: 配置Apac ...

  3. 第8月第15天 app支持后台播放

    1. AVAudioSession *audioSession = [AVAudioSession sharedInstance]; //默认情况下扬声器播放 [audioSession setCat ...

  4. Spark笔记之使用UDAF(User Defined Aggregate Function)

    一.UDAF简介 先解释一下什么是UDAF(User Defined Aggregate Function),即用户定义的聚合函数,聚合函数和普通函数的区别是什么呢,普通函数是接受一行输入产生一个输出 ...

  5. 内置函数bytes()

    a=b'\x00\x9c@c' print a[3]#99,c的ascii码是99 print a[1]#156 并且byte是无法修改的 c[1]=155 Traceback (most recen ...

  6. Javascript - Vue - vue对象

    vue提供了一整套前端解决方案,可以提升企业开发效率 vue的处理过程 app.js 项目入口,所有请求最先进入此模块进行处理 route.js 由app.js调用,处理路由的分发 controlle ...

  7. yui压缩JS和CSS文件

    CSS和JS文件经常需要压缩,比如我们看到的XX.min.js是经过压缩的JS. 压缩文件第一个是可以减小文件大小,第二个是对于JS文件,默认会去掉所有的注释,而且会去掉所有的分号,也会将我们的一些参 ...

  8. 【SVN】命令行忽略不必要的文件和文件夹

    SVN命令参考:   https://www.cnblogs.com/wlsxmhz/p/5775393.html 我们需要明白命令行设置忽略文件和文件夹是通过设置svn:ignore属性设置的,pr ...

  9. python_ssh连接

    首先下载paramikopip install paramiko查看并启动ssh服务service ssh status 添加用户:useradd -d /home/zet zetpasswd zet ...

  10. 六、vue路由Vue Router

    一.基本概念 route, routes, router 1, route,它是一条路由,由这个英文单词也可以看出来,它是单数, Home按钮  => home内容, 这是一条route,  a ...