Cod sursa(job #1069490)

Utilizator R.A.RFMI Romila Remus Arthur R.A.R Data 30 decembrie 2013 07:45:12
Problema Cerere Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 0.91 kb
#include <fstream>
#include <vector>
using namespace std;

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

const int NMAX = 100001;

vector<int> V[NMAX];
int K[NMAX], Stack[NMAX], solve[NMAX];

void DF(int node, int value){
    Stack[value] = node;
    if(K[node])
        solve[node] = solve[ Stack[value - K[node]]] + 1;

    for(int i = 0; i < V[node].size(); i++){
        DF(V[node][i], value + 1);
    }

}

int main()
{
    int N, i, x, y, treeRoot;
    in >> N;

    for(i = 1; i <= N; i++){
        in >> K[i];
        if(K[i])
            solve[i]++;
    }

    for(i = 1, treeRoot = N; i < N; i++){
        in >> x >> y;

        V[x].push_back(y);

        treeRoot ^= y ^ i;
    }

    DF(treeRoot,0);

    for(i = 1; i <= N; i++){//arunc o cerere maimutei i
        out << solve[i] <<' ';
    }
    return 0;
}


/*       1
    2           6
  3   4       7    8
      5      9 10

















*/