zoukankan      html  css  js  c++  java
  • AT2061 Tree Restoring (树的直径)

    https://www.luogu.org/problemnew/show/AT2061

    题意:

    给你每个点到最远点的距离,求是否存在这样的一颗树。

    n<=200。

    思路:

    一个性质是每个点的最远点一定是直径的端点。 
    然后我们找出直径长度,要求能构造出这条直径(每种距离都有2个点,当然直径中心只需要1个)。 
    然后其余点都可以直接接在直径上,因此到最远点的距离不能少于直径长度的一半。 
    满足就能构造。

     分奇偶画几个图就好了

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    int n,a[104],num[104],flag=0;
    int main(){
        cin>>n;int maxx=0;
        for(int i=1;i<=n;i++){
            cin>>a[i];
            maxx=max(maxx,a[i]);
            num[a[i]]++;
        }
        for(int i=maxx;i>maxx/2;i--){
            if(num[i]<2){
                flag=1;
                break;
            }num[i]-=2;
        }
        if((maxx&1)==0){
            if(num[maxx>>1]) num[maxx>>1]--;
            else flag=1;
        }
        if(flag) cout<<"Impossible";
        else{
            for(int i=1;i<=(maxx+1)/2;i++){
                if(num[i]){cout<<"Impossible";return 0;}
            }
            cout<<"Possible";
        }
    }
  • 相关阅读:
    JVM内存模型与类加载机制
    JS 实现动态轮播图
    Jedis & spring-data-redis
    JAVA反射机制与动态代理
    JavaScript -- 筑基
    IO流与装饰者模式
    ES&IK环境搭建
    Elasticsearch笔记
    DQL
    DDL--DML
  • 原文地址:https://www.cnblogs.com/wifimonster/p/10227601.html
Copyright © 2011-2022 走看看