#include #include"testlib.h" using ll = long long; using namespace std; struct UnionFind { vector< int > data; UnionFind(int sz) { data.assign(sz, -1); } bool unite(int x, int y) { x = find(x), y = find(y); if(x == y) return (false); if(data[x] > data[y]) swap(x, y); data[x] += data[y]; data[y] = x; return (true); } int find(int k) { if(data[k] < 0) return (k); return (data[k] = find(data[k])); } int size(int k) { return (-data[find(k)]); } }; int main(int argc, char* argv[]){ const int MIN_N = 3; const int MAX_N = 100000; const int MIN_K = 2; const int MAX_K = 6; registerValidation(argc, argv); int n = inf.readInt(MIN_N, MAX_N); inf.readSpace(); int k = inf.readInt(MIN_K, MAX_K); inf.readEoln(); UnionFind uf(n); for (int i = 0; i < n; i++) { int a = inf.readInt(1, n); inf.readSpace(); int b = inf.readInt(1, n); inf.readEoln(); ensuref(a != b, "a and b should not be equal"); uf.unite(a - 1, b - 1); } ensuref(uf.size(0) == n, "the graph should be connected"); inf.readEof(); return 0; }