Pagini recente » Cod sursa (job #2629460) | Cod sursa (job #2185118) | Cod sursa (job #2623588) | Cod sursa (job #1894817)
// HPDecomp.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
typedef struct st
{
unsigned long long v;
struct st *p;
}s;
int main()
{
unsigned int N, M, i, p, q, pathx[100000], pathy[100000], depthx, depthy, notRoot;
s v[100000], *px, *py;
unsigned long long r, t, max = 0;
if (!freopen("heavypath.in", "r", stdin))
return -1;
if (!freopen("heavypath.out", "w", stdout))
return -1;
scanf("%d %d", &N, &M);
scanf("%llu", &v[0].v);
v[0].p = 0;
for (i = 1; i < N; ++i)
{
scanf("%llu", &v[i].v);
v[i].p = 0;
}
for (i = 0; i < N - 1; ++i)
{
scanf("%d %d", &p, &q);
if (!v[q-1].p)
v[q-1].p = &v[p-1];
else
return -1;
}
for (i = 0; i < M; ++i)
{
scanf("%d %llu %llu", &N, &r, &t);
if (!N)
{
v[r - 1].v = t;
}
else
{
depthx = depthy = 0;
px = &v[r - 1];
py = &v[t - 1];
notRoot = 0;
while (px)
{
pathx[depthx++] = px - &v[0];
px = px->p;
}
while (py)
{
pathy[depthy++] = py - &v[0];
py = py->p;
}
depthx--;
depthy--;
while ((pathx[depthx] == pathy[depthy]) && (depthx) && (depthy))
{
notRoot = 1;
depthx--;
depthy--;
}
if (notRoot)
max = v[pathx[depthx + 1]].v;
else
max = v[pathx[depthx]].v;
while (depthx)
{
if (max < v[pathx[depthx]].v)
max = v[pathx[depthx]].v;
depthx--;
}
while (depthy)
{
if (max < v[pathx[depthy]].v)
max = v[pathx[depthy]].v;
depthy--;
}
if (max < v[pathx[0]].v)
max = v[pathx[0]].v;
if (max < v[pathy[0]].v)
max = v[pathy[0]].v;
printf("%llu\n", max);
}
}
return 0;
}