#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define REP(i,a,b) for(int i=a;i<(int)b;i++) #define rep(i,n) REP(i,0,n) #define all(c) (c).begin(), (c).end() #define zero(a) memset(a, 0, sizeof a) #define minus(a) memset(a, -1, sizeof a) #define minimize(a, x) a = std::min(a, x) #define maximize(a, x) a = std::max(a, x) typedef long long ll; int const inf = 1<<29; struct union_find { vector rank_; vector size_; vector rid_; union_find(int n) { rank_.resize(n); rid_.assign(n, -1); size_.resize(n, 1); } void unite(int u, int v) { u = root(u), v = root(v); if(u == v) { return; } size_[u] = size_[v] = size_[u] + size_[v]; if(rank_[u] < rank_[v]) { rid_[u] = v; } else { rid_[v] = u; if(rank_[u] == rank_[v]) { rank_[u]++; } } } bool same(int u, int v) { return root(u) == root(v); } int root(int x) { if(rid_[x] < 0) return x; else return rid_[x] = root(rid_[x]); } int operator[](int x) { return root(x); } int size_of(int x) { return size_[x]; } }; int main() { int N, M; cin >> N >> M; union_find uf(N); rep(i, M) { int u, v; cin >> u >> v; u--, v--; uf.unite(u, v); } map mp; rep(i, N) { mp[uf.root(i)] ++; } vector sizecnt(N + 1); rep(i, N+1) { if(uf[i] == i) { sizecnt[uf.size_of(uf[i])] ++; } } vector dp(N + 1, inf); dp[0] = 0; // programming contest challenge book p.303 int W = 0; for(int i = 1; i <= N; i++) { int num = sizecnt[i]; for(int k = 1; num > 0; k *= 2) { int mul = min(k, num); num -= mul; W += i * mul; for(int j = W; j >= i * mul; j--) { if(dp[j - i * mul] >= inf) { continue; } minimize(dp[j], dp[j - i * mul] + mul); } } } for(int i = 1; i <= N; i++) { printf("%d\n", dp[i] == inf ? -1 : dp[i] - 1); } return 0; }