Cod sursa(job #1827829)

Utilizator NicolaalexandraNicola Alexandra Mihaela Nicolaalexandra Data 12 decembrie 2016 13:45:45
Problema Cerere Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.12 kb
//#include <fstream>
#include <cstdio>
#include <vector>
#define DIM 100001
using namespace std;
int i,n,x,y,v[DIM],sol[DIM],sol2[DIM],v2[DIM],w[DIM];
vector <int> L[DIM];
//ifstream fin ("cerere.in");
//ofstream fout ("cerere.out");
FILE *fin = fopen ("cerere.in","r");
FILE *fout = fopen ("cerere.out","w");
void dfs (int nod,int niv){
    v2[nod] = 1;
    if (v[nod] == 0)
        sol[niv] = 0;
    else
        sol[niv] = sol[niv-v[nod]]+1;
    sol2[nod] = sol[niv];
    for (int i=0;i<L[nod].size();i++)
        if (v2[L[nod][i]] == 0)
            dfs (L[nod][i],niv+1);


}
int main (){

    //fin>>n;
    fscanf (fin,"%d",&n);
    for (i=1;i<=n;i++)
        fscanf (fin,"%d",&v[i]);
        //fin>>v[i];

    for (i=1;i<n;i++){
        //fin>>x>>y;
        fscanf (fin,"%d%d",&x,&y);
        L[x].push_back(y);
       // L[y].push_back(x);
        w[y] = 1;
    }
    for (i=1;i<=n;i++){
        if (w[i] == 0){
            dfs (i,1);
            break;
        }
    }
    for (i=1;i<=n;i++)
        fprintf (fout,"%d ",sol2[i]);
        //fout<<sol2[i]<<" ";




    return 0;
}