[HDOJ4786]Fibonacci Tree 最小生成树

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4786

先跑一遍最小生成树,注意判断是否已全部联通(用一个记号来统计最后生成树中有多少条边)。再记下最小生成树的权值和A。

再反向排序,求一遍最大生成树。记下权值和B。问题转换成求[A,B]内是否有斐波那契数存在。

  1 #include <algorithm>
  2 #include <iostream>
  3 #include <iomanip>
  4 #include <cstring>
  5 #include <climits>
  6 #include <complex>
  7 #include <fstream>
  8 #include <cassert>
  9 #include <cstdio>
 10 #include <bitset>
 11 #include <vector>
 12 #include <deque>
 13 #include <queue>
 14 #include <stack>
 15 #include <ctime>
 16 #include <set>
 17 #include <map>
 18 #include <cmath>
 19
 20 using namespace std;
 21
 22 typedef struct P {
 23     int u;
 24     int v;
 25     int w;
 26 }P;
 27
 28 bool cmp1(P p1, P p2) {return p1.w < p2.w;}
 29 bool cmp2(P p1, P p2) {return p1.w > p2.w;}
 30 int A, B;
 31
 32 const int maxn = 100010;
 33 int n, m, cnt;
 34 int pre[maxn];
 35 int fib[maxn];
 36 P e[maxn];
 37
 38 int find(int x) {
 39     return x == pre[x] ? pre[x] : pre[x] = find(pre[x]);
 40 }
 41
 42 bool unite(int x, int y) {
 43     x = find(x);
 44     y = find(y);
 45     if(x != y) {
 46         pre[x] = y;
 47         return 1;
 48     }
 49     return 0;
 50 }
 51
 52 void init() {
 53     for(int i = 0; i < maxn; i++) {
 54         pre[i] = i;
 55     }
 56 }
 57
 58 void clear() {
 59     init();
 60     memset(e, 0, sizeof(e));
 61     A = 0, B = 0;
 62     cnt = 0;
 63 }
 64
 65 int main() {
 66     // freopen("in", "r", stdin);
 67     int T;
 68     fib[0] = 0, fib[1] = 1, fib[2] = 1;
 69     for(int i = 3; i <= 26; i++) {
 70         fib[i] = fib[i-1] + fib[i-2];
 71     }
 72     scanf("%d", &T);
 73     for(int _ = 1; _ <= T; _++) {
 74         printf("Case #%d: ", _);
 75         clear();
 76         scanf("%d %d", &n, &m);
 77         for(int i = 0; i < m; i++) {
 78             scanf("%d %d %d", &e[i].u, &e[i].v, &e[i].w);
 79         }
 80         sort(e, e+m, cmp1);
 81         for(int i = 0; i < m; i++) {
 82             if(unite(e[i].u, e[i].v)) {
 83                 A += e[i].w;
 84                 cnt++;
 85             }
 86         }
 87         if(cnt != n - 1) printf("No\n");
 88         else {
 89             init();
 90             sort(e, e+m, cmp2);
 91             for(int i = 0; i < m; i++) {
 92                 if(unite(e[i].u, e[i].v)) {
 93                     B += e[i].w;
 94                     cnt++;
 95                 }
 96             }
 97             int flag = 0;
 98             for(int i = 1; i <= 25; i++) {
 99                 if(fib[i] <= B && fib[i] >= A) {
100                     flag = 1;
101                     break;
102                 }
103             }
104             if(flag) printf("Yes\n");
105             else printf("No\n");
106         }
107     }
108 }
时间: 2024-10-26 20:00:27

[HDOJ4786]Fibonacci Tree 最小生成树的相关文章

HDU 4786 Fibonacci Tree 最小生成树变形

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

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 (最小生成树扩展)

///白边优先和黑边优先做两次最小生成树 ///若有斐波那契树在这中间为yes # include <stdio.h> # include <algorithm> # include <iostream> # include <string.h> # include <math.h> using namespace std; struct node { int x; int y; int v; }; struct node a[100010];

Fibonacci Tree(最小生成树,最大生成树)

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

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

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

POJ 4786 Fibonacci Tree

Fibonacci Tree Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 478664-bit integer IO format: %I64d      Java class name: Main Coach Pang is interested in Fibonacci numbers while Uncle Yang wants him to do som

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 【生成树+想法】

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