#include using namespace std; using lint = long long; using pint = pair; using plint = pair; struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template void ndarray(vector& vec, const V& val, int len) { vec.assign(len, val); } template void ndarray(vector& vec, const V& val, int len, Args... args) { vec.resize(len), for_each(begin(vec), end(vec), [&](T& v) { ndarray(v, val, args...); }); } template bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; } template bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; } int floor_lg(long long x) { return x <= 0 ? -1 : 63 - __builtin_clzll(x); } template pair operator+(const pair &l, const pair &r) { return make_pair(l.first + r.first, l.second + r.second); } template pair operator-(const pair &l, const pair &r) { return make_pair(l.first - r.first, l.second - r.second); } template vector sort_unique(vector vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } template istream &operator>>(istream &is, vector &vec) { for (auto &v : vec) is >> v; return is; } template ostream &operator<<(ostream &os, const vector &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; } template ostream &operator<<(ostream &os, const array &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; } #if __cplusplus >= 201703L template istream &operator>>(istream &is, tuple &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; } template ostream &operator<<(ostream &os, const tuple &tpl) { std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os; } #endif template ostream &operator<<(ostream &os, const deque &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; } template ostream &operator<<(ostream &os, const set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const unordered_set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const unordered_multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const pair &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; } template ostream &operator<<(ostream &os, const map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const unordered_map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } #ifdef HITONANODE_LOCAL constexpr int Wmax = 10 + 1; const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m"; #define dbg(x) cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << endl #else constexpr int Wmax = 50 + 1; #define dbg(x) (x) #endif std::vector> gauss_jordan(int W, std::vector> mtr) { int H = mtr.size(), c = 0; for (int h = 0; h < H; h++, c++) { if (c == W) break; int piv = -1; for (int j = h; j < H; j++) if (mtr[j][c]) { piv = j; break; } if (piv == -1) { h--; continue; } std::swap(mtr[piv], mtr[h]); for (int hh = 0; hh < H; hh++) { if (hh != h and mtr[hh][c]) mtr[hh] ^= mtr[h]; } } return mtr; } int rank_gauss_jordan(int W, const std::vector> &mtr) // Rank of Gauss-Jordan eliminated matrix { for (int h = (int)mtr.size() - 1; h >= 0; h--) { if (mtr[h]._Find_first() < W) return h + 1; } return 0; } std::vector> matmul(const std::vector> &A, const std::vector> &B) { int H = A.size(), K = B.size(); std::vector> C(H); for (int i = 0; i < H; i++) { for (int j = 0; j < K; j++) { if (A[i][j]) C[i] ^= B[j]; } } return C; } std::vector> matpower(std::vector> X, long long n) { int D = X.size(); std::vector> ret(D); for (int i = 0; i < D; i++) ret[i][i] = 1; while (n) { if (n & 1) ret = matmul(ret, X); X = matmul(X, X), n >>= 1; } return ret; } // Solve Ax = b on F_2 // - retval: {true, one of the solutions, {freedoms}} (if solution exists) // {false, {}, {}} (otherwise) std::tuple, std::vector>> system_of_linear_equations(std::vector> A, std::bitset b, int W) { int H = A.size(); assert(W + 1 <= Wmax); assert(H <= Wmax); std::vector> M = A; for (int i = 0; i < H; i++) M[i][W] = b[i]; M = gauss_jordan(W + 1, M); std::vector ss(W, -1); for (int i = 0; i < H; i++) { int j = M[i]._Find_first(); if (j == W) { return std::make_tuple(false, std::bitset(), std::vector>()); } if (j < W) ss[j] = i; } std::bitset x; std::vector> D; for (int j = 0; j < W; j++) { if (ss[j] == -1) { std::bitset d; d[j] = 1; for (int jj = 0; jj < W; jj++) if (ss[jj] != -1) d[jj] = M[ss[jj]][j]; D.emplace_back(d); } else x[j] = M[ss[j]][W]; } return std::make_tuple(true, x, D); } void bad() { puts("-1"); exit(0); } int main() { int N, M; cin >> N >> M; using ull = unsigned long long; vector> basis; while (M--) { int a = 2; // cin >> a; ull mask = 0; while (a--) { int b; cin >> b; b--; mask += 1ULL << b; } int y; cin >> y; for (auto [v, w] : basis) { if (chmin(mask, mask ^ v)) y ^= w; } if (!mask and y) bad(); if (mask) basis.emplace_back(mask, y); } vector ret(N); REP(d, 30) { vector> A; bitset b; b.reset(); REP(i, basis.size()) { b[i] = ((basis[i].second >> d) & 1); bitset a; a.reset(); REP(j, N) if ((basis[i].first >> j) & 1) a[j] = 1; A.emplace_back(a); } auto [ok, solution, freedoms] = system_of_linear_equations(A, b, N); if (!ok) bad(); REP(i, N) ret[i] += int(solution[i]) << d; } for (auto x : ret) cout << x << '\n'; }