POJ 4786 Fibonacci Tree

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

2013 Asia Chengdu Regional Contest

解题:最小生成树Kruskal思想。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 const int maxn = 100010;
18 struct arc {
19     int u,v,c;
20 };
21 int uf[maxn],n,m,cnt;
22 int fib[50] = {1,2};
23 arc e[maxn];
24 void init() {
25     for(int i = 2; i < 30; i++)
26         fib[i] = fib[i-1] + fib[i-2];
27 }
28 bool cmp1(const arc &x,const arc &y) {
29     return x.c < y.c;
30 }
31 bool cmp2(const arc &x,const arc &y) {
32     return x.c > y.c;
33 }
34 int Find(int x) {
35     if(x == uf[x]) return uf[x];
36     return uf[x] = Find(uf[x]);
37 }
38 int kruskal(bool flag) {
39     for(int i = 0; i <= n; i++) uf[i] = i;
40     int k = cnt = 0;
41     if(flag) sort(e,e+m,cmp1);
42     else sort(e,e+m,cmp2);
43     for(int i = 0; i < m; i++) {
44         int tx = Find(e[i].u);
45         int ty = Find(e[i].v);
46         if(tx != ty) {
47             uf[tx] = ty;
48             if(e[i].c) k++;
49             cnt++;
50         }
51     }
52     return k;
53 }
54 int main() {
55     int t,x,y,cs = 1;
56     init();
57     scanf("%d",&t);
58     while(t--) {
59         scanf("%d %d",&n,&m);
60         for(int i = 0; i < m; i++)
61             scanf("%d %d %d",&e[i].u,&e[i].v,&e[i].c);
62         x = kruskal(true);
63         if(cnt < n-1) {
64             printf("Case #%d: No\n",cs++);
65             continue;
66         }
67         y = kruskal(false);
68         int *tmp = lower_bound(fib,fib+30,x);
69         if(*tmp >= x && *tmp <= y)
70             printf("Case #%d: Yes\n",cs++);
71         else printf("Case #%d: No\n",cs++);;
72     }
73     return 0;
74 }

时间: 2024-10-16 20:10:23

POJ 4786 Fibonacci Tree的相关文章

hdoj 4786 Fibonacci Tree 【生成树+想法】

题目:hdoj 4786 Fibonacci Tree 题意:给出 n 个点 m 条边的图,边只有两种颜色,白色和黑色,让你判断能不能让一个生成树中白边的个数为斐波那契数. 分析:这是个想法题目,前提是知道生成树的定义:生成树必须是所有点都在树中 那么既然要是斐波那契数,我只要把白色边的最大个数和最小个数求出来,如果这个范围内有斐波那契数的话,那么就满足条件. 当然这样求的前提条件是期间的所有的生成树都是满足条件的.即都是满足能够生成树的. ok,AC代码: #include<iostream>

HDU 4786 Fibonacci Tree 最小生成树变形

思路: 这题比赛的时候想了好久,最后队友机智的想到了. 不过那时不是我敲的,现在敲的1A. 想好就容易了. 直接把1或者0当做边的权值,然后按边从小到大排序,然后算最小生成用到了几条白边,然后再按边从大到小排序,然后再算白边用了几条.然后最小和最大需要用到的白边都算出来了.如果在这最小最大区间中存在那个啥数列的话就是Yes,否则就是No. 为什么在这区间里面就是对的呢?刚开始我也想了好久,然后发现,因为白边权值是1,然后黑边是0,然后假设用到白边最小的是6,最大的是10,那么,我们可以用黑边去替

HDU 4786 Fibonacci Tree(生成树,YY乱搞)

http://acm.hdu.edu.cn/showproblem.php?pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1733    Accepted Submission(s): 543 Problem Description Coach Pang is interested in

Hdoj 4786 Fibonacci Tree 【kruskal】

Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2588 Accepted Submission(s): 822 Problem Description Coach Pang is interested in Fibonacci numbers while Uncle Yang wants him to do s

HDOJ 4786 Fibonacci Tree

最大生成树夹最小生成树,老题目了,依稀记得当年在成都靠这题捡了个铜..... Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1572    Accepted Submission(s): 479 Problem Description Coach Pang is interested in Fibonac

HDU 4786 Fibonacci Tree 并查集+生成树=kruskal

Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2149    Accepted Submission(s): 682 Problem Description Coach Pang is interested in Fibonacci numbers while Uncle Yang wants him t

hdu 4786 Fibonacci Tree(最小生成树)

Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2952    Accepted Submission(s): 947 Problem Description Coach Pang is interested in Fibonacci numbers while Uncle Yang wants him t

hdu 4786 Fibonacci Tree ( 最小生成树 )

Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2487    Accepted Submission(s): 796 Problem Description Coach Pang is interested in Fibonacci numbers while Uncle Yang wants him t

HDU 4786 Fibonacci Tree 生成树

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4786 题意:有N个节点(1 <= N <= 10^5),M条边(0 <= M <= 10^5).当中一部分边被染成了黑色,剩下的边是白色,问能不能建立一棵树,树中有斐波那契数个白色边. 思路:用克鲁斯卡尔建三次树,第一是用全部边建树.推断能否建成一棵树,第二次用黑边建树,最多能够用到x条黑边(不成环),n-1-x就是最少须要用的白边的数量,第三次用白边建树,最多能够用到y条白边.假设在[y