// {{{ Templates #include #define show(x) cerr << #x << " = " << x << endl using namespace std; using ll = long long; using pii = pair; using vi = vector; template ostream& operator<<(ostream& os, const vector& v) { os << "sz:" << v.size() << "\n["; for (const auto& p : v) { os << p << ","; } os << "]\n"; return os; } template ostream& operator<<(ostream& os, const pair& p) { os << "(" << p.first << "," << p.second << ")"; return os; } constexpr ll MOD = (ll)1e9 + 7LL; template constexpr T INF = numeric_limits::max() / 100; // }}} class DisjointSets { public: DisjointSets(const int v) { m_parent.resize(v); m_rank.resize(v); m_size.resize(v); for (int i = 0; i < v; i++) { m_parent[i] = i; m_rank[i] = 0; m_size[i] = 1; } } bool same(const int a, const int b) { return find(a) == find(b); } int find(const int a) { if (m_parent[a] == a) { return a; } else { return m_parent[a] = find(m_parent[a]); } } void unite(const int a_, const int b_) { const int a = find(a_); const int b = find(b_); if (a == b) { return; } if (m_rank[a] > m_rank[b]) { m_parent[b] = a; m_size[a] += m_size[b]; } else { m_parent[a] = b; m_size[b] += m_size[a]; } if (m_rank[a] == m_rank[b]) { m_rank[b]++; } } int getSize(const int a) { return m_size[m_parent[a]]; } private: vector m_parent; vector m_rank; vector m_size; }; int main() { cin.tie(0); ios::sync_with_stdio(false); int N, M; cin >> N >> M; DisjointSets uf(N); for (int i = 0; i < M; i++) { int u, v; cin >> u >> v; u--, v--; uf.unite(u, v); } vector sizes; unordered_set st; for (int i = 0; i < N; i++) { if (st.find(uf.find(i)) == st.end()) { st.insert(uf.find(i)); sizes.push_back(uf.getSize(i)); } } sort(sizes.begin(), sizes.end()); auto sizes_cop = sizes; sizes_cop.erase(unique(sizes_cop.begin(), sizes_cop.end()), sizes_cop.end()); const int size = sizes_cop.size(); vector> elem(size); for (int i = 0; i < size; i++) { const int n = upper_bound(sizes.begin(), sizes.end(), sizes_cop[i]) - lower_bound(sizes.begin(), sizes.end(), sizes_cop[i]); for (int j = 1; j <= n; j++) { elem[i][sizes_cop[i] * j] = 0; } } unordered_map ans = elem[0]; for (int i = 1; i < size; i++) { unordered_map tmp = elem[i]; for (const auto& p1 : ans) { for (const auto& p2 : elem[i]) { const int sum = p1.first + p2.first; const int edge = p1.second + p2.second + 1; if (tmp.find(sum) == tmp.end()) { tmp[sum] = edge; } else { tmp[sum] = min(tmp[sum], edge); } } } for (const auto& p : tmp) { const int num = p.first; const int edge = p.second; if (ans.find(num) == ans.end()) { ans[num] = edge; } else { ans[num] = min(ans[num], edge); } } } for (int i = 1; i <= N; i++) { if (ans.find(i) == ans.end()) { cout << -1 << endl; } else { cout << ans[i] << endl; } } return 0; }