#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define FOR(I,A,B) for(int I = (A); I < (B); ++I) #define CLR(mat) memset(mat, 0, sizeof(mat)) typedef long long ll; // union - find tree !!!!!!!!!!!!!!!!!!!!!!!!! vector par; //oya vector rnk; //ki no hu ka sa // n要素で初期化 void init(int n){ par.resize(n);rnk.resize(n); FOR(i, 0, n){par[i] = i;rnk[i] = 1;} } //木の根を求める int find(int x){ if(par[x] == x) return x; else return par[x] = find(par[x]); } //xとyの属する集合を併合 void unite(int x, int y){ x = find(x);y = find(y); if(x == y) return; if(rnk[x] > rnk[y]) { par[y] = x; rnk[x] += rnk[y]; rnk[y] = 0; } else if(rnk[x] < rnk[y]) { par[x] = y; rnk[y] += rnk[x]; rnk[x] = 0; } else { if(x < y) { par[y] = x; rnk[x] += rnk[y]; rnk[y] = 0; } else { par[x] = y; rnk[y] += rnk[x]; rnk[x] = 0; } } return; } int main() { int N, M; cin >> N >> M; init(N+1); FOR(i,0,M) { int a, b; cin >> a >> b; unite(a, b); } FOR(i,1,N+1) { cout << find(i) << endl; } return 0; }