#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); for (int row = rank; row < a.m; ++row) { if (mat[row][a.n]) return std::vector(); } std::vector res(a.n, false); for (int i = 0, j; i < rank; ++i) { j = (i == 0 ? mat[i]._Find_first() : mat[i]._Find_next(j)); res[j] = mat[i][a.n]; } return res; } int main() { constexpr int N = 50, B = 30; using binary_matrix = BinaryMatrix; int n, m; std::cin >> n >> m; std::vector> b(m); std::vector y(m); for (int i = 0; i < m; ++i) { int a; std::cin >> a; b[i].resize(a); for (int j = 0; j < a; ++j) { std::cin >> b[i][j]; --b[i][j]; } std::cin >> y[i]; } std::vector x(n, 0); for (int bit = 0; bit < B; ++bit) { binary_matrix a(m, n, 0); std::vector v(m); for (int i = 0; i < m; ++i) { for (int bij : b[i]) a[i][bij] = 1; v[i] = y[i] >> bit & 1; } std::vector ans = linear_equation(a, v); if (ans.empty()) { std::cout << "-1\n"; return 0; } for (int i = 0; i < n; ++i) x[i] |= ans[i] << bit; } for (int i = 0; i < n; ++i) std::cout << x[i] << '\n'; return 0; }