結果

問題 No.1421 国勢調査 (Hard)
ユーザー 👑 emthrmemthrm
提出日時 2021-03-05 22:51:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 134 ms / 2,000 ms
コード長 5,790 bytes
コンパイル時間 2,287 ms
コンパイル使用メモリ 204,352 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 10:39:11
合計ジャッジ時間 4,603 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 134 ms
5,376 KB
testcase_23 AC 132 ms
5,376 KB
testcase_24 AC 133 ms
5,376 KB
testcase_25 AC 133 ms
5,376 KB
testcase_26 AC 132 ms
5,376 KB
testcase_27 AC 26 ms
5,376 KB
testcase_28 AC 36 ms
5,376 KB
testcase_29 AC 29 ms
5,376 KB
testcase_30 AC 26 ms
5,376 KB
testcase_31 AC 29 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
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 <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> 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 <int Col = 2500>
struct BinaryMatrix {
  int m, n;

  BinaryMatrix(int m, int n = Col, bool def = false) : m(m), n(n), dat(m, std::bitset<Col>(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<Col> &operator[](const int idx) const { return dat[idx]; }
  inline std::bitset<Col> &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<std::bitset<Col>> dat;
};

template <int Col>
int gauss_jordan(BinaryMatrix<Col> &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 <int Col>
std::vector<int> linear_equation(const BinaryMatrix<Col> &a, const std::vector<int> &b) {
  BinaryMatrix<Col> 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<int> 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<int>(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<unsigned long long>(rand()) << 32;
    return static_cast<long long>(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<unsigned int>(std::time(nullptr));
} xor128;

int main() {
  constexpr int N = 50, B = 30;
  using binary_matrix = BinaryMatrix<N>;
  int n, m; cin >> n >> m;
  vector<vector<int>> b(m);
  vector<int> y(m);
  // vector<int> 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<bool> 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<int> ans(n, 0);
  REP(bit, B) {
    binary_matrix a(m, n, 0);
    vector<int> v(m);
    REP(i, m) {
      for (int bij : b[i]) a[i][bij] = 1;
      v[i] = y[i] >> bit & 1;
    }
    vector<int> 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;
}
0