Fibonacci Tree

Time Limit: 2000ms
Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 4786
64-bit integer IO format: %I64d      Java class name: Main

 
  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

 
解题:最小生成树Kruskal思想。
 
 #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的更多相关文章

  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) P ...

  5. hdoj 4786 Fibonacci Tree【并查集+最小生成树(kruskal算法)】

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

  6. HDU 4786 Fibonacci Tree (2013成都1006题)

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

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

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

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

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

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

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

随机推荐

  1. 11153 kill boss

    11153 kill boss 时间限制:1000MS  内存限制:65535K提交次数:1090 通过次数:340 题型: 编程题   语言: G++;GCC Description Acmer最近 ...

  2. ant整合junit自己主动化測试

    一. 使用Junit进行測试 1. Java业务代码: public class HelloWorld { // 測试返回"world" public String hello() ...

  3. 常用框架(一):spring+springMvc+mybatis+maven

    项目说明: (1) 本例采用 maven web 工程做例子讲解 (2) 利用mybaits 提供的代码生成工具自动生成代码(dao接口,sql mapper映射文件,pojo数据库映射类) (3) ...

  4. Android常用的一些make命令【转】

    本文转载自:http://blog.csdn.net/liuxd3000/article/details/39181377 1.make -jX  X表示数字,这个命令将编译Android系统并生成镜 ...

  5. 虚拟机CentOS设置IP

    虚拟机里Centos7的IP地址查看方法 本地虚拟机安装了CentOS 7,想通过ftp上传文件,发现通过ifconfig,没有inet这个属性 查看ens33网卡的配置:vi /etc/syscon ...

  6. OC数组和字典中存入niu值

    在NSArray和NSDictionary中nil有特殊的含义.但是某些时候,我们必须要放入nil怎么办? 要想放入nil就必须用到一个类NSNull,这个类只有一个类方法,就是null.[NSNul ...

  7. WPF 漏斗控件 等待沙漏效果

    由于WPF中不支持gif图片因此要实现一个漏斗沙漏效果有点小麻烦. 网上有一款开源的控件 理论上完全开源 官网 http://wpfspark.codeplex.com/贴一下效果图 大家感觉需要就在 ...

  8. BZOJ3573: [Hnoi2014]米特运输(树上乱搞)

    Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 1669  Solved: 1031[Submit][Status][Discuss] Descript ...

  9. Java基础5一数组的常见应用算法

    常用算法 1.冒泡排序: 原理:比较两个相邻的元素,将值大的元素交换至右端 示例: public static void bubbleSort(int[] a) { int n = a.length; ...

  10. 09.javaweb简单标签编程

    一.简单标签 1,  简介:由于传统标签使用三个标签接口来完成不同的功能,显得过于繁琐,不利于标签技术的推广, SUN公司为降低标签技术的学习难度,在JSP 2.0中定义了一个更为简单.便于编写和调用 ...