#include "testlib.h" #include using namespace std; const int MIN_N = 1; const int MAX_N = 1e5; const int MIN_K = 1; const int MAX_K = 6; struct UnionFind { int num; vector rs, ps; UnionFind(int n) : num(n), rs(n, 1), ps(n, 0) { iota(ps.begin(), ps.end(), 0); } int find(int x) { return (x == ps[x] ? x : ps[x] = find(ps[x])); } bool same(int x, int y) { return find(x) == find(y); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rs[x] < rs[y]) swap(x, y); rs[x] += rs[y]; ps[y] = x; num--; } int size(int x) { return rs[find(x)]; } int count() const { return num; } }; signed main(int argc, char *argv[]) { registerValidation(argc, argv); int N = inf.readInt(MIN_N, MAX_N, "N"); inf.readSpace(); inf.readInt(MIN_K, MAX_K, "K"); inf.readEoln(); UnionFind uf(N); for (int i = 0; i < N; i++) { int a = inf.readInt(1, N, "a_i"); inf.readSpace(); int b = inf.readInt(1, N, "b_i"); inf.readEoln(); uf.unite(a - 1, b - 1); } ensure(uf.count() == 1); inf.readEof(); return 0; }