Cod sursa(job #1895811)

Utilizator l-teenLucian l-teen Data 28 februarie 2017 11:11:28
Problema Heavy Path Decomposition Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.46 kb
// 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[10000], *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;
                notRoot = 1;
            }
            if (notRoot)
                depthx--;
            notRoot = 0;
            while (py)
            {
                pathy[depthy++] = py - &v[0];
                py = py->p;
                notRoot = 1;
            }
            if (notRoot)
                depthy--;
            notRoot = 0;
            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;
}