Table of Contents

ExplanationImplementation

Explanation

Assume there is some hard route that goes from vertex uu to vertex vv. Let the node that the path from uu to vv and the furthest node from the hard route meet be node xx. Use rerooting DP to calculate the hardest route and the number of such hardest routes for every node xx.

Implementation

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

C++

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n;
cin >> n;
vector<vector<int>> adj(n);
for (int i = 1; i < n; i++) {
int x, y;

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!