Official Analysis (C++)

Implementation

Time Complexity: O(N2logN2)\mathcal{O}(N^2 \log N^2)

C++

Interestingly, the following code passes all test cases but won't pass if T=1T = 1; you'd have to make a separate edge case for that.

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Edge {
int u, v, w;
};
struct DSU {
vector<int> p, sz, unprocessed_starts;

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!