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;
}
AtCoder - 2061 Tree Restoring的更多相关文章
- Tree Restoring
Tree Restoring Time limit : 2sec / Memory limit : 256MB Score : 700 points Problem Statement Aoki lo ...
- ZOJ 3965 Binary Tree Restoring
Binary Tree Restoring 思路: 递归 比较a序列和b序列中表示同一个子树的一段区间,不断递归 代码: #include<bits/stdc++.h> using nam ...
- zoj 3965 Binary Tree Restoring(搜索)
Binary Tree Restoring Time Limit: 1 Second Memory Limit: 65536 KB Special Judge Given two ...
- AtCoder Grand Contest 005 C - Tree Restoring
题目传送门:https://agc005.contest.atcoder.jp/tasks/agc005_c 题目大意: 给定一个长度为\(N\)的整数序列\(A_i\),问能否构造一个\(N\)个节 ...
- 2017浙江省赛 H - Binary Tree Restoring ZOJ - 3965
地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3965 题目: iven two depth-first-search ...
- AtCoder AGC030B Tree Burning
题目链接 https://atcoder.jp/contests/agc030/tasks/agc030_b 题解 细节好题.. 首先假设第一步往右走,那么可以发现当拐弯的次数一定时路径是唯一的 于是 ...
- [AGC005C]Tree Restoring 构造
Description 给出一个数组a,要求构造一颗树,使节点x距离最远的点的距离为\(a_x\). Input 第一行一个正整数NN(2≤N≤1002≤N≤100) 接下来一行,有NN个 ...
- ZOJ3965 Binary Tree Restoring
ZOJ3965 给定一颗二叉树的两种DFS序列 输出一种可能的二叉树的结构. 考察树的递归性质,不要想的太复杂. 当前节点在两个串中后面的节点假如不同则能确认两个子树,如果相同则把下个点作当前点的一个 ...
- AtCoder Grand Contest 005
AtCoder Grand Contest 005 A - STring 翻译 给定一个只包含\(ST\)的字符串,如果出现了连续的\(ST\),就把他删去,然后所有位置前移.问最后剩下的串长. 题解 ...
随机推荐
- 【BZOJ 3195 】[Jxoi2012]奇怪的道路 装压dp
受惯性思维的影响自动把二进制状态认为是连与不连......... 我们这里二进制状态表示的是奇偶,这样的话我们f[i][j][k]表示的就是前i个城市用了j个边他前k个城市的奇偶状态,然后想想怎么转移 ...
- Ubuntu下安装LNMP之nginx的卸载
我在安装Nginx时,是采用自己从网上down下自己需要的nginx版本进行编译安装的,如果使用过apt库来进行安装的话可以参考这篇文章:ubuntu中彻底删除nginx 假如是编译安装的童鞋,可以按 ...
- Codeforces Round #520 (Div. 2) D. Fun with Integers
D. Fun with Integers 题目链接:https://codeforc.es/contest/1062/problem/D 题意: 给定一个n,对于任意2<=|a|,|b|< ...
- xmlns:sys="clr-namespace:System;assembly=mscorlib" NOTE: System;与assembly中间不能有空格
xmlns:sys="clr-namespace:System;assembly=mscorlib" NOTE: System;与assembly中间不能有空格 否则报错, Er ...
- The 'brew link' step did not complete successfully
在mac 上更新node时遇到了一系列的问题: 卸载node重新安装之后提示: The 'brew link' step did not complete successfully 其实这里已经给出了 ...
- [USACO07FEB]新牛棚Building A New Barn
洛谷题目链接:[USACO07FEB]新牛棚Building A New Barn 题目描述 After scrimping and saving for years, Farmer John has ...
- HDU1847 Good Luck in CET-4 Everybody!
大学英语四级考试就要来临了,你是不是在紧张的复习?也许紧张得连短学期的ACM都没工夫练习了,反正我知道的Kiki和Cici都是如此.当然,作为在 考场浸润了十几载的当代大学生,Kiki和Cici更懂得 ...
- Linux 工作站安全加固规范
目标受众 这是一套 Linux 基金会为其系统管理员提供的推荐规范. 这个文档用于帮助那些使用 Linux 工作站来访问和管理项目的 IT 设施的系统管理员团队. 如果你的系统管理员是远程员工,你也许 ...
- windows支持applocker的版本
Operating system requirements The following table show the on which operating systems AppLocker fe ...
- Charles Android 抓包失败SSLHandshake: Received fatal alert: certificate_unknown
前提: Android使用Charles抓取Https请求的报文时,Android和Charles都正确安装了证书之后出现抓包失败,报错SSLHandshake: Received fatal ale ...