#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(int i=0; i next; while(pos.size()){ int p = pos.front(); pos.pop(); if(ans[p+unko[i]] > ans[p]+1){ ans[p+unko[i]] = ans[p]+1; next.push_back(p+unko[i]); } next.push_back(p); } sort(next.begin(), next.end()); next.erase( unique(next.begin(), next.end()), next.end()); for(auto itr=next.begin(); itr!=next.end(); 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; }