#include struct RNG { void setSeed(uint64_t seed) { seedSet = true; x = seed; } template T choice(std::vector& v) { assert(!v.empty()); return v[range(0, (int)v.size() - 1)]; } template void shuffle(std::vector& v) { const int N = v.size(); for (int i = 0; i < N - 1; i++) { const int j = range(i, N - 1); if (i == j) continue; std::swap(v[i], v[j]); } } // generate random integers in [from, to] template typename std::common_type::type range(T from, U to) { static_assert(std::is_integral_v); static_assert(std::is_integral_v); static_assert(std::is_integral_v::type>); assert(from <= to); uint64_t width = to - from + 1; typename std::common_type::type ret = from; ret += xorshift64() % width; return ret; } private: bool seedSet = false; uint64_t x; uint64_t xorshift64() { assert(seedSet); x ^= x << 13; x ^= x >> 7; x ^= x << 17; return x; } } rng; using namespace std; // https://ja.wikipedia.org/wiki/プリューファー列 vector> random_tree(int N) { vector> edges; vector p(N - 2); for (auto&& x : p) { x = rng.range(1, N); } vector d(N + 1, 1); for (auto&& x : p) { d[x]++; } priority_queue, greater> pque; for (int i = 1; i <= N; i++) { if (d[i] == 1) pque.push(i); } for (auto&& i : p) { auto j = pque.top(); pque.pop(); edges.push_back({i, j}); d[i]--; d[j]--; if (d[i] == 1) pque.push(i); if (d[j] == 1) pque.push(j); } int u = pque.top(); pque.pop(); int v = pque.top(); pque.pop(); assert(pque.empty()); edges.push_back({u, v}); for (auto&& pa : edges) { pa.first--; pa.second--; } return edges; } int main(int argc, char* argv[]) { const int seed = 7654345; rng.setSeed(seed); const int N = 200000; auto edges = random_tree(N); cout << N << endl; for (auto&& [u, v] : edges) { cout << min(u, v) + 1 << ' ' << max(u, v) + 1 << endl; } }