// No.556 仁義なきサルたち // https://yukicoder.me/problems/no/556 // #include #include #include using namespace std; struct Monkey { int boss; unordered_set members; }; void solve(vector &m, unsigned int M); int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); unsigned int N, M; cin >> N >> M; vector m(N+1); for (auto i = 1; i <= N; ++i) m[i].boss = i; solve(m, M); for (auto i = 1; i <= N; ++i) cout << m[i].boss << endl; } void solve(vector &m, unsigned int M) { for (auto i = 0; i < M; ++i) { int a, b; cin >> a >> b; int a_boss = m[a].boss; int b_boss = m[b].boss; if (a_boss == b_boss) ; else if (m[a_boss].members.size() > m[b_boss].members.size() || (m[a_boss].members.size() == m[b_boss].members.size() && a_boss < b_boss)) { m[b_boss].boss = a_boss; m[a_boss].members.insert(b_boss); for (auto i: m[b_boss].members) { m[i].boss = a_boss; m[a_boss].members.insert(i); } } else { m[a_boss].boss = b_boss; m[b_boss].members.insert(a_boss); for (auto i: m[a_boss].members) { m[i].boss = b_boss; m[b_boss].members.insert(i); } } } }