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 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

题解:

这个题就是求一遍最小的生成树,

求一遍最大的生成树

两个中间是不是有斐波那契数

最后还有判一下联通;

kruskal;

代码:

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<string.h>
  4 #include<algorithm>
  5 using namespace std;
  6 const int MAXN=100010;
  7 struct Node {
  8     int s,e,c;
  9 };
 10 Node dt[MAXN];
 11 int pre[MAXN];
 12 int M,t1,N;
 13 int cmp1(Node a,Node b){
 14     return a.c<b.c;
 15 }
 16 int cmp2(Node a,Node b){
 17     return a.c>b.c;
 18 }
 19 /*int cmp1(const void *a,const void *b){
 20     if((*(Node *)a).c<(*(Node *)b).c)return -1;
 21     else return 1;
 22 }
 23 int cmp2(const void *a,const void *b){
 24     if((*(Node *)a).c>(*(Node *)b).c)return -1;
 25     else return 1;
 26 }*/
 27 int find(int x){
 28     return pre[x]= x==pre[x]?x:find(pre[x]);
 29 }
 30 bool merge(Node a){
 31     if(!pre[a.s])pre[a.s]=a.s;
 32     if(!pre[a.e])pre[a.e]=a.e;
 33     int f1,f2;
 34     f1=find(a.s);f2=find(a.e);
 35     if(f1!=f2){
 36         pre[f1]=f2;
 37         t1++;
 38         if(a.c)return true;
 39     }
 40     return false;
 41 }
 42 int kruskal(){int tot=0;
 43         t1=1;
 44     for(int i=0;i<M;i++){
 45         if(merge(dt[i]))tot++;
 46     }
 47     if(t1==N)return tot;
 48     else return -1;
 49 }
 50 bool fp[MAXN];
 51 void gf(){
 52     int a,b,c=0;
 53     memset(fp,false,sizeof(fp));
 54     a=1;b=2;
 55     fp[a]=fp[b]=true;
 56     while(c<MAXN){
 57         c=a+b;
 58         fp[c]=true;
 59         a=b;
 60         b=c;
 61     }
 62 }
 63 int main(){
 64     int T,s1,s2,ans,flot=0;
 65     scanf("%d",&T);
 66     while(T--){
 67             flot++;
 68             memset(pre,0,sizeof(pre));
 69         scanf("%d%d",&N,&M);
 70         for(int i=0;i<M;i++){
 71             scanf("%d%d%d",&dt[i].s,&dt[i].e,&dt[i].c);
 72         }
 73        // qsort(dt,M,sizeof(dt[0]),cmp1);
 74        sort(dt,dt+M,cmp1);
 75         s1=kruskal();
 76         //qsort(dt,M,sizeof(dt[0]),cmp2);
 77         sort(dt,dt+M,cmp2);
 78         memset(pre,0,sizeof(pre));
 79         s2=kruskal();
 80         //printf("%d %d\n",s1,s2);
 81         gf();
 82         ans=0;
 83         if(s1<0||s2<0){
 84             printf("Case #%d: No\n",flot);
 85             continue;
 86         }
 87        //for(int i=0;i<100;i++)printf("fp[%d]=%d ",i,fp[i]);puts("");
 88         if(s1>s2){
 89             int q=s1;
 90             s1=s2;
 91             s2=q;
 92         }
 93         for(int i=s1;i<=s2;i++){
 94             if(fp[i])ans=1;
 95         }
 96         if(ans)printf("Case #%d: Yes\n",flot);
 97         else printf("Case #%d: No\n",flot);
 98     }
 99     return 0;
100 }
时间: 2024-12-21 14:39:27

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

[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 <

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];

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

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

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

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 生成树+贪心?

N个顶点,M条边,每条边可能为黑色或是白色( 0 or 1 ),问有没有可能用为斐波那契数的数目的白色边构成一棵生成树.所以需要删掉图中的环,根据每次删掉的边有一个白色边的上限和下限,判断一下中间有没有斐波那契数就可以了.实现方法是根据颜色排序,先放黑色边得到的是最小数目的白色边构成的生成树,先放白色边得到是最大数目的白色边构成的生成树. #include<cstring> #include<string> #include<fstream> #include<i

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