AT2061 Tree Restoring (树的直径)

it2022-05-09  24

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"; } }

 

转载于:https://www.cnblogs.com/wifimonster/p/10227601.html

相关资源:数据结构—成绩单生成器

最新回复(0)