Official Analysis

Binary lifting can be used to find the heaviest edge in the path from node uu to vv. Therefore, we only need to find the heaviest edge from uu to lca(u,v)\text{lca}(u,v) and vv to lca(u,v)\text{lca}(u,v).

Implementation

Time Complexity: O(MlogN)\mathcal{O}(M\log N)

C++

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 2e5;
const int MAXL = 20; // approximately maximum log
// for euler-tour
array<int, MAXN> enter_time, exit_time, depth;

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!