#include #include #include #include #include #include #include #include #include #include using namespace std; class UnionFindTree{ typedef struct { int parent; int rank; }base_node; vector node; public: UnionFindTree(int n){ node.resize(n); for(int i=0; i node[y].rank){ node[y].parent = x; }else{ node[x].rank++; unite(x,y); } } }; #include typedef bitset<100000> bits; #include int main(){ int n,m; cin >> n >> m; vector> g(n); UnionFindTree uft(n); for(int i=0; i cnt(n, 0); for(int i=0; i w; vector unko; for(int i=0; i ans(n+1, 5*n); queue pos; pos.push(0); ans[0] =-1; for(auto itr=w.begin(); itr!=w.end(); itr++){ for(int k=1; itr->second>0; k<<=1){ int c = min(itr->second, k); vector next; while(pos.size()){ int p = pos.front(); pos.pop(); next.push_back(p); if(ans[p + c * itr->first] > ans[p] + c){ ans[p+c * itr->first] = ans[p] + c; next.push_back(p + c * itr->first); } } ; sort(next.begin(), next.end()); next.erase( unique(next.begin(), next.end()), next.end()); for(auto itr_=next.rbegin(); itr_!=next.rend(); itr_++){ pos.push(*itr_); } itr->second -= c; } } /* //for(int i=0; i next; while(pos.size()){ int p = pos.front(); pos.pop(); next.push_back(p); for(int k=0; ksecond; k++){ if(ans[p + itr->first] > ans[p] + 1){ ans[p + itr->first] = ans[p] + 1; next.push_back(p + itr->first); p += itr->first; } } } sort(next.begin(), next.end()); next.erase( unique(next.begin(), next.end()), next.end()); for(auto itr_=next.rbegin(); itr_!=next.rend(); itr_++){ pos.push(*itr_); } } */ for(int i=1; i<=n; i++){ if(ans[i] == 5*n) ans[i] = -1; printf("%d\n", ans[i]); } return 0; }