Cod sursa(job #3317898)

Utilizator Andrei-Dani-10Pisla Andrei Daniel Andrei-Dani-10 Data 25 octombrie 2025 23:07:42
Problema Heavy Path Decomposition Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.51 kb
#include <fstream>

#include <vector>
#include <algorithm>

using namespace std;

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

const int nmax = 1e5;
const int maxint = (1 << 30);
int n, nrq, type, x, y, costt[nmax + 2];

vector <int> muchii[nmax + 2];
vector <int> nodepaths[nmax + 2];

int heavypath[nmax + 2], subtreesize[nmax + 2];
int wherenodeidx[nmax + 2], withtag[nmax + 2];
int depth[nmax + 2], father[nmax + 2];

int headpath[nmax + 2];

void dfsbuildhld(int node, int parent){

    depth[node] = 1 + depth[parent];
    father[node] = parent;

    for(auto nxt : muchii[node]){
        if(nxt != parent)
            dfsbuildhld(nxt, node);
    }

    return;
}

int hldquery(int a, int b){
    int maxpath = 0;

    for(; a != b; ){
        if(depth[a] < depth[b])
            swap(a, b);

        maxpath = max(maxpath, costt[a]);
        a = father[a];
    }

    maxpath = max(maxpath, costt[a]);

    return maxpath;
}

int main(){

    in.tie(NULL) -> sync_with_stdio(false); out.tie(NULL);

    in>>n>>nrq;
    for(int i = 1; i <= n; i++){
        in>>costt[i];
    }

    for(int i = 1; i <= n - 1; i++){
        in>>x>>y;
        muchii[x].push_back(y);
        muchii[y].push_back(x);
    }

    dfsbuildhld(1, 0);

    for(int itq = 1; itq <= nrq; itq++){
        in>>type>>x>>y;

        if(type == 0){
            costt[x] = y;
        }else if(type == 1){
            out<<hldquery(x, y)<<"\n";
        }
    }

    return 0;
}