#include using namespace std; using LL = long long int; #define incID(i, l, r) for(int i = (l) ; i < (r); ++i) #define decID(i, l, r) for(int i = (r) - 1; i >= (l); --i) #define incII(i, l, r) for(int i = (l) ; i <= (r); ++i) #define decII(i, l, r) for(int i = (r) ; i >= (l); --i) #define inc(i, n) incID(i, 0, n) #define dec(i, n) decID(i, 0, n) #define inc1(i, n) incII(i, 1, n) #define dec1(i, n) decII(i, 1, n) #define inID(v, l, r) ((l) <= (v) && (v) < (r)) #define inII(v, l, r) ((l) <= (v) && (v) <= (r)) #define PB push_back #define EB emplace_back #define MP make_pair #define FI first #define SE second #define FR front() #define BA back() #define ALL(v) v.begin(), v.end() #define RALL(v) v.rbegin(), v.rend() auto setmin = [](auto & a, auto b) { return (b < a ? a = b, true : false); }; auto setmax = [](auto & a, auto b) { return (b > a ? a = b, true : false); }; auto setmineq = [](auto & a, auto b) { return (b <= a ? a = b, true : false); }; auto setmaxeq = [](auto & a, auto b) { return (b >= a ? a = b, true : false); }; #define SI(v) static_cast(v.size()) #define RF(e, v) for(auto & e: v) #define until(e) while(! (e)) #define if_not(e) if(! (e)) #define ef else if #define UR assert(false) #define IN(T, ...) T __VA_ARGS__; IN_(__VA_ARGS__); void IN_() { }; template void IN_(T & a, U & ... b) { cin >> a; IN_(b ...); }; template void OUT(T && a) { cout << a << endl; } template void OUT(T && a, U && ... b) { cout << a << " "; OUT(b ...); } // ---- ---- class UnionFind { private: int n, s; vector t; vector> v; public: UnionFind(int arg_n = 0) { init(arg_n); } void init(int arg_n) { n = s = arg_n; t.clear(); v.clear(); inc(i, n) { t.PB(i); v.EB(1, i); } } int get_n() { return n; } int size() { return s; } int id(int x) { return t.at(x); } const vector> & get_v() { return v; } bool unite(int x, int y) { x = id(x); y = id(y); if(x == y) { return false; } if(v[x].size() < v[y].size()) { swap(x, y); } for(auto & e: v[y]) { v[x].PB(e); t[e] = x; } v[y].clear(); s--; return true; } bool same(int x, int y) { return (id(x) == id(y)); } const vector & operator[](int x) { return v[id(x)]; } friend ostream & operator<<(ostream & os, const UnionFind & uf) { inc(i, uf.n) { os << i << ": "; for(auto & e: uf.v[i]) { os << e << " "; } os << "\n"; } return os; } }; // ---- ---- int main() { IN(int, n, m); UnionFind uf(n); inc(i, m) { IN(int, a, b); a--; b--; uf.unite(a, b); } map mp; RF(e, uf.get_v()) { mp[SI(e)]++; } int INF = n + 1; vector dp(n + 1, INF); dp[0] = 0; RF(e, mp) { dec(i, n) { if(dp[i] == INF) { continue; } inc1(j, e.SE) { if_not(setmin(dp[i + e.FI * j], dp[i] + j)) { break; } } } } inc1(i, n) { OUT(dp[i] == INF ? -1 : dp[i] - 1); } }