Tree Restoring
Tree Restoring
Time limit : 2sec / Memory limit : 256MB
Score : 700 points
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.
分析:对于一棵树来说,假设直径有两个端点a,b,那么任意一点到其他点最远距离必然是max(dist(p,a),dist(p,b)),
那么根据直径来构树,以树直径为奇数举例,那么这条链上必然有偶数个点,且最远距离为k,k-1,...,(k+1)/2,(k+1)/2...,k-1,k;
那么也就是不存在最远距小于(k+1)/2的点,且(k+1)/2有两个点,大于(k+1)/2的至少有2个;
树直径为偶数时同理;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=1e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t;
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int a[maxn],ma,vis[maxn];
bool flag;
int main()
{
int i,j;
scanf("%d",&n);
rep(i,,n)scanf("%d",&a[i]),vis[a[i]]++,ma=max(ma,a[i]);
if(ma%==)
{
rep(i,,ma/-)if(vis[i])flag=true;
rep(i,ma/,ma)
{
if(i==ma/)
{
if(vis[i]!=)flag=true;
}
else if(vis[i]<)flag=true;
}
}
else
{
rep(i,,(ma+)/-)if(vis[i])flag=true;
rep(i,(ma+)/,ma)
{
if(i<(ma+)/&&vis[i])flag=true;
if(i==(ma+)/)
{
if(vis[i]!=)flag=true;
}
else if(vis[i]<)flag=true;
}
}
if(flag)puts("Impossible");
else puts("Possible");
//system("Pause");
return ;
}
Tree Restoring的更多相关文章
- 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 ...
- 2017浙江省赛 H - Binary Tree Restoring ZOJ - 3965
地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3965 题目: iven two depth-first-search ...
- AtCoder - 2061 Tree Restoring
Problem Statement Aoki loves numerical sequences and trees. One day, Takahashi gave him an integer s ...
- [AGC005C]Tree Restoring 构造
Description 给出一个数组a,要求构造一颗树,使节点x距离最远的点的距离为\(a_x\). Input 第一行一个正整数NN(2≤N≤1002≤N≤100) 接下来一行,有NN个 ...
- AtCoder Grand Contest 005 C - Tree Restoring
题目传送门:https://agc005.contest.atcoder.jp/tasks/agc005_c 题目大意: 给定一个长度为\(N\)的整数序列\(A_i\),问能否构造一个\(N\)个节 ...
- ZOJ3965 Binary Tree Restoring
ZOJ3965 给定一颗二叉树的两种DFS序列 输出一种可能的二叉树的结构. 考察树的递归性质,不要想的太复杂. 当前节点在两个串中后面的节点假如不同则能确认两个子树,如果相同则把下个点作当前点的一个 ...
- Red–black tree ---reference wiki
source address:http://en.wikipedia.org/wiki/Red%E2%80%93black_tree A red–black tree is a type of sel ...
- AtCoder Grand Contest 005
AtCoder Grand Contest 005 A - STring 翻译 给定一个只包含\(ST\)的字符串,如果出现了连续的\(ST\),就把他删去,然后所有位置前移.问最后剩下的串长. 题解 ...
随机推荐
- PHP 时间与字符串的相互转化
1.求两个日期的差数,例如2007-3-5 ~ 2007-3-6 的日期差数 echo abs(strtotime("2007-3-5") - strtotime("20 ...
- POJ 1042 Gone Fishing#贪心
(- ̄▽ ̄)-* #include<iostream> #include<cstdio> #include<cstring> using namespace std ...
- OMCS开发手册(02) -- 多媒体连接器
OMCS开发手册(01) -- 多媒体设备管理器 一文,我们从Owner的角度详细描述了多媒体设备管理器的使用,本文我们将站在Guest的角度,描述OMCS中另一类组件/控件:多媒体连接器.多媒体连接 ...
- 使用Cookie来统计浏览次数,当天重复刷新不增加
这是一种不严谨的做法,在浏览量不是很重要的时候可以使用 var oldCookie = Request.Cookies["newsCookie"]; if (oldCookie = ...
- vultr vps注册和后台教程
我用了一年多vultr vps,感觉相当满意.我算是vultr最早的一批用户了,当时他们充值多少赠送多少,吸引大批新用户搬家到vultr,给linode和digitalocean vps不少的市场压力 ...
- Fiddler AutoResponder正则替换
今天感冒,写简单些. Fiddler AutoResponder正则替换: regex:(?inx).+20150826_1_1_386.mp4/playlist.m3u8.*$ .表示任意字符 *: ...
- pickView不需要明确设置高度
pickView不用设置高度,若设置了,在iOS8.4.1上不会被填充,会造成没有把pickView高度设置成你想要的,但是布局其他地方的时候却会从你设置的地方开始,就会造成布局不紧挨着或者遮盖着的效 ...
- PMP培训感想
终于等到了C打头的邮件,等了几个星期,悬着的心终于放了.1P4M,基本跟平时模块成绩一致.当初模考时,第一次考了126,还算比较满意,毕竟还没开始苦逼看嘛,悲剧的是不管后面再怎么努力,成绩始终在1 ...
- HDU 5795 A Simple Nim
打表找SG函数规律. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> ...
- C语言_数字排列顺序
#include <stdio.h> #include <stdlib.h> #define LENGTH 8 void main() { , , , , , , , }; ; ...