hdu 4786(生成树)
Fibonacci Tree
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4028 Accepted Submission(s): 1252
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, ... )
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).
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.
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
Case #2: No
#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(生成树)的更多相关文章
- HDU 4786 生成树 并查集+极大极小值 黑白边 确定选择白边的数量
题意: 给定一个无向图 n 个点 m条无向边 u v val val == 1 表示边(u, v) 为白边 问能否找到n个点的生成树, 使得白边数为斐波那契数 思路: 并查集求图是否连通( 是否存在生 ...
- HDU 4786 Fibonacci Tree(生成树,YY乱搞)
http://acm.hdu.edu.cn/showproblem.php? pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others ...
- HDU 4786 Fibonacci Tree 生成树
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4786 题意:有N个节点(1 <= N <= 10^5),M条边(0 <= M <= ...
- hdu 4786 最小生成树与最大生成树
/* 题意 :有一些边权值为1和0,判断是否存在一个生成树使得他的总权值为一个斐波那契数. 解法:建立一个最小生成树向里面加权值为1的边替换为0的边,保证原来的联通.因为权值为1,可直接求出最大生成树 ...
- hdu 4786 Fibonacci Tree (最小、最大生成树)
题意: N个点,M条边.每条边连接两个点u,v,且有一个权值c,c非零即一. 问能否将N个点形成一个生成树,并且这棵树的边权值和是一个fibonacii数. (fibonacii数=1,2,3,5,8 ...
- hdu 4786 Fibonacci Tree (2013ACMICPC 成都站 F)
http://acm.hdu.edu.cn/showproblem.php?pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) ...
- HDU 4786 Fibonacci Tree 最小生成树
Fibonacci Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4786 Description Coach Pang is intere ...
- HDU 4786(最小生成树 kruskal)
题目链接:pid=4786" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=4786 Prob ...
- 【HDU 4786 Fibonacci Tree】最小生成树
一个由n个顶点m条边(可能有重边)构成的无向图(可能不连通),每条边的权值不是0就是1. 给出n.m和每条边的权值,问是否存在生成树,其边权值和为fibonacci数集合{1,2,3,5,8...}中 ...
随机推荐
- 2017 United Kingdom and Ireland Programming(Gym - 101606)
题目很水.睡过了迟到了一个小时,到达战场一看,俩队友AC五个了.. 就只贴我补的几个吧. B - Breaking Biscuits Gym - 101606B 旋转卡壳模板题.然后敲错了. 代码是另 ...
- 动态规划:HDU2844-Coins(多重背包的二进制优化)
Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- 8 REST Framework 实现Web API 1
1 参考博客: http://blog.csdn.net/SVALBARDKSY/article/details/50548073 2 准备工作 1. 环境 Python: Python 3.5 D ...
- Java JVM 内存空间解析
运行时数据区: 运行时数据区主要分五块,分别是Method Area , VM Stack , Native Method Stack , Heap , program Counter Registe ...
- 【POJ 3764】The Xor-longest Path
题目 给定一个\(n\)个点的带权无根树,求树上异或和最大的一条路径. \(n\le 10^5\) 分析 一个简单的例子 相信大家都做过这题: 给定一个\(n\)个点的带权无根树,有\(m\)个询问, ...
- BugKu-图穷匕见
拿到图片后,先放到winhex,看看文件头是不是和jpg匹配,看看文件尾,不是FFD9 ,说明后边肯定是藏了什么东西. 顺便找一下文件尾有没有flag(估计是签到题目才会这样吧). binwalk跑一 ...
- Python-S9——Day115-Flask Web框架
01 当日内容概要 1 当日内容概要 1.1 Flask基础: 1.2 Web框架包含的基础组件: 1.2.1 路由.视图函数.模板渲染: 1.3 Flask配置文件: 1.4 Flask的路由系统: ...
- CSU-1974 神奇药水
CSU-1974 神奇药水 Description 对于csuxushu来说,能够在CSU(California State University)组织2017年的ACM暑期集训让他感到十分荣幸. ...
- hdu 3714 Error Curves(三分)
Error Curves Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- 构建乘积数组--java
题目:给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1].不能使 ...