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 ...
随机推荐
- Web API接口设计(学习)
1.在接口定义中确定MVC的GET或者POST方式 由于我们整个Web API平台是基于MVC的基础上进行的API开发,因此整个Web API的接口,在定义的时候,一般需要显示来声明接口是[HttpG ...
- Softmax回归——logistic回归模型在多分类问题上的推广
Softmax回归 Contents [hide] 1 简介 2 代价函数 3 Softmax回归模型参数化的特点 4 权重衰减 5 Softmax回归与Logistic 回归的关系 6 Softma ...
- EOJ 1114 素数环
题意 一个由自然数 1…n (n≤18) 素数环就是如下图所示,环上任意两个节点上数值之和为素数. 1 / \ 4 2 \ / 3 Input 输入只有一个数 n,表示你需要建立一个 1… ...
- NOIP2012 D2 T2 借教室 线段树 OR 二分法
题目描述: 在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要向学校申请借教室.教室的大小功能不同,借教室人的身份不同,借教室的手续也不一样. 面对海量租借教室的信息,我们自 ...
- 使用C#正则表达式获取必应每日图片地址
微软的Bing搜索引擎首页每天都会提供了一些有趣的图片,下面使用正则表达式获取图片的地址,不管是在手机app还是在网站上都是很好的图片素材,而且每天更新,非常不错. 首先访问微软的API,该地址返回的 ...
- 关于用户禁用Cookie的解决办法和Session的图片验证码应用
当用户通过客户端浏览页面初始化了Session之后(如:添加购物车,用户登陆等),服务器会将这些session数据保存在:Windows保存在C:\WINDOWS\Temp的目录下,Linux则是保存 ...
- swiper套路
swiper插件 quick start 基本结构 <div class="swiper-container"> <div class="swiper- ...
- 织梦忘记密码DedeCMS密码重设工具radminpass找回密码
本工具是用于新人忘记管理员密码重设所制作,只需要将radminpass.php文件拷贝到根目录,运行“http://yousite/radminpass.php(yousite为网站域名)”,按照操作 ...
- WPF 标题栏 右键窗口标题添加关于对话框
/// <summary> /// wpf标题栏 右键菜单 中添加新项 /// </summary> public partial class MainWindow : Win ...
- adb 通过 WiFi 连接 Android 设备
PC 和 Android 设备连接在同一个局域网. 查看 Android 设备的 IP:设置 > WLAN > 选择连接的WiFi > 查看IP地址. PC 端执行: ping &l ...