#include #include #include #include #include #include using namespace std; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define all(c) begin(c), end(c) using ll = long long; struct UnionFind { vector par; int cnt; UnionFind(int size_) : par(size_, -1), cnt(size_) { } bool merge(int x, int y) { x = find(x); y = find(y); if (x != y) { if (par[y] < par[x]) swap(x, y); par[x] += par[y]; par[y] = x; cnt--; } return x != y; } bool get(int x, int y) { return find(x) == find(y); } int find(int x) { return par[x] < 0 ? x : par[x] = find(par[x]); } int size(int x) { return -par[find(x)]; } int size() { return cnt; } }; int n, m; vector u, v; void solve() { UnionFind uf(n); rep(i, m) { uf.merge(u[i] - 1, v[i] - 1); } set vis; // root, size map cnt; rep(i, n) { if (vis.count(uf.find(i))) continue; vis.insert(uf.find(i)); cnt[uf.size(i)]++; } vector size, cost; for (auto &p : cnt) { int acc = 0; int i = 1; while (i <= p.second) { size.push_back(p.first*i); cost.push_back(i); acc += i; i *= 2; } i /= 2; if (acc == p.second) continue; size.push_back(p.first * (p.second - acc)); cost.push_back(p.second - acc); } vector dp(n + 1, 1000000000); dp[0] = 0; for (int i = 0; i < cost.size(); i++) { for (int j = n; j >= size[i]; --j) { if (j - size[i] == 0) { dp[j] = min(dp[j], cost[i] - 1); } else { dp[j] = min(dp[j], dp[j - size[i]] + cost[i]); } } } for (int i = 0; i < n; i++) { cout << (dp[i + 1] == 1000000000 ? -1 : dp[i + 1]) << '\n'; } } int main() { while (cin >> n >> m) { u.resize(m); v.resize(m); rep(i, m) cin >> u[i] >> v[i]; solve(); } }