/* -*- coding: utf-8 -*- * * 556.cc: No.556 仁義なきサルたち - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 10000; const int INF = 1 << 30; /* typedef */ typedef vector vi; typedef queue qi; typedef pair pii; struct UFT { int links[MAX_N], sizes[MAX_N]; UFT() {} void clear(int n) { for (int i = 0; i < n; i++) links[i] = i, sizes[i] = 1; } int root(int i) { int i0 = i; while (links[i0] != i0) i0 = links[i0]; return (links[i] = i0); } int size(int i) { return sizes[root(i)]; } bool same(int i, int j) { return root(i) == root(j); } int merge(int i0, int i1) { int r0 = root(i0), r1 = root(i1), mr; if (r0 == r1) return r0; if (r0 > r1) swap(r0, r1); if (sizes[r0] >= sizes[r1]) { links[r1] = r0; sizes[r0] += sizes[r1]; mr = r0; } else { links[r0] = r1; sizes[r1] += sizes[r0]; mr = r1; } return mr; } }; /* global variables */ UFT uft; /* subroutines */ /* main */ int main() { int n, m; scanf("%d%d", &n, &m); uft.clear(n); for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); a--, b--; uft.merge(a, b); } for (int i = 0; i < n; i++) printf("%d\n", uft.root(i) + 1); return 0; }