Cod sursa(job #2314150)

Utilizator maria15Maria Dinca maria15 Data 7 ianuarie 2019 23:03:10
Problema Cerere Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream fin("cerere.in");
ofstream fout("cerere.out");

int n, i, k[1000001], a, b, t[1000001], nr[1000001], y;
vector<int> v[1000001], q;
char s[10];

void dfs(int nod, int niv){
    q.push_back(nod);
    int sol = q[niv-1-k[nod]];
    nr[nod] = nr[sol] + 1;
    for(int i = 0;i<v[nod].size();i++){
        int crt = v[nod][i];
        dfs(crt, niv+1);
    }
    q.pop_back();
}

int main(){
    fin>>n;
    for(i=1;i<=n;i++){
        fin>>s;
        for(y=0;s[y]!=0;y++)
            k[i] = k[i]*10 + s[y] - '0';
    }
    for(i=1;i<n;i++){
        fin>>s;
        a = b = 0;
        for(y=0;s[y]!=0;y++)
            a = a*10 + s[y] - '0';
        fin>>s;
        for(y=0;s[y]!=0;y++)
            b = b*10 + s[y] - '0';
        v[a].push_back(b);
        t[b] = a;
    }
    for(i=1;i<=n;i++)
        nr[i] = -1;

    for(i=1;;i++)
        if(t[i] == 0){
            dfs(i, 1);
            break;
        }
    for(i=1;i<=n;i++)
        fout<<nr[i]<<" ";
    return 0;
}