結果
問題 | No.1421 国勢調査 (Hard) |
ユーザー |
![]() |
提出日時 | 2021-03-05 22:55:20 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 24 ms / 2,000 ms |
コード長 | 7,988 bytes |
コンパイル時間 | 2,312 ms |
コンパイル使用メモリ | 208,324 KB |
最終ジャッジ日時 | 2025-01-19 11:50:03 |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 30 |
ソースコード
#include <bits/stdc++.h>using namespace std;using lint = long long;using pint = pair<int, int>;using plint = pair<lint, lint>;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##_end_;i++)#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)#define REP(i, n) FOR(i,0,n)#define IREP(i, n) IFOR(i,0,n)template <typename T, typename V>void ndarray(vector<T>& vec, const V& val, int len) { vec.assign(len, val); }template <typename T, typename V, typename... Args> void ndarray(vector<T>& vec, const V& val, int len, Args... args) { vec.resize(len), for_each(begin(vec), end(vec), [&](T& v) { ndarray(v, val, args...); }); }template <typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }template <typename T> 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 <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }template <typename T> vector<T> sort_unique(vector<T> vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end());return vec; }template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }template <typename T, size_t sz> ostream &operator<<(ostream &os, const array<T, sz> &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']';return os; }#if __cplusplus >= 201703Ltemplate <typename... T> istream &operator>>(istream &is, tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); returnis; }template <typename... T> ostream &operator<<(ostream &os, const tuple<T...> &tpl) { std::apply([&os](auto &&... args) { ((os << args << ','), ...);},tpl); return os; }#endiftemplate <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os;}template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }template <typename T, typename TH> ostream &operator<<(ostream &os, const unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ',';os << '}'; return os; }template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os;}template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}';return os; }template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << '(' << pa.first << ',' << pa.second << ')';return os; }template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }template <typename TK, typename TV, typename TH> ostream &operator<<(ostream &os, const unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp)os << v.first << "=>" << v.second << ','; os << '}'; return os; }#ifdef HITONANODE_LOCALconstexpr 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#elseconstexpr int Wmax = 50 + 1;#define dbg(x) (x)#endifstd::vector<std::bitset<Wmax>> gauss_jordan(int W, std::vector<std::bitset<Wmax>> 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<std::bitset<Wmax>> &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<std::bitset<Wmax>> matmul(const std::vector<std::bitset<Wmax>> &A, const std::vector<std::bitset<Wmax>> &B) {int H = A.size(), K = B.size();std::vector<std::bitset<Wmax>> 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<std::bitset<Wmax>> matpower(std::vector<std::bitset<Wmax>> X, long long n) {int D = X.size();std::vector<std::bitset<Wmax>> 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<bool, std::bitset<Wmax>, std::vector<std::bitset<Wmax>>> system_of_linear_equations(std::vector<std::bitset<Wmax>> A, std::bitset<Wmax> b,int W) {int H = A.size();assert(W + 1 <= Wmax);assert(H <= Wmax);std::vector<std::bitset<Wmax>> M = A;for (int i = 0; i < H; i++) M[i][W] = b[i];M = gauss_jordan(W + 1, M);std::vector<int> 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<Wmax>(), std::vector<std::bitset<Wmax>>()); }if (j < W) ss[j] = i;}std::bitset<Wmax> x;std::vector<std::bitset<Wmax>> D;for (int j = 0; j < W; j++) {if (ss[j] == -1) {std::bitset<Wmax> 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);} elsex[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<pair<ull, int>> basis;while (M--) {int a;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<int> ret(N);REP(d, 30) {vector<bitset<Wmax>> A;bitset<Wmax> b;b.reset();REP(i, basis.size()) {b[i] = ((basis[i].second >> d) & 1);bitset<Wmax> 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';}