• 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;
    }
  • 相关阅读:
    kickstart自动安装部署RHEL7
    物流即使查询API
    快递单号查询快递鸟API接口-100家快递单轨迹推送
    物流跟踪API-快递单订阅
    如何最快实现物流即使查询功能-物流轨迹查询API
    提供一个不错的物流物流接口给大家,本人亲测,真的不错
    给idea添加类注释和方法注释模板
    分享一个生成反遗忘复习计划的java程序
    用TreeSet和Comparator给list集合元素去重
    对poi-excel导出的浅层理解
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5492112.html
Copyright © 2020-2023  润新知