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 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+10;
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n,m,k,t;
inline ll read()
{
    ll x=0;int f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int a[maxn],ma,vis[maxn];
bool flag;
int main()
{
    int i,j;
    scanf("%d",&n);
    rep(i,1,n)scanf("%d",&a[i]),vis[a[i]]++,ma=max(ma,a[i]);
    if(ma%2==0)
    {
        rep(i,1,ma/2-1)if(vis[i])flag=true;
        rep(i,ma/2,ma)
        {
            if(i==ma/2)
            {
                if(vis[i]!=1)flag=true;
            }
            else if(vis[i]<2)flag=true;
        }
    }
    else
    {
        rep(i,1,(ma+1)/2-1)if(vis[i])flag=true;
        rep(i,(ma+1)/2,ma)
        {
            if(i<(ma+1)/2&&vis[i])flag=true;
            if(i==(ma+1)/2)
            {
                if(vis[i]!=2)flag=true;
            }
            else if(vis[i]<2)flag=true;
        }
    }
    if(flag)puts("Impossible");
    else puts("Possible");
    //system("Pause");
    return 0;
}

转载于:https://www.cnblogs.com/dyzll/p/5927546.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当程序在加载检查点时失败并出现"failed to load checkpoint, restoring previous"的错误信息时,意味着程序无法成功加载检查点,并正在恢复到先前保存的状态。 检查点是在机器学习或深度学习训练过程中定期保存的模型的快照。它通常用于在训练过程中定期保存模型的参数和优化器的状态,以便在需要时恢复训练过程。 "failed to load checkpoint"的错误信息表示加载检查点时出现了问题,可能是由于以下原因之一: 1. 检查点文件丢失或损坏:如果检查点文件在加载过程中丢失或损坏,程序将无法成功加载检查点。 2. 版本不兼容:如果使用的程序或库的版本与保存检查点时的版本不兼容,也可能导致加载失败。 在出现这个错误时,程序会尝试从先前保存的状态中进行恢复,这是为了最大限度地减少训练过程中的数据丢失。 为了解决这个问题,可以尝试以下步骤: 1. 检查检查点文件:确保检查点文件存在并且完好无损。如果文件丢失或损坏,你可能需要寻找其他备份或重新训练模型。 2. 检查版本兼容性:确保使用的程序和库的版本与保存检查点时的版本相匹配。如果版本不兼容,可以尝试更新程序或库,或者重新保存检查点。 最后,如果以上步骤都没有解决问题,你可能需要查找相关的错误消息和日志来获取更多的信息,或者在相关的社区或论坛上寻求帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值