import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; immutable int INF = 1 << 29; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto uf = new UnionFind(N); foreach (_; 0..M) { s = readln.split.map!(to!int); uf.unite(s[0]-1, s[1]-1); } int[int] cnt; foreach (i; 0..N) if (uf.table[i] < 0) cnt[-uf.table[i]] += 1; auto dp = new int[][](2, N+1); dp[0].fill(INF); dp[1].fill(INF); dp[0][0] = -1; int cur = 0; int tar = 1; foreach (i; cnt.keys) { dp[tar] = dp[cur].dup; for (int j = 0; j < N; ++j) { for (int k = 1; k * i + j <= N && k <= cnt[i]; ++k) { dp[tar][k*i+j] = min(dp[cur][k*i+j], dp[cur][j] + k); } } cur ^= 1; tar ^= 1; } iota(1, N+1).map!(i => dp[cur][i] == INF ? -1 : dp[cur][i]).each!writeln; } class UnionFind { int N; int[] table; this(int n) { N = n; table = new int[](N); fill(table, -1); } int find(int x) { return table[x] < 0 ? x : (table[x] = find(table[x])); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (table[x] > table[y]) swap(x, y); table[x] += table[y]; table[y] = x; } }