#define _USE_MATH_DEFINES #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 1000000007; // constexpr int MOD = 998244353; constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; template struct BinaryMatrix { int m, n; BinaryMatrix(int m, int n = Col, bool def = false) : m(m), n(n), dat(m, std::bitset(0)) { if (def) { for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) dat[i][j] = 1; } } BinaryMatrix pow(long long exponent) const { BinaryMatrix tmp = *this, res(n, n); for (int i = 0; i < n; ++i) res[i][i] = 1; while (exponent > 0) { if (exponent & 1) res *= tmp; tmp *= tmp; exponent >>= 1; } return res; } inline const std::bitset &operator[](const int idx) const { return dat[idx]; } inline std::bitset &operator[](const int idx) { return dat[idx]; } BinaryMatrix &operator=(const BinaryMatrix &x) { m = x.m; n = x.n; dat.resize(m); for (int i = 0; i < m; ++i) dat[i] = x[i]; return *this; } BinaryMatrix &operator+=(const BinaryMatrix &x) { for (int i = 0; i < m; ++i) dat[i] ^= x[i]; return *this; } BinaryMatrix &operator*=(const BinaryMatrix &x) { int height = m, width = x.n; BinaryMatrix t_x(x.n, x.m), res(height, width); for (int i = 0; i < x.n; ++i) for (int j = 0; j < x.m; ++j) t_x[i][j] = x[j][i]; for (int i = 0; i < height; ++i) for (int j = 0; j < width; ++j) res[i][j] = ((dat[i] & t_x[j]).count() & 1); *this = res; return *this; } BinaryMatrix operator+(const BinaryMatrix &x) const { return BinaryMatrix(*this) += x; } BinaryMatrix operator*(const BinaryMatrix &x) const { return BinaryMatrix(*this) *= x; } private: std::vector> dat; }; template int gauss_jordan(BinaryMatrix &mat, bool is_extended = false) { int rank = 0; for (int col = 0; col < mat.n; ++col) { if (is_extended && col == mat.n - 1) break; int pivot = -1; for (int row = rank; row < mat.m; ++row) { if (mat[row][col]) { pivot = row; break; } } if (pivot == -1) continue; std::swap(mat[rank], mat[pivot]); for (int row = 0; row < mat.m; ++row) { if (row != rank && mat[row][col]) mat[row] ^= mat[rank]; } ++rank; } return rank; } template std::vector linear_equation(const BinaryMatrix &a, const std::vector &b) { BinaryMatrix mat(a.m, a.n + 1); for (int i = 0; i < a.m; ++i) { for (int j = 0; j < a.n; ++j) mat[i][j] = a[i][j]; mat[i][a.n] = b[i]; } int rank = gauss_jordan(mat, true); std::vector res; for (int row = rank; row < a.m; ++row) { if (mat[row][a.n]) return res; } res.assign(a.n, 0); for (int i = 0; i < rank; ++i) { FOR(j, i, a.n) { if (mat[i][j]) { res[j] = mat[i][a.n]; break; } } } return res; } struct Xor128 { int rand() { unsigned int t = x ^ (x << 11); x = y; y = z; z = w; w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); return static_cast(w); } int rand(int ub) { int res = rand() % ub; return res < 0 ? res + ub : res; } int rand(int lb, int ub) { return lb + rand(ub - lb); } long long randll() { unsigned long long res = static_cast(rand()) << 32; return static_cast(res | rand()); } long long randll(long long ub) { long long res = randll() % ub; return res < 0 ? res + ub : res; } long long randll(long long lb, long long ub) { return lb + randll(ub - lb); } private: unsigned int x = 123456789, y = 362436069, z = 521288629, w = static_cast(std::time(nullptr)); } xor128; int main() { constexpr int N = 50, B = 30; using binary_matrix = BinaryMatrix; int n, m; cin >> n >> m; vector> b(m); vector y(m); // vector house(n); // REP(i, n) house[i] = xor128.rand(0, 1 << B); REP(i, m) { int a; cin >> a; // int a = xor128.rand(1, n + 1); b[i].resize(a); REP(j, a) cin >> b[i][j], --b[i][j]; cin >> y[i]; // vector is_in_b(n, false); // while (count(ALL(is_in_b), true) < a) is_in_b[xor128.rand(n)] = true; // REP(j, n) { // if (is_in_b[j]) { // b[i].emplace_back(j); // y[i] ^= house[j]; // } // } } // REP(i, n) cout << house[i] << " \n"[i + 1 == n]; // REP(i, m) { // REP(j, b[i].size()) cout << b[i][j] << ' '; // cout << ": " << y[i] << '\n'; // } vector ans(n, 0); REP(bit, B) { binary_matrix a(m, n, 0); vector v(m); REP(i, m) { for (int bij : b[i]) a[i][bij] = 1; v[i] = y[i] >> bit & 1; } vector sol = linear_equation(a, v); if (sol.empty()) { cout << "-1\n"; return 0; } REP(i, n) ans[i] |= sol[i] << bit; } REP(i, n) cout << ans[i] << '\n'; // REP(i, n) cout << ans[i] << " \n"[i + 1 == n]; // REP(i, m) { // int xo = 0; // REP(j, b[i].size()) xo ^= ans[b[i][j]]; // cout << xo << " \n"[i + 1 == m]; // } return 0; }