結果

問題 No.1078 I love Matrix Construction
ユーザー 👑 emthrmemthrm
提出日時 2020-06-12 21:49:26
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 191 ms / 2,000 ms
コード長 2,877 bytes
コンパイル時間 2,135 ms
コンパイル使用メモリ 207,720 KB
実行使用メモリ 48,280 KB
最終ジャッジ日時 2023-09-06 10:12:42
合計ジャッジ時間 6,098 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 17 ms
9,948 KB
testcase_03 AC 50 ms
20,340 KB
testcase_04 AC 75 ms
26,692 KB
testcase_05 AC 60 ms
22,756 KB
testcase_06 AC 17 ms
9,908 KB
testcase_07 AC 7 ms
5,744 KB
testcase_08 AC 56 ms
23,168 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 191 ms
48,280 KB
testcase_11 AC 86 ms
28,016 KB
testcase_12 AC 138 ms
40,448 KB
testcase_13 AC 183 ms
45,044 KB
testcase_14 AC 111 ms
32,680 KB
testcase_15 AC 162 ms
42,924 KB
testcase_16 AC 6 ms
5,156 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 13 ms
8,068 KB
testcase_19 AC 30 ms
14,240 KB
testcase_20 AC 29 ms
13,888 KB
testcase_21 AC 2 ms
4,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;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const 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() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

struct TwoSatLight {
  TwoSatLight(int n) : n(n), graph(n << 1), rev_graph(n << 1), used(n << 1, false), id(n << 1, -1) {}

  int negate(int x) { return (n + x) % (n << 1); }

  void add_or(int x, int y) {
    graph[negate(x)].emplace_back(y);
    graph[negate(y)].emplace_back(x);
    rev_graph[y].emplace_back(negate(x));
    rev_graph[x].emplace_back(negate(y));
  }

  void add_if(int x, int y) { add_or(negate(x), y); }

  void add_nand(int x, int y) { add_or(negate(x), negate(y)); }

  void set_true(int x) { add_or(x, x); }

  void set_false(int x) { set_true(negate(x)); }

  vector<bool> build() {
    REP(i, n << 1) {
      if (!used[i]) dfs(i);
    }
    int now = 0;
    for (int i = (n << 1) - 1; i >= 0; --i) {
      if (id[order[i]] == -1) rev_dfs(order[i], now++);
    }
    vector<bool> res(n);
    REP(i, n) {
      if (id[i] == id[negate(i)]) return {};
      res[i] = id[negate(i)] < id[i];
    }
    return res;
  }

private:
  int n;
  vector<vector<int>> graph, rev_graph;
  vector<bool> used;
  vector<int> id, order;

  void dfs(int ver) {
    used[ver] = true;
    for (int e : graph[ver]) {
      if (!used[e]) dfs(e);
    }
    order.emplace_back(ver);
  }

  void rev_dfs(int ver, int now) {
    id[ver] = now;
    for (int e : rev_graph[ver]) {
      if (id[e] == -1) rev_dfs(e, now);
    }
  }
};

int main() {
  int n; cin >> n;
  vector<int> s(n), t(n), u(n);
  REP(i, n) cin >> s[i], --s[i];
  REP(i, n) cin >> t[i], --t[i];
  REP(i, n) cin >> u[i];
  TwoSatLight two_sat(n * n);
  REP(i, n) REP(j, n) {
    int x = s[i] * n + j, y = j * n + t[i];
    if (u[i] == 0) {
      two_sat.add_or(x, y);
    } else if (u[i] == 1) {
      two_sat.add_if(x, y);
    } else if (u[i] == 2) {
      two_sat.add_if(y, x);
    } else if (u[i] == 3) {
      two_sat.add_or(two_sat.negate(x), two_sat.negate(y));
    }
  }
  vector<bool> ans = two_sat.build();
  if (ans.empty()) {
    cout << "-1\n";
    return 0;
  }
  int pos = 0;
  REP(i, n) REP(j, n) cout << ans[pos++] << " \n"[j + 1 == n];
  return 0;
}
0