#include #include #include using namespace std; int N, M; class UnionFind { int n; vector par; vector rank; vector cnt; public: UnionFind(int n) : n(n), cnt(n), par(n), rank(n) { for (int i = 0; i < n; i++) { par[i] = i; cnt[i] = 1; } } int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] < rank[y]) { par[x] = y; cnt[y] += cnt[x]; } else { par[y] = x; cnt[x] += cnt[y]; if (rank[x] == rank[y]) ++rank[x]; } } int count(int x) { return cnt[x]; } bool same(int x, int y) { return find(x) == find(y); } }; int main(int argc, char *argv[]) { cin >> N >> M; UnionFind uf(N); for (int i = 0; i < M; i++) { int v, w; cin >> v >> w; --v, --w; uf.unite(v, w); } map cmp; for (int i = 0; i < N; i++) { if (uf.find(i) == i) { cmp[uf.count(i)]++; } } vector dp(N+1, -1); vector ret(N+1, 99999999); for (auto &x : cmp) { int k = x.first; int v = x.second; cout << k << " " << v << endl; dp[0] = v; ret[0] = 0; for (int i = 1; i <= N; i++) { if (dp[i] >= 0) { dp[i] = v; } else if (i-k >= 0 && dp[i-k] > 0) { dp[i] = dp[i-k]-1; ret[i] = min(ret[i], ret[i-k] + 1); } else dp[i] = -1; } } for (int i = 1; i <= N; i++) { if (dp[i] == -1) cout << -1 << endl; else cout << ret[i]-1 << endl; } return 0; }