Cod sursa(job #478536)

Utilizator andra23Laura Draghici andra23 Data 19 august 2010 00:14:38
Problema Cerere Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.14 kb
#include<iostream>
#include<fstream>
#include<vector>

using namespace std;

int a[100010], n, s[100010], tata[100010], d[100010], viz[100010], next[100010];
vector<int> son[100010];

int main(){
    ifstream f("cerere.in");
    ofstream g("cerere.out");
    int i, x, y, k, t;
    f>>n;
    for (i = 1; i <= n; i++)
        f>>a[i];
        
    for (i = 1; i <= n-1; i++){
        f>>x>>y;
        son[x].push_back(y);
        tata[y] = x;
    }
    
    int rad = 1;
    while (tata[rad] != 0)
        rad = tata[rad];
    
    int vf = 1;
    s[vf] = rad;
    
    while (vf > 0){
        x = s[vf];
        if (a[x] > 0)
            d[x] = d[s[vf-a[x]]]+1;
        if (viz[x] == 0 && son[x].size() != 0){
            vf++;
            s[vf] = son[x][0];
            next[vf] = 1;   
            viz[s[vf-1]] = 1; 
        }
        else 
            if (next[vf] < son[s[vf-1]].size ()){
                s[vf] = son[s[vf-1]][next[vf]];  
                next[vf]++;
            }
            else 
                vf--;   
    }
    
    for (i = 1; i <= n; i++)
        g<<d[i]<<" ";
       
    return 0;
}