結果

問題 No.1421 国勢調査 (Hard)
ユーザー 👑 emthrmemthrm
提出日時 2021-03-05 22:13:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,209 bytes
コンパイル時間 2,319 ms
コンパイル使用メモリ 209,908 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-07 02:22:33
合計ジャッジ時間 5,629 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 2 ms
5,248 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 2 ms
5,248 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 2 ms
5,248 KB
testcase_13 AC 2 ms
5,248 KB
testcase_14 AC 2 ms
5,248 KB
testcase_15 AC 2 ms
5,248 KB
testcase_16 WA -
testcase_17 AC 2 ms
5,248 KB
testcase_18 AC 2 ms
5,248 KB
testcase_19 AC 2 ms
5,248 KB
testcase_20 AC 2 ms
5,248 KB
testcase_21 AC 2 ms
5,248 KB
testcase_22 AC 134 ms
5,248 KB
testcase_23 AC 134 ms
5,248 KB
testcase_24 AC 134 ms
5,248 KB
testcase_25 AC 134 ms
5,248 KB
testcase_26 AC 133 ms
5,248 KB
testcase_27 AC 26 ms
5,248 KB
testcase_28 AC 37 ms
5,248 KB
testcase_29 AC 29 ms
5,248 KB
testcase_30 AC 25 ms
5,248 KB
testcase_31 AC 29 ms
5,248 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) res[i] = mat[i][a.n];
  return res;
}

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);
  REP(i, m) {
    int a; cin >> a;
    b[i].resize(a);
    REP(j, a) cin >> b[i][j], --b[i][j];
    cin >> y[i];
  }
  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';
  return 0;
}
0