/* -*- coding: utf-8 -*- * * 317.cc: No.317 辺の追加 - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 100000; const int INF = 1 << 30; /* typedef */ typedef vector vi; struct UFT { int links[MAX_N], ranks[MAX_N], sizes[MAX_N]; void clear(int n) { for (int i = 0; i < n; i++) links[i] = i, ranks[i] = sizes[i] = 1; } UFT() {} UFT(int n) { clear(n); } int root(int i) { int i0 = i; while (links[i0] != i0) i0 = links[i0]; return (links[i] = i0); } int rank(int i) { return ranks[root(i)]; } int size(int i) { return sizes[root(i)]; } bool same(int i, int j) { return root(i) == root(j); } int merge(int i0, int i1) { int r0 = root(i0), r1 = root(i1), mr; if (r0 == r1) return r0; if (ranks[r0] == ranks[r1]) { links[r1] = r0; sizes[r0] += sizes[r1]; ranks[r0]++; mr = r0; } else if (ranks[r0] > ranks[r1]) { links[r1] = r0; sizes[r0] += sizes[r1]; mr = r0; } else { links[r0] = r1; sizes[r1] += sizes[r0]; mr = r1; } return mr; } }; /* global variables */ UFT uft; vi cs; int dp[MAX_N + 1]; /* subroutines */ /* main */ int main() { int n, m; cin >> n >> m; uft.clear(n); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--, v--; if (! uft.same(u, v)) uft.merge(u, v); } for (int i = 0; i < n; i++) if (uft.root(i) == i) cs.push_back(uft.size(i)); int cn = cs.size(); //for (int i = 0; i < cn; i++) printf("%d ", cs[i]); putchar('\n'); for (int i = 0; i <= n; i++) dp[i] = INF; dp[0] = 0; for (int i = 0; i < cn; i++) { int &ci = cs[i]; for (int j = n - ci, k = n; j >= 0; j--, k--) if (dp[j] < INF) { int d = dp[j] + 1; if (dp[k] > d) dp[k] = d; } } for (int i = 1; i <= n; i++) printf("%d\n", (dp[i] >= INF) ? -1 : dp[i] - 1); return 0; }