Pagini recente » Cod sursa (job #728743) | Cod sursa (job #792632) | Cod sursa (job #3194476) | Cod sursa (job #2875943) | Cod sursa (job #127749)
Cod sursa(job #127749)
#include <stdio.h>
typedef struct nod
{
int inf;
nod *adr;
} *pNod;
pNod v[16001];
int viz[16001], n, val[16001], max, sc;
void adauga(int x, int y)
{
pNod q;
q = new nod;
q -> inf = y;
q -> adr = v[x];
v[x] = q;
q = new nod;
q -> inf = x;
q -> adr = v[y];
v[y] = q;
}
void citire()
{
freopen("asmax.in","r",stdin);
freopen("asmax.out","w",stdout);
scanf("%d",&n);
int i, x, y;
for (i = 1; i <= n; i++) scanf("%d",&val[i]);
for (i = 1; i < n; i++)
{
scanf("%d %d",&x, &y);
adauga(x,y);
}
}
void df(int nod)
{
viz[nod] = 1;
sc += val[nod];
int suma = sc;
if (suma > max) max = suma;
pNod p;
for (p = v[nod]; p != NULL; p = p -> adr)
{
if (!viz[p -> inf])
{
df(p -> inf);
// sc = suma;
}
//sc = suma;
}
}
int main()
{
citire();
df(1);
printf("%d\n",max);
return 0;
}