Cod sursa(job #1946677)

Utilizator l-teenLucian l-teen Data 30 martie 2017 12:33:45
Problema Heavy Path Decomposition Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.94 kb
// HPDecomp.cpp : Defines the entry point for the console application.
//

#include <stdio.h>

typedef struct st
{
    unsigned long long v;
    struct st *p;
    int d;
}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);

    for (i = 0; i < N; ++i)
    {
        scanf("%llu", &v[i].v);
        v[i].p = 0;
        v[i].d = -1;
    }

    v[0].d = 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];
            if (v[p - 1].d != -1)
                v[q - 1].d = v[p - 1].d + 1;
            else
                return -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
        {
            --r; --t;
            max = 0;
            px = &v[r];
            py = &v[t];
            while (px->d > v[t].d)
            {
                if (max < px->v)
                    max = px->v;
                px = px->p;
            }
            while (py->d > v[r].d)
            {
                if (max < py->v)
                    max = py->v;
                py = py->p;
            }
            while (px != py)
            {
                if (max < px->v)
                    max = px->v;
                if (max < py->v)
                    max = py->v;
                px = px->p;
                py = py->p;
            }
            if (px->v > max)
                max = px->v;
            printf("%llu\n", max);
        }
    }

	return 0;
}