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. swift初始化

    swift初始化 class INIT: NSObject { // 一个结构体的初始化 // 1.存储属性的初始化 struct Fahrenheit { var temperature :Doub ...

  2. 黑马day01xml 解析方式与原理分析

    dom解析方式和sax解析

  3. linux中字符串转换函数 simple_strtoul

    Linux内核中提供的一些字符串转换函数: lib/vsprintf.c 1. unsigned long long simple_strtoull(const char *cp, char **en ...

  4. 【HDU 1846】 Brave Game

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1846 [算法] 巴什博弈 若有(m+1)个石子,显然先手不能直接取完,后手必胜 因此,我们可以把石 ...

  5. Java登录界面简单设计

    package cn.com.view; import java.awt.Color; import java.awt.Font; import java.awt.SystemColor; impor ...

  6. Codeforces 680D Bear and Tower of Cubes 贪心 DFS

    链接 Codeforces 680D Bear and Tower of Cubes 题意 求一个不超过 \(m\) 的最大体积 \(X\), 每次选一个最大的 \(x\) 使得 \(x^3\) 不超 ...

  7. 如何在在页面中清除一个已知的cookie?

    前些天在写一个项目的时候,使用cookie来存储一些用户数据,在用户登出时需要清理以往的数据,对于一个初学者来说,我需要学习如何清除一个已知的cookie. 首先,引入两个js文件: 1.jquery ...

  8. window下安装svn

    下载 http://subversion.apache.org/ 注意:上边的黑窗口不要关闭! 如何校验svn服务有运行

  9. NFA

    任意正则表达式都存在一个与之对应的NFA,反之亦然. 正则表达式 ((A*B|AC)D)对应的NFA(有向图), 其中红线对应的为该状态的ε转换, 黑线表示匹配转换 我们定义的NFA具有以下特点: 正 ...

  10. chrome打开控制台状态下,没有人为打断点,自动进入断点模式的解决方法

    如下图所示:在控制台去掉Sources -> XHR/fetch Breakpoints -> Any XHR or fetch 的勾