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 Na1,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≦aiN−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的更多相关文章

  1. ZOJ 3965 Binary Tree Restoring

    Binary Tree Restoring 思路: 递归 比较a序列和b序列中表示同一个子树的一段区间,不断递归 代码: #include<bits/stdc++.h> using nam ...

  2. zoj 3965 Binary Tree Restoring(搜索)

    Binary Tree Restoring Time Limit: 1 Second      Memory Limit: 65536 KB      Special Judge Given two ...

  3. 2017浙江省赛 H - Binary Tree Restoring ZOJ - 3965

    地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3965 题目: iven two depth-first-search ...

  4. AtCoder - 2061 Tree Restoring

    Problem Statement Aoki loves numerical sequences and trees. One day, Takahashi gave him an integer s ...

  5. [AGC005C]Tree Restoring 构造

    Description ​ 给出一个数组a,要求构造一颗树,使节点x距离最远的点的距离为\(a_x\). Input ​ 第一行一个正整数NN(2≤N≤1002≤N≤100) ​ 接下来一行,有NN个 ...

  6. AtCoder Grand Contest 005 C - Tree Restoring

    题目传送门:https://agc005.contest.atcoder.jp/tasks/agc005_c 题目大意: 给定一个长度为\(N\)的整数序列\(A_i\),问能否构造一个\(N\)个节 ...

  7. ZOJ3965 Binary Tree Restoring

    ZOJ3965 给定一颗二叉树的两种DFS序列 输出一种可能的二叉树的结构. 考察树的递归性质,不要想的太复杂. 当前节点在两个串中后面的节点假如不同则能确认两个子树,如果相同则把下个点作当前点的一个 ...

  8. 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 ...

  9. AtCoder Grand Contest 005

    AtCoder Grand Contest 005 A - STring 翻译 给定一个只包含\(ST\)的字符串,如果出现了连续的\(ST\),就把他删去,然后所有位置前移.问最后剩下的串长. 题解 ...

随机推荐

  1. HTML通过事件传递参数到js一

    原文链接:http://bbs.51cto.com/thread-1098421-1-1.html 目标处理函数为selectAttr(test) 1.直接传递给定参数如: onclick=" ...

  2. jQuery Post 提交内容中有标签报错

    Post编辑一点内容要传后台数据库: var html = editor2.html() console.log(encodeURIComponent(html)); //console.log(&q ...

  3. CF 602C The Two Routes(dij+邻接矩阵)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cmath> #include<algori ...

  4. jquery获取li中的各项属性值attr

    发布新内容时的设计 默认显示一个按钮 如:发布按钮(放在h3字体里面)(鼠标上面时.显示发布到哪个模块下拉菜单发在li里面) $('#pup_model li , #pup_model h3').cl ...

  5. C#读取和写入文件

    一.读取文件 如果你要读取的文件内容不是很多, 可以使用 File.ReadAllText(FilePath) 或指定编码方式 File.ReadAllText(FilePath, Encoding) ...

  6. 二维离散平稳小波分解swt2

    对信号X进行N尺度平稳小波分解 [A,H,V,D]=swt2(X,N,'wname'); clc,clear all,close all; load woman; [cA,cH,cV,cD]=swt2 ...

  7. ESFramework 4.0 进阶(01)-- 消息

    需要交互的分布式系统之间通过消息来传递有意义的信息.消息是通信框架的核心.离开了消息,再谈通信框架就没有任何意义,所以,消息是ESFramework中一个最核心的概念. 一. 消息的类别 在具体的应用 ...

  8. StretchBlt 提高图片显示质量的方法

    通过SetStretchMode(STRETCH_HALFTONE)实现,同时注意重新设置画刷起点 LRESULT CMainDlg::OnPaint(UINT uMsg, WPARAM wParam ...

  9. Objective-C中的instancetype与id的区别

    一.什么是instancetype instancetype是clang 3.5开始,clang提供的一个关键字,表示某个方法返回的未知类型的Objective-C对象.我们都知道未知类型的的对象可以 ...

  10. 【ADT】链表的基本C语言实现

    什么是抽象数据类型?首先,这一概念是软件开发人员在力求编写的代码健壮.易维护且可以复用的过程中产生的.英文是AbstractData Type.有人将其比作"抽象"的墙壁,&quo ...