POJ 4786 Fibonacci Tree
Fibonacci Tree
This problem will be judged on HDU. Original ID: 4786
64-bit integer IO format: %I64d Java class name: Main
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
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
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc {
int u,v,c;
};
int uf[maxn],n,m,cnt;
int fib[] = {,};
arc e[maxn];
void init() {
for(int i = ; i < ; i++)
fib[i] = fib[i-] + fib[i-];
}
bool cmp1(const arc &x,const arc &y) {
return x.c < y.c;
}
bool cmp2(const arc &x,const arc &y) {
return x.c > y.c;
}
int Find(int x) {
if(x == uf[x]) return uf[x];
return uf[x] = Find(uf[x]);
}
int kruskal(bool flag) {
for(int i = ; i <= n; i++) uf[i] = i;
int k = cnt = ;
if(flag) sort(e,e+m,cmp1);
else sort(e,e+m,cmp2);
for(int i = ; i < m; i++) {
int tx = Find(e[i].u);
int ty = Find(e[i].v);
if(tx != ty) {
uf[tx] = ty;
if(e[i].c) k++;
cnt++;
}
}
return k;
}
int main() {
int t,x,y,cs = ;
init();
scanf("%d",&t);
while(t--) {
scanf("%d %d",&n,&m);
for(int i = ; i < m; i++)
scanf("%d %d %d",&e[i].u,&e[i].v,&e[i].c);
x = kruskal(true);
if(cnt < n-) {
printf("Case #%d: No\n",cs++);
continue;
}
y = kruskal(false);
int *tmp = lower_bound(fib,fib+,x);
if(*tmp >= x && *tmp <= y)
printf("Case #%d: Yes\n",cs++);
else printf("Case #%d: No\n",cs++);;
}
return ;
}
POJ 4786 Fibonacci Tree的更多相关文章
- 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(生成树,YY乱搞)
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 Fibonacci Tree
Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) P ...
- hdoj 4786 Fibonacci Tree【并查集+最小生成树(kruskal算法)】
Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 4786 Fibonacci Tree (2013成都1006题)
Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 4786 Fibonacci Tree(最小生成树)
Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 【HDU 4786 Fibonacci Tree】最小生成树
一个由n个顶点m条边(可能有重边)构成的无向图(可能不连通),每条边的权值不是0就是1. 给出n.m和每条边的权值,问是否存在生成树,其边权值和为fibonacci数集合{1,2,3,5,8...}中 ...
- hdu 4786 Fibonacci Tree 乱搞 智商题目 最小生成树
首先计算图的联通情况,如果图本身不联通一定不会出现生成树,输出"NO",之后清空,加白边,看最多能加多少条,清空,加黑边,看能加多少条,即可得白边的最大值与最小值,之后判断Fibo ...
随机推荐
- Android学习笔记(8):ViewGroup类
A ViewGroup is a special view that can contain other views (called children.) The view group is the ...
- 时间格式字符串转化为date和时间戳
NSString *dateStr=@"2012-05-17 11:23:23"; NSLog(@"dateStr=%@",dateStr); NSDateFo ...
- luogu1024 一元三次方程求解
题目大意 已知一元三次方程\(ax^3+bx^2+cx+d=0\): 有且只有3个根 对\(\forall x, x\in[-100,100]\) 对\(\forall x_1,x_2,|x_1-x_ ...
- 关于ShapeDrawable应用的一些介绍(中)之Gradient
版权声明:本文为博主原创文章,未经博主允许不得转载. Gradient,渐变,是在界面设计中最经常用到的一种技巧,只要涉及到颜色的处理,浓妆淡抹总相宜,说的就是它. 在Android中,当然也提供了这 ...
- Swift - 将字符串拆分成数组(把一个字符串分割成字符串数组)
在Swift中,如果需要把一个字符串根据特定的分隔符拆分(split)成字符串数组,通常有如下两种方法: 1,使用componentsSeparatedByString()方法 1 2 3 4 5 l ...
- poj--1664--放苹果(递归好体)
放苹果 Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u Submit Status De ...
- mysqls,为node.js而编写的sql语句生成插件 (crud for mysql).
It is written in JavaScript,crud for mysql.You can also use transactions very easily. mysqls 一款专为nod ...
- BZOJ 1537 cdq分治
思路: 我只是想写一下cdq-- 二维偏序 一维排序 一维cdq分治 (我忘了归并排序怎么写了,,,) 写了个sort- 复杂度是O(nlog^2n) //By SiriusRen #include ...
- vim产生的.swap文件
转载自 http://ibeyond.blog.51cto.com/1988404/800138 有时候在用vim打开文件时提示类似以下的信息: E325: 注意发现交换文件 ".expor ...
- DataTable转Json就是这么简单(Json.Net DLL (Newtonsoft))
之前JSON转DataTable可以见我之前的随笔 链接Json转换成DataTable 之前没有用过DataTable,之后随着需求的叠加发现需要将DataTable转换成Json.因为之前都是用的 ...