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\),就把他删去,然后所有位置前移.问最后剩下的串长. 题解 ...
随机推荐
- java中的日期处理
学习Java日期处理,看见这一篇比较详细,转载之. 转自:http://www.cnblogs.com/hqr9313/archive/2012/04/19/2458221.html 时间日期1) ...
- Iphone CPU 架构类型
armv6设备:iPhone, iPhone2, iPhone 3G,第一代.第二代iPod Touch armv7设备:iPhone 3GS, iPhone 4, iPhone 4S iPad , ...
- linux下IPC通信
# 管道( pipe ):管道是一种半双工的通信方式,数据只能单向流动,而且只能在具有亲缘关系的进程间使用.进程的亲缘关系通常是指父子进程关系. # 有名管道 (named pipe) : 有名管道也 ...
- [转]整理jquery开发技巧
1.创建一个嵌套的过滤器 1.$(jquery).filter(":not(:has(.selected))") //去掉所有不包含class为.selected的元素 2.使 ...
- Leetcode 136 137 260 SingleNumber I II III
Leetccode 136 SingleNumber I Given an array of integers, every element appears twice except for one. ...
- 仿bmfn 底部
<!DOCTYPE html> <!-- saved from url=(0019)http://www.bmfn.cn/ --> <html class="k ...
- updateMany
db.tblDaily.updateMany( {"Comments.ViewCount":0}, {$addToSet:{"Comments.$.CommentDate ...
- ArrayList和LinkedList和Vector源码分析
ArrayList源码: private static final int DEFAULT_CAPACITY = 10;//默认长度 /** * Shared empty array instance ...
- SharePoint 2013 字段属性之JSLink 转载来源(http://www.cnblogs.com/jianyus/p/3544482.html)
在SharePoint 2013中,SPField新增加了一个属性是JSLink,使用客户端脚本修改字段前台展示,我们可以用很多方法修改这个脚本的引用,然后来修改脚本,下面,我们举一个简单的例子. 具 ...
- 关于ios 程序加载百度地图lib,出现链接错误:找不到符号 (null): _OBJC_CLASS_$_BMKMapManager的解决办法
报告的错误信息 ld: warning: ignoring file /Users/5012/Documents/sphuang/IOS_project/baidu_map/ShareLocation ...