Cod sursa(job #2406328)

Utilizator liviu2000Dragomirescu Liviu liviu2000 Data 15 aprilie 2019 17:20:31
Problema Heavy Path Decomposition Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.2 kb
#include <bits/stdc++.h>
#define N 100005

using namespace std;

ifstream fin("heavypath.in") ;
ofstream fout("heavypath.out") ;

vector<int> graf[N] ;
int tt[N] , lvl[N] , a[N] ;

void dfs(int nod,int pf)
{
    lvl[nod] = lvl[pf]+1 ;
    tt[nod] = pf ;
    for ( auto vec : graf[nod] )
        if ( vec != pf)
            dfs(vec,nod) ;
}

int lca(int x,int y)
{
    int sol=0;
    if ( lvl[x] < lvl[y] )
        swap(x,y) ;
    while ( lvl[x] != lvl[y] )
    {
        sol = max(sol,a[x]) ;
        x = tt[x] ;
    }
    while ( x != y )
    {
        sol = max(sol,a[x]) ;
        sol = max(sol,a[y]) ;
        x = tt[x] ;
        y = tt[y] ;
    }
    sol = max(sol,a[x]);
    return sol;
}

int main()
{
    int n , q , i , x , y , tp;
    fin >> n >> q ;
    for ( i = 1 ; i <= n ; i++ )
    {
        fin >> x ;
        a[i] = x ;
    }
    for ( i = 1 ; i < n ; i++ )
    {
        fin >> x >> y ;
        graf[x].push_back(y) ;
        graf[y].push_back(x) ;
    }
    dfs(1,0) ;
    for ( i = 1 ; i <= q ; i++ )
    {
        fin >> tp >> x >> y ;
        if ( tp == 0 )
            a[x] = y ;
        else
            fout << lca(x,y) << '\n' ;
    }
}