Cod sursa(job #608822)

Utilizator GavrilaVladGavrila Vlad GavrilaVlad Data 18 august 2011 12:49:24
Problema Heavy Path Decomposition Scor Ascuns
Compilator cpp Status done
Runda Marime 1.54 kb
//Heavy Path Decomposition, brut, O(N * M)

#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

#define MAXN 100010

int N, M;
int v[MAXN], fol[MAXN], tata[MAXN], niv[MAXN];
vector<int> G[MAXN];
pair<int, pair<int, int> > op[MAXN];

void citire()
{
    scanf("%d%d", &N, &M);
    for(int i = 1; i <= N; ++i)
        scanf("%d", &v[i]);

    int a, b;
    for(int i = 1; i < N; ++i)
    {
        scanf("%d%d", &a, &b);
        G[a].push_back(b);
        G[b].push_back(a);
    }

    int t, x, y;
    for(int i = 1; i <= M; ++i)
    {
        scanf("%d%d%d", &t, &x, &y);
        op[i] = make_pair(t, make_pair(x, y));
    }
}

void df(int nod)
{
    fol[nod] = 1;

    for(vector<int> :: iterator it = G[nod].begin(); it != G[nod].end(); ++it)
    {
        if(fol[*it])
            continue;

        niv[*it] = niv[nod] + 1;
        tata[*it] = nod;

        df(*it);
    }
}

void solve()
{
    int t, x, y, sol = 0;

    for(int i = 1; i <= M; ++i)
    {
        t = op[i].first; x = op[i].second.first, y = op[i].second.second;
        if(t==0)
            v[x] = y;
        else
        {
            sol = 0;
            while(x != y)
            {
                if(niv[x] < niv[y])
                    swap(x, y);

                sol = max(sol, v[x]);
                x = tata[x];
            }

            sol = max(sol, v[x]);

            printf("%d\n", sol);
        }
    }
}

int main()
{
    freopen("heavypath.in", "r", stdin);
    freopen("heavypath.out", "w", stdout);

    citire();
    df(1);
    solve();

    return 0;
}