codeforces D. Numbers on Tree nodes of the current node in the subtree smaller than - to the point number

Topic link: https: //codeforces.com/contest/1287/problem/D
subject to the effect:
give you a tree, each of a number of a [i] and c [i]. c [i]: all child node i j satisfy a [i]> a [j ] is the number of nodes. Now you ancestor nodes n and c [i] allows you to assign a [i] (1 <= a [i] <= 1e9).
If the output can not be allocated NO, YES may be output if the output of each point and a [i].
Here Insert Picture Description
Here Insert Picture Description

#include <bits/stdc++.h>
#define LL long long
using namespace std;
 
struct node{
    int to;
    int c;
};
vector <node> v[2005];
int vis[2005], f=0;
int pos[2005];
 
vector<int> dfs(int u, int fa){
 
    vector<int> a;
    for(int i=0; i<v[u].size(); i++){
        int to=v[u][i].to;
        if(to!=fa){
            vector<int> b=dfs(to, u);
            for(int i=0; i<b.size(); i++){
                a.push_back(b[i]);
            }
        }
    }
    if(vis[u]>a.size()){//NO
        f=1;
    }
    else{//插入到子节点序列中
        a.insert(a.begin()+vis[u], u);
    }
    return a;
}
 
int main()
{
 
    int n, root;
    scanf("%d", &n);
    for(int i=1; i<=n; i++){
        int a, b;
        scanf("%d%d", &a, &b);
        if(a){
            v[i].push_back(node{a, b});
            v[a].push_back(node{i, b});
        }
        else{
            root=i;
        }
        vis[i]=b;
    }
    vector <int> a=dfs(root, -1);
 
    if(f){
        printf("NO\n");
        return 0;
    }
 
    int p=1;
    for(int i=0; i<a.size(); i++){//编号
        pos[a[i]]=p++;
    }
 
    printf("YES\n");
    for(int i=1; i<=n; i++){
        printf("%d ", pos[i]);
    }
 
    return 0;
}
Published 374 original articles · won praise 22 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_21433411/article/details/103866000