AtCoder - 2061 Tree Restoring
Problem Statement
Aoki loves numerical sequences and trees.
One day, Takahashi gave him an integer sequence of length N, a1,a2,…,aN, which made him want to construct a tree.
Aoki wants to construct a tree with N vertices numbered 1 through N, such that for each i=1,2,…,N, the distance between vertex i and the farthest vertex from it is ai, assuming that the length of each edge is 1.
Determine whether such a tree exists.
Constraints
- 2≦N≦100
- 1≦ai≦N−1
Input
The input is given from Standard Input in the following format:
N
a1 a2 … aN
Output
If there exists a tree that satisfies the condition, print Possible
. Otherwise, print Impossible
.
Sample Input 1
5
3 2 2 3 3
Sample Output 1
Possible
The diagram above shows an example of a tree that satisfies the conditions. The red arrows show paths from each vertex to the farthest vertex from it.
Sample Input 2
3
1 1 2
Sample Output 2
Impossible
Sample Input 3
10
1 2 2 2 2 2 2 2 2 2
Sample Output 3
Possible
Sample Input 4
10
1 1 2 2 2 2 2 2 2 2
Sample Output 4
Impossible
Sample Input 5
6
1 1 1 1 1 5
Sample Output 5
Impossible
Sample Input 6
5
4 3 2 3 4
Sample Output 6
Possible
把直径构造出来,然后讨论讨论就好了
#include<bits/stdc++.h> #define ll long long using namespace std; const int maxn=205; int cnt[maxn],n,now,M; int main(){ scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&now); M=max(M,now); cnt[now]++; } for(int i=M;i>M-i;i--) if(cnt[i]<2){ puts("Impossible"); return 0;} if(!(M&1)&&cnt[M>>1]!=1){ puts("Impossible"); return 0;} if((M&1)&&cnt[M-(M>>1)]!=2){ puts("Impossible"); return 0;} puts("Possible"); return 0; }