CF - Duff in the Army
Author: Chongtian Ma
Explanation
Note that for each two nodes and , the path between them is just to and to . Also note the constraint of for each query. To answer each query, we only need to store the people with the smallest ids for the path from to and to , and combine them. This can be done with binary lifting, which will store the smallest ids for each vertex to their 'th ancestor.
Implementation
We can answer each query in . Even though the constant factor is pretty bad, the problem limits give room for it. Note that if you do store more than smallest ids the problem will yield a MLE!
Time Complexity: for each query.
C++
#include <bits/stdc++.h>using namespace std;const int MAXN = 1e5 + 1;const int MAXL = 20; // approximately maximum log// for euler-tourarray<int, MAXN> enter_time, exit_time, depth;int timer = 1;// for binary lifting
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!