zoukankan      html  css  js  c++  java
  • hdu 4786(生成树)

    Fibonacci Tree

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 4028    Accepted Submission(s): 1252


    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
    题意:n个点m条边,其中有一些边是白边,一些边是黑边,问是否存在一棵树上的白边数量是斐波拉契数列里面的某个数.
    题解:很巧妙的思想,先按白边排序将白边最多的树选出来,然后黑边排序将白边最少的树选出来。然后如果有斐波拉契数在两棵树的大小中间(因为如果存在的话,是可以通过删边得到的),如果存在,就Ok,还要判一下连通分量。
    #include <stdio.h>
    #include <algorithm>
    #include <string.h>
    #include <math.h>
    #include <queue>
    using namespace std;
    const int N = 100005;
    int father[N];
    struct Edge{
        int u,v,color;
    }edge[N];
    int _find(int x){
        if(x!=father[x]) {
            father[x] = _find(father[x]);
        }
        return father[x];
    }
    
    int n,m;
    int cmp(Edge a,Edge b){
        return a.color>b.color;
    }
    int cmp1(Edge a,Edge b){
        return a.color<b.color;
    }
    int kruskal(){
        int cost=0;
        for(int i=0;i<m;i++){
            int x=_find(edge[i].u);
            int y=_find(edge[i].v);
            if(x!=y){
                father[x] = y;
                cost+=edge[i].color;
            }
        }
       return cost;
    }
    bool vis[N];
    void init(){
        memset(vis,false,sizeof(vis));
        int a=1,b=2;
        vis[1]=true,vis[2] =true;
        while(a+b<N){
            vis[a+b]=true;
            swap(a,b);
            b = a+b;
        }
    }
    int main()
    {
        int tcase;
        scanf("%d",&tcase);
        int t = 1;
        init();
        while(tcase--){
            scanf("%d%d",&n,&m);
            for(int i=0;i<m;i++){
                scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].color);
            }
            for(int i=1;i<=n;i++) father[i] = i;
            sort(edge,edge+m,cmp);
            int maxn = kruskal();
            int ans = 0;
            for(int i=1;i<=n;i++){
                if(father[i]==i) ans++;
                if(ans>1) break;
            }
            if(ans>1) {
                printf("Case #%d: No
    ",t++);
                continue;
            }
            for(int i=1;i<=n;i++) father[i] = i;
            sort(edge,edge+m,cmp1);
            int minn = kruskal();
            ans = 0;
            for(int i=1;i<=n;i++){
                if(father[i]==i) ans++;
                if(ans>1) break;
            }
            if(ans>1) {
                printf("Case #%d: No
    ",t++);
                continue;
            }
            //printf("%d %d
    ",minn,maxn);
            bool flag = false;
            for(int i=minn;i<=maxn;i++){
                if(vis[i]){
                    flag = true;
                    break;
                }
            }
            if(flag) printf("Case #%d: Yes
    ",t++);
            else printf("Case #%d: No
    ",t++);
        }
        return 0;
    }
  • 相关阅读:
    对话框
    枚举、联合
    WinCE 应用程序开机自启动方法
    调用directshow出现链接错误
    修改了WINCE自带的驱动程序后如何编译
    如何设置WINCE系统字体、字号?如何设置自己开发的软件的字体、字号
    在不采用硬件计时器的情况下如何创建更精确的计时器
    驱动程序如何发通知给应用程序
    如何得到WAV文件播放的总时间
    解决CE6和CE5在Platform Builder的Connectivity Options上的冲突
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5492112.html
Copyright © 2011-2022 走看看