zoukankan      html  css  js  c++  java
  • 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
     给了一个无向图..每个边要么是白的.要么是黑的..问能否构造一个生成树..让白边在生成树的个数为fibonacci数...
    题解:
    这个题就是求一遍最小的生成树,
    求一遍最大的生成树
    两个中间是不是有斐波那契数
    最后还有判一下联通;
    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
    ",s1,s2);
     81         gf();
     82         ans=0;
     83         if(s1<0||s2<0){
     84             printf("Case #%d: No
    ",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
    ",flot);
     97         else printf("Case #%d: No
    ",flot);
     98     }
     99     return 0;
    100 }
  • 相关阅读:
    HashMap 实现原理
    王东江网站
    网站建设
    mysql 查询 执行流程
    两个线程交替打印1到100
    三个线程交替打印十次ABC
    Java动态链接是什么意思
    双亲委派机制
    笔记
    redis集群搭建
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4725600.html
Copyright © 2011-2022 走看看