#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair pii; typedef pair pll; const int INF = 1e9; const ll LINF = 1e18; template ostream& operator << (ostream& out,const pair& o){ out << "(" << o.first << "," << o.second << ")"; return out; } template ostream& operator << (ostream& out,const vector V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; } template ostream& operator << (ostream& out,const vector > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; } template ostream& operator << (ostream& out,const map mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; } /* 問題文============================================================ ================================================================= 解説============================================================= ================================================================ */ class SCC { public: typedef int TYPE; TYPE V; vector> G; vector> rG; vector> group_G; vector> group_rG; vector group; vector List; vector visited; int group_num; SCC(TYPE V) :V(V),group_num(0) { G.resize(V); rG.resize(V); } void init(TYPE V){ G.clear(); G.resize(V); rG.clear(); rG.resize(V); } void add_edge(TYPE u, TYPE v) { G[u].emplace_back(v); rG[v].emplace_back(u); } void dfs1(TYPE u) { visited[u] = 1; for (TYPE& next : G[u]){ if (visited[next] != 1) dfs1(next); } List.emplace_back(u); } void dfs2(TYPE u,TYPE group_num) { visited[u] = 1; group[u] = group_num; for (TYPE& next : rG[u]){ if (visited[next] != 1) dfs2(next, group_num); } } void make_group() { group.clear(); group.assign(V, -1); visited.clear(); visited.assign(V, 0); for (int i = 0; i < V;i++){ if (visited[i] != 1) dfs1(i); } reverse(List.begin(), List.end()); visited.clear(); visited.assign(V, 0); group_num = 0; for (TYPE& v : List) { if (visited[v] != 1) dfs2(v, group_num++); } } void make_group_graph(){ group_G.clear(); group_G.resize(group_num); group_rG.clear(); group_rG.resize(group_num); for(int u = 0; u < V;u++){ for(TYPE& v: G[u]){ if(!same(u,v)){ group_G[group[u]].emplace_back(group[v]); group_rG[group[v]].emplace_back(group[u]); } } } } TYPE get_group(TYPE n){ return group[n]; } bool same(TYPE u,TYPE v) { return group[u] == group[v]; } vector>& get_G() { return G; } vector>& get_rG() { return rG; } vector>& get_group_G() { return group_G; } vector>& get_group_rG() { return group_rG; } }; void solve(){ int N,M; cin >> N >> M; SCC scc(N); for(int i = 0; i < M;i++){ int u,v; cin >> u >> v; u--; v--; scc.add_edge(u, v); scc.add_edge(v, u); } scc.make_group(); vector cc(scc.group_num); for(auto g:scc.group) cc[g]++; sort(cc.begin(),cc.end()); vector cusum(cc.size()); cusum[0] = cc[0]; for(int i = 1; i < cc.size();i++) cusum[i] = cusum[i-1] + cc[i]; vector dp(N+1,LINF); dp[0] = 0; for(int j = 0; j < cc.size();j++){ for(int i = cusum[j]; i >= 0;i--){ if(dp[i]==LINF) continue; dp[i+cc[j]] = min(dp[i+cc[j]],dp[i]+1); } } for(int i = 1; i <= N;i++){ if(dp[i]==LINF) cout << -1 << endl; else cout << dp[i]-1 << endl; } } int main(void) { cin.tie(0); ios_base::sync_with_stdio(false); solve(); return 0; }