Pagini recente » Cod sursa (job #409282) | Cod sursa (job #1703708) | Cod sursa (job #1508260) | Cod sursa (job #982042) | Cod sursa (job #1076628)
#include <stdio.h>
#define NMAX 100000
typedef struct{
int vf, next;
}lista;
lista v[2 * NMAX + 1];
int ult[NMAX + 1], point[NMAX + 1], rez[NMAX + 1], noduri[NMAX + 1], pozmax = 1, trecut[NMAX + 1];
inline void add(int k, int x, int y){
v[k].vf = y;
v[k].next = ult[x];
ult[x] = k;
return ;
}
void caut(int x){
if(point[x] != 0){
rez[x] = 1 + rez[noduri[pozmax - point[x]]];
}
else rez[x] = 0;
trecut[x] = 1;
int poz = ult[x], y;
noduri[pozmax] = x;
pozmax++;
while(poz > 0){
y = v[poz].vf;
if(!trecut[y]) caut(y);
poz = v[poz].next;
}
noduri[pozmax] = 0;
pozmax--;
return ;
}
int radacina ( int x ){
if ( v[ v [ ult [ x ] ].next ].vf == 0 ) return x;
return radacina ( v[ v [ ult [ x ] ].next ].vf );
}
int main()
{
FILE *in = fopen("cerere.in", "r");
int n;
fscanf(in, "%d", &n);
int i;
for(i = 1; i <= n; i++){
fscanf(in, "%d", &point[i]);
}
int x, y, k = 0;
for(i = 1; i < n; i++){
fscanf(in, "%d%d", &x, &y);
k++;
add(k, x, y);
k++;
add(k, y, x);
}
fclose(in);
int tata;
tata = radacina ( 1 );
caut(tata);
FILE *out = fopen("cerere.out", "w");
for(i = 1; i <= n; i++){
fprintf(out, "%d ", rez[i]);
}
fclose(out);
return 0;
}