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\),就把他删去,然后所有位置前移.问最后剩下的串长. 题解 ...
随机推荐
- git使用笔记(八)团队协作
By francis_hao Nov 24,2016 本文由 刘英皓 创作,采用 知识共享 署名-非商业性使用-相同方式共享 3.0 中国大陆 许可协议进行许可.欢迎转载,请注明出处 ...
- Ubuntu下安装LNMP之独立添加php扩展模块
使用php的过程中,发现某个扩展没有添加,又不想重新编译php,这个时候我们就需要单独添加需要的扩展模块. 下面以mysqli扩展模块为例,具体介绍安装步骤. 1.安装mysql 具体参考:Ubunt ...
- 打造适合日常使用的ubuntu,以ubuntu 16.04.1 LTS为例(不涉及版本)
因为调试些程序需要用到ubuntu,又不喜欢虚拟机,因此装了双系统,在这过程中因为各种原因ubuntu来回安装过好多次,每次安装到用得爽都要捣鼓很久,也算稍微有点经验心得,将ubuntu调教的过程写在 ...
- MySQL:BlackHole
MySQL:BlackHole 顾名思义BlackHole就是黑洞,只有写入没有输出.现在就来实验一下吧 首先查看一下MySQL支持的存储引擎 mysql> show engines;+---- ...
- wyh的天鹅~vector的使用
链接:https://www.nowcoder.com/acm/contest/93/L来源:牛客网 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 262144K,其他语言52428 ...
- codeforces 1077F1
题目:https://codeforces.com/contest/1077/problem/F1 题意: 你有n幅画,第i幅画的好看程度为ai,再给你两个数字k,x 表示你要从中选出刚好x幅画,并且 ...
- Linux Top 命令参数解析
转载自:http://www.jb51.net/LINUXjishu/34604.html TOP是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执行该命令,它将独占前台,直到用户 ...
- poj 1528 Perfection
题目链接:http://poj.org/problem?id=1528 题目大意:输入一个数n,然后求出约数的和sum,在与这一个数n进行比较,如果sum>n,则输出ABUNDANT,如果sum ...
- SAR图像处理 MSTAR数据库利用问题
本人最近刚接手的新活,打算从事SAR目标识别的研究,从美国的mstar数据库中下到一些资源,但是这些数据并不是原始图片的格式,需要转换,由于刚刚接触,并不太了解,所以特此请教!如有感兴趣的朋友可以一起 ...
- Appium===Appium+Python API(转)
Appium+python自动化8-Appium Python API 前言: Appium Python API全集,不知道哪个大神整理的,这里贴出来分享给大家. 1.contexts contex ...