結果

問題 No.1421 国勢調査 (Hard)
ユーザー 👑 emthrmemthrm
提出日時 2021-03-07 03:22:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 133 ms / 2,000 ms
コード長 4,489 bytes
コンパイル時間 2,544 ms
コンパイル使用メモリ 212,860 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-08 23:02:40
合計ジャッジ時間 4,752 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,820 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 2 ms
6,820 KB
testcase_04 AC 2 ms
6,820 KB
testcase_05 AC 2 ms
6,816 KB
testcase_06 AC 2 ms
6,820 KB
testcase_07 AC 2 ms
6,820 KB
testcase_08 AC 2 ms
6,820 KB
testcase_09 AC 2 ms
6,820 KB
testcase_10 AC 2 ms
6,816 KB
testcase_11 AC 2 ms
6,816 KB
testcase_12 AC 2 ms
6,820 KB
testcase_13 AC 2 ms
6,820 KB
testcase_14 AC 2 ms
6,816 KB
testcase_15 AC 2 ms
6,816 KB
testcase_16 AC 2 ms
6,816 KB
testcase_17 AC 2 ms
6,816 KB
testcase_18 AC 2 ms
6,816 KB
testcase_19 AC 2 ms
6,816 KB
testcase_20 AC 2 ms
6,816 KB
testcase_21 AC 2 ms
6,820 KB
testcase_22 AC 133 ms
6,820 KB
testcase_23 AC 133 ms
6,816 KB
testcase_24 AC 133 ms
6,816 KB
testcase_25 AC 133 ms
6,816 KB
testcase_26 AC 132 ms
6,816 KB
testcase_27 AC 25 ms
6,816 KB
testcase_28 AC 36 ms
6,816 KB
testcase_29 AC 29 ms
6,820 KB
testcase_30 AC 26 ms
6,820 KB
testcase_31 AC 28 ms
6,816 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<bool> linear_equation(const BinaryMatrix<Col> &a, const std::vector<bool> &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);
  for (int row = rank; row < a.m; ++row) {
    if (mat[row][a.n]) return std::vector<bool>();
  }
  std::vector<bool> 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<N>;
  int n, m;
  std::cin >> n >> m;
  std::vector<std::vector<int>> b(m);
  std::vector<int> 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<int> x(n, 0);
  for (int bit = 0; bit < B; ++bit) {
    binary_matrix a(m, n, 0);
    std::vector<bool> 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<bool> 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;
}
0