Cod sursa(job #2217579)

Utilizator unknownpersonBidasca Carina Georgiana unknownperson Data 30 iunie 2018 22:26:48
Problema Heavy Path Decomposition Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.48 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("heavypath.in");
ofstream g("heavypath.out");
const int  MAXN =100010;
int N, M;
int v[MAXN], viz[MAXN], tata[MAXN], niv[MAXN];
vector<int> G[MAXN];
pair<int, pair<int, int> > op[MAXN];

void citire()
{
    f>>N>>M;
    for(int i = 1; i <= N; ++i)
        f>>v[i];

    int a, b;
    for(int i = 1; i < N; ++i)
    {
        f>>a>>b;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    int t, x, y;
    for(int i = 1; i <= M; ++i)
    {
        f>>t>>x>>y;
        op[i] = make_pair(t, make_pair(x, y));
    }
}

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

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

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

        df(*it);
    }
}

void solutie()
{
    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]);

            g<<sol<<"\n";
        }
    }
}

int main()
{
    citire();
    df(1);
    solutie();

    return 0;
}