Pagini recente » Cod sursa (job #1123978) | Cod sursa (job #1616850) | Cod sursa (job #2612766) | Cod sursa (job #874033) | Cod sursa (job #2218319)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream in("cerere.in");
ofstream out("cerere.out");
const int NMax = 100002;
const int INF = -2e9;
int N;
vector <int> a[NMax];
int val[NMax];
int d[NMax];
void citire(){
in>>N;
for(int i=1;i<=N;i++){
in>>val[i];
d[i]=0;
}
for(int i=0;i<N-1;i++){
int x,y; in>>x>>y;
a[y].push_back(x);
}
}
void dfs(int x, int parinte){
if(parinte == 1 || (x==parinte && val[x]==0)) return ;
else if(x==1) {d[parinte]++; return;}
int y = a[x][0];
if(val[parinte]!=0){
val[parinte]--;
dfs(y,parinte);
}
else if(d[x]!=0 || val[x] == 0){
d[parinte]+=(d[x]+1);
}
else{
dfs(y,x);
d[parinte]+=(d[x]+1);
}
}
int main()
{
citire();
for(int i=1; i<=N;i++) dfs(i,i);
for(int i=1;i<=N;i++) out<<d[i]<<" ";
return 0;
}