// #define _GLIBCXX_DEBUG #include // clang-format off std::ostream&operator<<(std::ostream&os,std::int8_t x){return os<<(int)x;} std::ostream&operator<<(std::ostream&os,std::uint8_t x){return os<<(int)x;} std::ostream&operator<<(std::ostream&os,const __int128_t &u){if(!u)os<<"0";__int128_t tmp=u<0?(os<<"-",-u):u;std::string s;while(tmp)s+='0'+(tmp%10),tmp/=10;return std::reverse(s.begin(),s.end()),os< namespace _la_internal { using namespace std; template struct Vector { valarray dat; Vector()= default; Vector(size_t n): dat(n) {} Vector(size_t n, const R &v): dat(v, n) {} Vector(const initializer_list &v): dat(v) {} R &operator[](int i) { return dat[i]; } const R &operator[](int i) const { return dat[i]; } bool operator==(const Vector &r) const { if (dat.size() != r.dat.size()) return false; for (int i= dat.size(); i--;) if (dat[i] != r.dat[i]) return false; return true; } bool operator!=(const Vector &r) const { return !(*this == r); } explicit operator bool() const { return dat.size(); } Vector operator-() const { return Vector(dat.size())-= *this; } Vector &operator+=(const Vector &r) { return dat+= r.dat, *this; } Vector &operator-=(const Vector &r) { return dat-= r.dat, *this; } Vector &operator*=(const R &r) { return dat*= r, *this; } Vector operator+(const Vector &r) const { return Vector(*this)+= r; } Vector operator-(const Vector &r) const { return Vector(*this)-= r; } Vector operator*(const R &r) const { return Vector(*this)*= r; } size_t size() const { return dat.size(); } friend R dot(const Vector &a, const Vector &b) { return assert(a.size() == b.size()), (a.dat * b.dat).sum(); } }; using u128= __uint128_t; using u64= uint64_t; using u8= uint8_t; class Ref { u128 *ref; u8 i; public: Ref(u128 *ref, u8 i): ref(ref), i(i) {} Ref &operator=(const Ref &r) { return *this= bool(r); } Ref &operator=(bool b) { return *ref&= ~(u128(1) << i), *ref|= u128(b) << i, *this; } Ref &operator|=(bool b) { return *ref|= u128(b) << i, *this; } Ref &operator&=(bool b) { return *ref&= ~(u128(!b) << i), *this; } Ref &operator^=(bool b) { return *ref^= u128(b) << i, *this; } operator bool() const { return (*ref >> i) & 1; } }; template <> class Vector { size_t n; public: valarray dat; Vector(): n(0) {} Vector(size_t n): n(n), dat((n + 127) >> 7) {} Vector(size_t n, bool b): n(n), dat(-u128(b), (n + 127) >> 7) { if (int k= n & 127; k) dat[dat.size() - 1]&= (u128(1) << k) - 1; } Vector(const initializer_list &v): n(v.size()), dat((n + 127) >> 7) { int i= 0; for (bool b: v) dat[i >> 7]|= u128(b) << (i & 127), ++i; } Ref operator[](int i) { return {begin(dat) + (i >> 7), u8(i & 127)}; } bool operator[](int i) const { return (dat[i >> 7] >> (i & 127)) & 1; } bool operator==(const Vector &r) const { if (dat.size() != r.dat.size()) return false; for (int i= dat.size(); i--;) if (dat[i] != r.dat[i]) return false; return true; } bool operator!=(const Vector &r) const { return !(*this == r); } explicit operator bool() const { return n; } Vector operator-() const { return Vector(*this); } Vector &operator+=(const Vector &r) { return dat^= r.dat, *this; } Vector &operator-=(const Vector &r) { return dat^= r.dat, *this; } Vector &operator*=(bool b) { return dat*= b, *this; } Vector operator+(const Vector &r) const { return Vector(*this)+= r; } Vector operator-(const Vector &r) const { return Vector(*this)-= r; } Vector operator*(bool b) const { return Vector(*this)*= b; } size_t size() const { return n; } friend bool dot(const Vector &a, const Vector &b) { assert(a.size() == b.size()); u128 v= 0; for (int i= a.dat.size(); i--;) v^= a.dat[i] & b.dat[i]; return __builtin_parityll(v >> 64) ^ __builtin_parityll(u64(v)); } }; template Vector operator*(const R &r, const Vector &v) { return v * r; } template ostream &operator<<(ostream &os, const Vector &v) { os << '['; for (int _= 0, __= v.size(); _ < __; ++_) os << (_ ? ", " : "") << v[_]; return os << ']'; } } using _la_internal::Vector; namespace _la_internal { template struct Mat { Mat(): W(0) {} Mat(size_t h, size_t w): W(w), dat(h * w) {} Mat(size_t h, size_t w, R v): W(w), dat(v, h * w) {} Mat(initializer_list> v): W(v.size() ? v.begin()->size() : 0), dat(v.size() * W) { auto it= begin(dat); for (const auto &r: v) { assert(r.size() == W); for (R x: r) *it++= x; } } size_t width() const { return W; } size_t height() const { return W ? dat.size() / W : 0; } auto operator[](int i) { return next(begin(dat), i * W); } auto operator[](int i) const { return next(begin(dat), i * W); } protected: size_t W; valarray dat; void add(const Mat &r) { assert(dat.size() == r.dat.size()), assert(W == r.W), dat+= r.dat; } D mul(const Mat &r) const { const size_t h= height(), w= r.W, l= W; assert(l == r.height()); D ret(h, w); auto a= begin(dat); auto c= begin(ret.dat); for (int i= h; i--; advance(c, w)) { auto b= begin(r.dat); for (int k= l; k--; ++a) { auto d= c; auto v= *a; for (int j= w; j--; ++b, ++d) *d+= v * *b; } } return ret; } Vector mul(const Vector &r) const { assert(W == r.size()); const size_t h= height(); Vector ret(h); auto a= begin(dat); for (size_t i= 0; i < h; ++i) for (size_t k= 0; k < W; ++k, ++a) ret[i]+= *a * r[k]; return ret; } }; template struct Mat { struct Array { u128 *bg; Array(u128 *it): bg(it) {} Ref operator[](int i) { return Ref{bg + (i >> 7), u8(i & 127)}; } bool operator[](int i) const { return (bg[i >> 7] >> (i & 127)) & 1; } }; struct ConstArray { const u128 *bg; ConstArray(const u128 *it): bg(it) {} bool operator[](int i) const { return (bg[i >> 7] >> (i & 127)) & 1; } }; Mat(): H(0), W(0), m(0) {} Mat(size_t h, size_t w): H(h), W(w), m((w + 127) >> 7), dat(h * m) {} Mat(size_t h, size_t w, bool b): H(h), W(w), m((w + 127) >> 7), dat(-u128(b), h * m) { if (size_t i= h, k= w & 127; k) for (u128 s= (u128(1) << k) - 1; i--;) dat[i * m]&= s; } Mat(const initializer_list> &v): H(v.size()), W(H ? v.begin()->size() : 0), m((W + 127) >> 7), dat(H * m) { auto it= begin(dat); for (const auto &r: v) { assert(r.size() == W); int i= 0; for (bool b: r) it[i >> 7]|= u128(b) << (i & 127), ++i; advance(it, m); } } size_t width() const { return W; } size_t height() const { return H; } Array operator[](int i) { return {next(begin(dat), i * m)}; } ConstArray operator[](int i) const { return {next(begin(dat), i * m)}; } ConstArray get(int i) const { return {next(begin(dat), i * m)}; } protected: size_t H, W, m; valarray dat; void add(const Mat &r) { assert(H == r.H), assert(W == r.W), dat^= r.dat; } D mul(const Mat &r) const { assert(W == r.H); D ret(H, r.W); u128 *c= begin(ret.dat); for (size_t i= 0; i < H; ++i, advance(c, r.m)) { ConstArray a= this->operator[](i); const u128 *b= begin(r.dat); for (size_t k= 0; k < W; ++k, advance(b, r.m)) if (a[k]) for (size_t j= 0; j < r.m; ++j) c[j]^= b[j]; } return ret; } Vector mul(const Vector &r) const { assert(W == r.size()); Vector ret(H); auto a= begin(dat); for (size_t i= 0; i < H; ++i) { u128 v= 0; for (size_t j= 0; j < m; ++j, ++a) v^= *a & r.dat[j]; ret[i]= __builtin_parityll(v >> 64) ^ __builtin_parityll(u64(v)); } return ret; } }; template struct Matrix: public Mat> { using Mat>::Mat; explicit operator bool() const { return this->W; } static Matrix identity_matrix(int n) { Matrix ret(n, n); for (; n--;) ret[n][n]= R(true); return ret; } Matrix submatrix(const vector &rows, const vector &cols) const { Matrix ret(rows.size(), cols.size()); for (int i= rows.size(); i--;) for (int j= cols.size(); j--;) ret[i][j]= (*this)[rows[i]][cols[j]]; return ret; } Matrix submatrix_rm(vector rows, vector cols) const { sort(begin(rows), end(rows)), sort(begin(cols), end(cols)), rows.erase(unique(begin(rows), end(rows)), end(rows)), cols.erase(unique(begin(cols), end(cols)), end(cols)); const int H= this->height(), W= this->width(), n= rows.size(), m= cols.size(); vector rs(H - n), cs(W - m); for (int i= 0, j= 0, k= 0; i < H; ++i) if (j < n && rows[j] == i) ++j; else rs[k++]= i; for (int i= 0, j= 0, k= 0; i < W; ++i) if (j < m && cols[j] == i) ++j; else cs[k++]= i; return submatrix(rs, cs); } bool operator==(const Matrix &r) const { if (this->width() != r.width() || this->height() != r.height()) return false; for (int i= this->dat.size(); i--;) if (this->dat[i] != r.dat[i]) return false; return true; } bool operator!=(const Matrix &r) const { return !(*this == r); } Matrix &operator*=(const Matrix &r) { return *this= this->mul(r); } Matrix operator*(const Matrix &r) const { return this->mul(r); } Matrix &operator*=(R r) { return this->dat*= r, *this; } template Matrix operator*(T r) const { static_assert(is_convertible_v); return Matrix(*this)*= r; } Matrix &operator+=(const Matrix &r) { return this->add(r), *this; } Matrix operator+(const Matrix &r) const { return Matrix(*this)+= r; } Vector operator*(const Vector &r) const { return this->mul(r); } Vector operator()(const Vector &r) const { return this->mul(r); } Matrix pow(uint64_t k) const { size_t W= this->width(); assert(W == this->height()); for (Matrix ret= identity_matrix(W), b= *this;; b*= b) if (k & 1 ? ret*= b, !(k>>= 1) : !(k>>= 1)) return ret; } }; template Matrix operator*(const T &r, const Matrix &m) { return m * r; } template ostream &operator<<(ostream &os, const Matrix &m) { os << "\n["; for (int i= 0, h= m.height(); i < h; os << ']', ++i) { if (i) os << "\n "; os << '['; for (int j= 0, w= m.width(); j < w; ++j) os << (j ? ", " : "") << m[i][j]; } return os << ']'; } template static bool is_zero(K x) { if constexpr (is_floating_point_v) return abs(x) < 1e-8; else return x == K(); } } using _la_internal::Matrix; namespace _la_internal { template class LU_Decomposition { Matrix dat; vector perm, piv; bool sgn; size_t psz; public: LU_Decomposition(const Matrix &A): dat(A), perm(A.height()), sgn(false), psz(0) { const size_t h= A.height(), w= A.width(); iota(perm.begin(), perm.end(), 0), piv.resize(min(w, h)); for (size_t c= 0, pos; c < w && psz < h; ++c) { pos= psz; if constexpr (is_floating_point_v) { for (size_t r= psz + 1; r < h; ++r) if (abs(dat[perm[pos]][c]) < abs(dat[perm[r]][c])) pos= r; } else if (is_zero(dat[perm[pos]][c])) for (size_t r= psz + 1; r < h; ++r) if (!is_zero(dat[perm[r]][c])) pos= r, r= h; if (is_zero(dat[perm[pos]][c])) continue; if (pos != psz) sgn= !sgn, swap(perm[pos], perm[psz]); const auto b= dat[perm[psz]]; for (size_t r= psz + 1, i; r < h; ++r) { auto a= dat[perm[r]]; K m= a[c] / b[c]; for (a[c]= K(), a[psz]= m, i= c + 1; i < w; ++i) a[i]-= b[i] * m; } piv[psz++]= c; } } size_t rank() const { return psz; } bool is_regular() const { return rank() == dat.height() && rank() == dat.width(); } K det() const { assert(dat.height() == dat.width()); K ret= sgn ? -1 : 1; for (size_t i= dat.width(); i--;) ret*= dat[perm[i]][i]; return ret; } vector> kernel() const { const size_t w= dat.width(), n= rank(); vector ker(w - n, Vector(w)); for (size_t c= 0, i= 0; c < w; ++c) { if (i < n && piv[i] == c) ++i; else { auto &a= ker[c - i]; a[c]= 1; for (size_t r= i; r--;) a[r]= -dat[perm[r]][c]; for (size_t j= i, k, r; j--;) { K x= a[j] / dat[perm[j]][k= piv[j]]; for (a[j]= 0, a[k]= x, r= j; r--;) a[r]-= dat[perm[r]][k] * x; } } } return ker; } Vector linear_equations(const Vector &b) const { const size_t h= dat.height(), w= dat.width(), n= rank(); assert(h == b.size()); Vector y(h), x(w); for (size_t c= 0; c < h; ++c) if (y[c]+= b[perm[c]]; c < w) for (size_t r= c + 1; r < h; ++r) y[r]-= y[c] * dat[perm[r]][c]; for (size_t i= n; i < h; ++i) if (!is_zero(y[i])) return Vector(); // no solution for (size_t i= n, r; i--;) for (x[piv[i]]= y[i] / dat[perm[i]][piv[i]], r= i; r--;) y[r]-= x[piv[i]] * dat[perm[r]][piv[i]]; return x; } Matrix inverse_matrix() const { if (!is_regular()) return Matrix(); // no solution const size_t n= dat.width(); Matrix ret(n, n); for (size_t i= 0; i < n; ++i) { Vector y(n); for (size_t c= 0; c < n; ++c) if (y[c]+= perm[c] == i; c < n && !is_zero(y[c])) for (size_t r= c + 1; r < n; ++r) y[r]-= y[c] * dat[perm[r]][c]; for (size_t j= n; j--;) { K m= ret[j][i]= y[j] / dat[perm[j]][j]; for (size_t r= j; r--;) y[r]-= m * dat[perm[r]][j]; } } return ret; } }; void add_upper(u128 *a, const u128 *b, size_t bg, size_t ed) { //[bg,ed) if (bg >= ed) return; size_t s= bg >> 7; a[s]^= b[s] & -(u128(1) << (bg & 127)); for (size_t i= (ed + 127) >> 7; --i > s;) a[i]^= b[i]; } void add_lower(u128 *a, const u128 *b, size_t ed) { //[0,ed) size_t s= ed >> 7; a[s]^= b[s] & ((u128(1) << (ed & 127)) - 1); for (size_t i= s; i--;) a[i]^= b[i]; } void subst_lower(u128 *a, const u128 *b, size_t ed) { //[0,ed) size_t s= ed >> 7; a[s]= b[s] & ((u128(1) << (ed & 127)) - 1); for (size_t i= s; i--;) a[i]= b[i]; } bool any1_upper(const u128 *a, size_t bg, size_t ed) { //[bg,ed) if (bg >= ed) return false; size_t s= bg >> 7; if (a[s] & -(u128(1) << (bg & 127))) return true; for (size_t i= (ed + 127) >> 7; --i > s;) if (a[i]) return true; return false; } template <> class LU_Decomposition { Matrix dat; vector perm, piv; size_t psz; public: LU_Decomposition(Matrix A): dat(A.width(), A.height()), perm(A.height()), psz(0) { const size_t h= A.height(), w= A.width(); iota(perm.begin(), perm.end(), 0), piv.resize(min(w, h)); for (size_t c= 0, pos; c < w && psz < h; ++c) { for (pos= psz; pos < h; ++pos) if (A.get(perm[pos])[c]) break; if (pos == h) continue; if (pos != psz) swap(perm[pos], perm[psz]); auto b= A.get(perm[psz]); for (size_t r= psz + 1; r < h; ++r) { auto a= A[perm[r]]; if (bool m= a[c]; m) add_upper(a.bg, b.bg, c, w), a[psz]= 1; } piv[psz++]= c; } for (size_t j= w; j--;) for (size_t i= h; i--;) dat[j][i]= A.get(perm[i])[j]; } size_t rank() const { return psz; } bool is_regular() const { return rank() == dat.height() && rank() == dat.width(); } bool det() const { return is_regular(); } vector> kernel() const { const size_t w= dat.height(), n= rank(); vector ker(w - rank(), Vector(w)); for (size_t c= 0, i= 0; c < w; ++c) { if (i < n && piv[i] == c) ++i; else { auto &a= ker[c - i]; subst_lower(begin(a.dat), dat[c].bg, i), a[c]= 1; for (size_t j= i, k; j--;) { bool x= a[j]; if (a[j]= 0, a[k= piv[j]]= x; x) add_lower(begin(a.dat), dat[k].bg, j); } } } return ker; } Vector linear_equations(const Vector &b) const { const size_t h= dat.width(), w= dat.height(), n= rank(); assert(h == b.size()); Vector y(h), x(w); for (size_t c= 0; c < h; ++c) if (y[c]^= b[perm[c]]; c < w && y[c]) add_upper(begin(y.dat), dat[c].bg, c + 1, h); if (any1_upper(begin(y.dat), n, h)) return Vector(); // no solution for (size_t i= n; i--;) if ((x[piv[i]]= y[i])) add_lower(begin(y.dat), dat[piv[i]].bg, i); return x; } Matrix inverse_matrix() const { if (!is_regular()) return Matrix(); // no solution const size_t n= dat.width(); Matrix ret(n, n); for (size_t i= 0; i < n; ++i) { Vector y(n); for (size_t c= 0; c < n; ++c) if (y[c]^= perm[c] == i; c < n && y[c]) add_upper(begin(y.dat), dat[c].bg, c, n); for (size_t j= n; j--;) if ((ret[j][i]= y[j])) add_lower(begin(y.dat), dat[j].bg, j); } return ret; } }; } using _la_internal::LU_Decomposition; using namespace std; signed main() { cin.tie(0); ios::sync_with_stdio(0); int N, M; cin >> N >> M; Matrix m(M, N); int Y[M]; for (int i= 0; i < M; ++i) { int A; cin >> A; for (int j= 0; j < A; ++j) { int B; cin >> B, --B; m[i][B]= 1; } cin >> Y[i]; } LU_Decomposition lud(m); int ans[N]; fill_n(ans, N, 0); for (int k= 0; k < 30; ++k) { Vector b(M); for (int i= 0; i < M; ++i) b[i]= (Y[i] >> k) & 1; auto sol= lud.linear_equations(b); if (!sol) return cout << -1 << '\n', 0; for (int i= 0; i < N; ++i) ans[i]|= sol[i] << k; } for (int i= 0; i < N; ++i) cout << ans[i] << '\n'; return 0; }