Pagini recente » Cod sursa (job #7717) | Cod sursa (job #198020) | Cod sursa (job #1849916) | Cod sursa (job #359233) | Cod sursa (job #2406328)
#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' ;
}
}