Cod sursa(job #3235996)

Utilizator maryyMaria Ciutea maryy Data 25 iunie 2024 02:24:24
Problema Cerere Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.74 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in("cerere.in");
ofstream out("cerere.out");
const int nmax = 1e5;
vector <int> s;
vector <int> g[nmax+1];
int k[nmax+1], d[nmax+1];
int r[nmax+1];
int n, first;
vector<int> stk;
void solve(int node)
{
    stk.push_back(node);
	r[node] = r[ stk[ stk.size() - k[node] - 1] ] + 1;
	for(auto it:g[node])
    {
		solve(it);
	}
	stk.pop_back();
}
int main()
{
    int x, y;
    in>>n;
    for(int i=1; i<=n; i++)
        in>>k[i];
    while(in>>x>>y)
    {
        g[x].push_back(y);
        d[y]++;
    }
    for(int i=1; i<=n; i++)
        if(d[i]==0)
            first=i;
    s.push_back(1);
    solve(1);
    for(int i=1; i<=n; i++)
        out<<r[i]-1<<" ";
}