#include #include using namespace std; struct UF{ vector par, size; UF(int n){ for(int i = 0; i < n; i++) par.emplace_back(i); size.assign(n, 1); } int root(int x){ return par[x] == x ? x : par[x] = root(par[x]); } bool same(int x, int y){ return root(x) == root(y); } void unite(int x, int y){ x = root(x); y = root(y); if(size[x] == size[y]){ if(x > y) swap(x, y); }else if(size[x] < size[y]) swap(x, y); par[y] = x; size[x] += size[y]; size[y] = 0; } }; int main(){ int n, m; cin >> n >> m; UF uf(n); while(m--){ int a, b; cin >> a >> b; if(uf.same(--a, --b)) continue; uf.unite(a, b); } for(int i = 0; i < n; i++) cout << uf.root(i) + 1 << endl; }