結果

問題 No.1745 Selfish Spies 2 (à la Princess' Perfectionism)
ユーザー optopt
提出日時 2021-11-11 10:29:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 513 ms / 5,000 ms
コード長 4,105 bytes
コンパイル時間 5,228 ms
コンパイル使用メモリ 288,600 KB
実行使用メモリ 39,092 KB
最終ジャッジ日時 2023-08-20 04:57:05
合計ジャッジ時間 12,931 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 3 ms
4,384 KB
testcase_15 AC 2 ms
4,384 KB
testcase_16 AC 3 ms
4,384 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 3 ms
4,384 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,384 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 9 ms
4,980 KB
testcase_25 AC 3 ms
4,384 KB
testcase_26 AC 3 ms
4,384 KB
testcase_27 AC 5 ms
4,380 KB
testcase_28 AC 42 ms
12,716 KB
testcase_29 AC 5 ms
4,380 KB
testcase_30 AC 5 ms
4,384 KB
testcase_31 AC 5 ms
4,380 KB
testcase_32 AC 5 ms
4,380 KB
testcase_33 AC 41 ms
12,664 KB
testcase_34 AC 40 ms
12,648 KB
testcase_35 AC 44 ms
12,496 KB
testcase_36 AC 43 ms
12,484 KB
testcase_37 AC 43 ms
12,512 KB
testcase_38 AC 40 ms
12,664 KB
testcase_39 AC 21 ms
6,016 KB
testcase_40 AC 34 ms
10,116 KB
testcase_41 AC 32 ms
8,544 KB
testcase_42 AC 75 ms
17,196 KB
testcase_43 AC 112 ms
23,032 KB
testcase_44 AC 154 ms
28,748 KB
testcase_45 AC 392 ms
31,056 KB
testcase_46 AC 374 ms
30,428 KB
testcase_47 AC 511 ms
39,020 KB
testcase_48 AC 513 ms
39,092 KB
testcase_49 AC 163 ms
21,952 KB
testcase_50 AC 149 ms
21,284 KB
testcase_51 AC 44 ms
8,356 KB
testcase_52 AC 48 ms
10,208 KB
testcase_53 AC 51 ms
10,332 KB
testcase_54 AC 167 ms
27,224 KB
testcase_55 AC 158 ms
23,664 KB
testcase_56 AC 295 ms
25,656 KB
testcase_57 AC 473 ms
38,968 KB
testcase_58 AC 470 ms
38,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#include<atcoder/all>
using namespace atcoder;
#define rep_(i, a_, b_, a, b, ...) for (int i = (a), lim##i = (b); i < lim##i; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__) // rep(i, a): [0, a); rep(i, a, b): [a, b)
struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;


struct dm_decomposition {
  using edge = pair<int, int>;
  int r, c;
  vector<edge> E;
  dm_decomposition(int _r, int _c) : r(_r), c(_c) {}
  void add_edge(int i, int j) { E.emplace_back(i, j); }

  pair<vector<vector<int>>, vector<vector<int>>> decompose() {
    int n = r + c, m = E.size();
    int s = n, t = s + 1;
    vector<vector<array<int, 3>>> G(n);
    vector<bool> is_used_V(n);
    {
      // max flow
      atcoder::mf_graph<int> mfg(n + 2);
      for (auto [i, j] : E) mfg.add_edge(i, r + j, 1);
      for (int i = 0; i < r; i++) mfg.add_edge(s, i, 1);
      for (int j = 0; j < c; j++) mfg.add_edge(r + j, t, 1);
      mfg.flow(s, t);

      // construct graph
      for (int k = 0; k < m; k++) {
        auto e = mfg.get_edge(k);
        if (e.flow == 1) {
          G[e.from].push_back({k, e.to, 3});
          G[e.to].push_back({k, e.from, 3});
          is_used_V[e.from] = is_used_V[e.to] = true;
        }
        else {
          G[e.from].push_back({k, e.to, 1});
          G[e.to].push_back({k, e.from, 2});
        }
      }
    }

    vector<int> V_0, V_inf;
    {
      // check reachability
      vector<int> R_unused, C_unused;
      for (int u = 0; u < r; u++) if (!is_used_V[u]) R_unused.push_back(u);
      for (int u = r; u < n; u++) if (!is_used_V[u]) C_unused.push_back(u);
      auto bfs = [&] (vector<int>& S, bool fwd) {
        vector<bool> seen(n);
        queue<int> que;
        for (auto u : S) seen[u] = true, que.push(u);
        while (!que.empty()) {
          auto u = que.front(); que.pop();
          for (auto [id, v, d] : G[u]) {
            if ((fwd && (d >> 0 & 1)) || (!fwd && (d >> 1 & 1))) {
              if (seen[v] == false) seen[v] = true, que.push(v);
            }
          }
        }
        vector<int> res;
        for (int u = 0; u < n; u++) if (seen[u]) res.push_back(u);
        return res;
      };
      V_inf = bfs(R_unused, true);
      V_0 = bfs(C_unused, false);
    }

    // erase nodes and edges
    vector<bool> is_erased_V(n), is_erased_E(m);
    for (auto u : V_0) {
      is_erased_V[u] = true;
      for (auto [id, v, d] : G[u]) is_erased_E[id] = true;
    }
    for (auto u : V_inf) {
      is_erased_V[u] = true;
      for (auto [id, v, d] : G[u]) is_erased_E[id] = true;
    }

    vector<vector<int>> Rs, Cs;
    auto push_nodes = [&] (vector<int>& V) {
      if (V.empty()) return;
      Rs.push_back({}), Cs.push_back({});
      for (auto u : V) {
        if (u < r) Rs.back().push_back(u);
        else Cs.back().push_back(u - r);
      }
    };
    push_nodes(V_0);

    // scc decomposition
    atcoder::scc_graph sccg(n);
    for (int u = 0; u < n; u++) {
      for (auto [id, v, d] : G[u]) {
        if ((d & 1) && !is_erased_E[id]) sccg.add_edge(u, v);
      }
    }
    for (auto vs : sccg.scc()) {
      if (is_erased_V[vs.front()]) continue;
      push_nodes(vs);
    }
    push_nodes(V_inf);
    return {Rs, Cs};
  }
};


void solve() {
  int n, m, l; cin >> n >> m >> l;
//   assert(1 <= n && n <= int(1e5));
//   assert(1 <= m && m <= int(1e5));
//   assert(1 <= l && l <= int(2e5));

  dm_decomposition dm(n, m);
//   set<pair<int, int>> S;
  rep(i, l) {
    int s, t; cin >> s >> t;
    // assert(1 <= s && s <= n);
    // assert(1 <= t && t <= m);
    // S.emplace(s, t);
    --s, --t;
    dm.add_edge(s, t);
  }
//   assert(S.size() == l);

  auto [rs, cs] = dm.decompose();

  vector<int> cid_R(n), cid_C(m);
  int cnum = rs.size();
  rep(k, cnum) for (auto i : rs[k]) cid_R[i] = k;
  rep(k, cnum) for (auto i : cs[k]) cid_C[i] = k;

  for (auto [i, j] : dm.E) {
    int p = cid_R[i], q = cid_C[j];
    cout << (p == q ? "Yes" : "No") << '\n';
  }
}


int main() {
  solve();
}
0