Cod sursa(job #3235995)

Utilizator maryyMaria Ciutea maryy Data 25 iunie 2024 02:22:59
Problema Cerere Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.56 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;
//void solve(int start)//afla al ki lea stramos pt fiecare maimuta
//{
//    queue <int> q;
//    int len = 1;
//    while(s.size())
//    {
//        int tata = s.back();
//        s.pop_back();
//        for(int i=0; i<g[tata].size(); i++)
//        {
//            int copil = g[tata][i];
//            if(k[copil]==1) r[copil] = r[tata] + 1;
//            else if(k[copil]==0) r[copil] = 0;
//            else
//            {
//                //r[copil] = r[ s[ len-k[copil] ] ] + 1;
//                out<<len<<" "<<k[copil]<<'\n';
//                //out<<copil<<" "<<s[len-k[copil]]<<'\n';
//            }
//
//            q.push(copil);
//        }
//        len--;
//        while(!q.empty())
//        {
//            s.push_back(q.front()); len++;
//            q.pop();
//        }
//    }
//}
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(first);
    solve(first);
    for(int i=1; i<=n; i++)
        out<<r[i]-1<<" ";
}