Cod sursa(job #2542168)

Utilizator alex_bb8Banilean Alexandru-Ioan alex_bb8 Data 9 februarie 2020 16:59:48
Problema Cerere Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("cerere.in");
ofstream g("cerere.out");


const int nmax=100005;
vector<int>G[nmax];
int n,d[nmax],fath[nmax],poz,sol[nmax];
bool viz[nmax];
vector<int>S;


void DFS(int node)
{
    viz[node]=1;

    S.push_back(node);

    if(d[node]!=0)
        sol[node]=sol[S[S.size()-1-d[node]]]+1;


    for(auto x:G[node])
    {
        if(!viz[x])
         DFS(x);
    }

    S.pop_back();
}

int main()
{
    fath[0]=1;
    f>>n;
    for(int i=1;i<=n;i++)
     f>>d[i];

    for(int i=1;i<n;i++)
     {
      int x,y;
      f>>x>>y;
      G[x].push_back(y);
      fath[y]=x;
      if(fath[x]==0 && fath[poz]!=0) poz=x;
      if(fath[poz]) poz=0;

     }

    DFS(poz);


    for(int i=1;i<=n;i++) g<<sol[i]<<" ";


    return 0;
}