結果

問題 No.1745 Selfish Spies 2 (à la Princess' Perfectionism)
ユーザー suisensuisen
提出日時 2022-12-25 01:19:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,186 ms / 5,000 ms
コード長 6,404 bytes
コンパイル時間 2,849 ms
コンパイル使用メモリ 228,748 KB
実行使用メモリ 52,628 KB
最終ジャッジ日時 2024-04-29 07:10:11
合計ジャッジ時間 11,752 ms
ジャッジサーバーID
(参考情報)
judge4 / 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 3 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 3 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 3 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 4 ms
5,376 KB
testcase_18 AC 4 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 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 8 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 5 ms
5,376 KB
testcase_28 AC 29 ms
8,076 KB
testcase_29 AC 4 ms
5,376 KB
testcase_30 AC 4 ms
5,376 KB
testcase_31 AC 4 ms
5,376 KB
testcase_32 AC 5 ms
5,376 KB
testcase_33 AC 29 ms
8,072 KB
testcase_34 AC 28 ms
8,076 KB
testcase_35 AC 30 ms
7,848 KB
testcase_36 AC 31 ms
7,932 KB
testcase_37 AC 30 ms
7,932 KB
testcase_38 AC 29 ms
7,940 KB
testcase_39 AC 19 ms
6,144 KB
testcase_40 AC 26 ms
8,096 KB
testcase_41 AC 27 ms
8,104 KB
testcase_42 AC 58 ms
12,884 KB
testcase_43 AC 79 ms
16,436 KB
testcase_44 AC 110 ms
20,288 KB
testcase_45 AC 535 ms
33,364 KB
testcase_46 AC 482 ms
33,240 KB
testcase_47 AC 336 ms
52,020 KB
testcase_48 AC 312 ms
51,892 KB
testcase_49 AC 172 ms
13,752 KB
testcase_50 AC 167 ms
14,156 KB
testcase_51 AC 66 ms
7,452 KB
testcase_52 AC 42 ms
11,116 KB
testcase_53 AC 40 ms
11,004 KB
testcase_54 AC 144 ms
19,960 KB
testcase_55 AC 141 ms
20,104 KB
testcase_56 AC 1,186 ms
19,628 KB
testcase_57 AC 324 ms
52,500 KB
testcase_58 AC 326 ms
52,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "test/graph/dm/yuki1745.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/1745"

#line 2 "src/graph/BipartiteMatching.hpp"

#line 2 "src/Template.hpp"

#define CUT
#include <bits/stdc++.h>

using namespace std;

#define rep(i, l, r) for (int i = (l); i < (r); ++i)
#define rrep(i, l, r) for (int i = (r); i --> (l);)
#define all(c) begin(c), end(c)

#ifdef LOCAL
#define debug(...) debug_impl(#__VA_ARGS__, __VA_ARGS__)
template <class H, class... Ts> void debug_impl(string s, H&& h, Ts&&... ts) {
  cerr << '(' << s << "): (" << forward<H>(h);
  ((cerr << ", " << forward<Ts>(ts)), ..., (cerr << ")\n"));
}
#else
#define debug(...) void(0)
#endif

template <class T> bool chmax(T& a, const T& b) { return b > a ? (a = b, true) : false; }
template <class T> bool chmin(T& a, const T& b) { return b < a ? (a = b, true) : false; }

template <class T> istream& operator>>(istream& in, vector<T>& v) {
  for (auto& e : v) in >> e;
  return in;
}
template <class ...Args> void read(Args&... args) {
  (cin >> ... >> args);
}
template <class T> ostream& operator<<(ostream& out, const vector<T>& v) {
  int n = v.size();
  rep(i, 0, n) {
    out << v[i];
    if (i + 1 != n) out << ' ';
  }
  return out;
}
template <class H, class ...Ts> void print(H&& h, Ts &&... ts) {
  cout << h, ((cout << ' ' << forward<Ts>(ts)), ..., (cout << '\n'));
}

struct io_setup_ {
  io_setup_() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    cout << fixed << setprecision(10);
  }
} io_setup{};
#undef CUT

#define NOTE compile command: \texttt{g++ -std=gnu++17 -Wall -Wextra -g -fsanitize=address -fsanitize=undefined \$\{file\} -o \$\{fileDirname\}/\$\{fileBasenameNoExtension\}}
#undef NOTE
#define NOTE \texttt{-DLOCAL} を加えると \texttt{debug(...)} による出力が有効となる
#undef NOTE
#line 4 "src/graph/BipartiteMatching.hpp"

#define CUT
vector<int> bipartite_matching(const vector<vector<int>>& g) {
  const int n = g.size();
  vector<int> res(n, -1);
  vector<int8_t> use(n);
  auto dfs = [&](auto dfs, int v) {
    if (exchange(use[v], true)) return false;
    for (auto u : g[v]) if (res[u] < 0 or dfs(dfs, res[u])) {
      return res[u] = v, res[v] = u, true;
    }
    return false;
  };
  for (bool upd = true; exchange(upd, false);) {
    use.assign(n, false);
    rep(v, 0, n) upd |= res[v] == -1 and dfs(dfs, v);
  }
  return res;
}
#undef CUT
#line 3 "src/graph/DMDecomp.hpp"
#define CUT
#line 2 "src/graph/SCC.hpp"

#line 4 "src/graph/SCC.hpp"

#define CUT
// Returns (# of SCC, SCC id) (id: topological order)
pair<int, vector<int>> scc_ids(const vector<vector<int>>& g) {
  int n = g.size();
  int num = 0;
  int t = 0;
  vector<int> cmp, low(n), ord(n, -1), id(n);
  cmp.reserve(n);
  auto dfs = [&](auto self, int v) -> void {
    low[v] = ord[v] = t++;
    cmp.push_back(v);
    for (int to : g[v]) {
      if (ord[to] == -1) {
        self(self, to);
        chmin(low[v], low[to]);
      } else {
        chmin(low[v], ord[to]);
      }
    }
    if (low[v] != ord[v]) return;
    while (true) {
      int u = cmp.back();
      cmp.pop_back();
      ord[u] = n;
      id[u] = num;
      if (u == v) break;
    }
    num++;
  };
  rep(i, 0, n) if (ord[i] == -1) dfs(dfs, i);
  for (auto& x : id) x = num - 1 - x;
  return { num, id };
}
// Return vector of SCCs in topological order
vector<vector<int>> scc(const vector<vector<int>>& g) {
  auto [k, ids] = scc_ids(g);
  vector<vector<int>> cmps(k);
  int n = g.size();
  rep(i, 0, n) cmps[ids[i]].push_back(i);
  return cmps;
}
#undef CUT
#line 5 "src/graph/DMDecomp.hpp"
vector<pair<vector<int>, vector<int>>> dm_decomp(int lsiz, int rsiz, vector<pair<int, int>> match, vector<pair<int, int>> edges) {
  vector<int> tor(lsiz, -1), tol(rsiz, -1);
  for (auto &[i, j] : match) {
    tor[i] = j;
    tol[j] = i;
  }
  vector<vector<int>> g(lsiz), h(rsiz);
  for (auto &[i, j] : edges) {
    g[i].push_back(j);
    h[j].push_back(i);
  }

  vector<int8_t> wk_l(lsiz, false), wk_r(rsiz, false);
  auto dfs_l = [&](auto dfs_l, int i) -> void {
    if (i == -1 or exchange(wk_l[i], true)) return;
    for (int j : g[i]) wk_r[j] = true, dfs_l(dfs_l, tol[j]);
  };
  rep(i, 0, lsiz) if (tor[i] == -1) dfs_l(dfs_l, i);

  vector<int8_t> w0_l(lsiz, false), w0_r(rsiz, false);
  auto dfs_r = [&](auto dfs_r, int j) -> void {
    if (j == -1 or exchange(w0_r[j], true)) return;
    for (int i : h[j]) w0_l[i] = true, dfs_r(dfs_r, tor[i]);
  };
  rep(j, 0, rsiz) if (tol[j] == -1) dfs_r(dfs_r, j);

  vector<pair<vector<int>, vector<int>>> dm;
  auto add = [&](int i, int j) {
    auto& [l, r] = dm.back();
    l.push_back(i), r.push_back(j);
  };
  // W_0
  dm.emplace_back();
  rep(i, 0, lsiz) if (w0_l[i]) {
    add(i, tor[i]);
  }
  rep(j, 0, rsiz) if (w0_r[j] and tol[j] == -1) {
    dm.back().second.push_back(j);
  }
  // W_1, ..., W_{k-1}
  vector<vector<int>> scc_g(lsiz + rsiz);
  for (int i = 0; i < lsiz; ++i) {
    for (int j : g[i]) scc_g[i].push_back(lsiz + j);
    int j = tor[i];
    if (j != -1) scc_g[lsiz + j].push_back(i);
  }
  for (const auto& group : scc(scc_g)) {
    if (int v0 = group.front(); v0 < lsiz) {
      if (w0_l[v0] or wk_l[v0]) continue;
    } else {
      v0 -= lsiz;
      if (w0_r[v0] or wk_r[v0]) continue;
    }
    dm.emplace_back();
    for (int i : group) if (i < lsiz) add(i, tor[i]);
  }
  // W_k
  dm.emplace_back();
  rep(j, 0, rsiz) if (wk_r[j]) {
    add(tol[j], j);
  }
  rep(i, 0, lsiz) if (wk_l[i] and tor[i] == -1) {
    dm.back().first.push_back(i);
  }
  return dm;
}
#undef CUT
#line 5 "test/graph/dm/yuki1745.test.cpp"

int main() {
  int n, m, l;
  read(n, m, l);

  vector<pair<int, int>> edges;
  vector<vector<int>> g(n + m);
  for (int i = 0; i < l; ++i) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    g[u].push_back(n + v);
    g[n + v].push_back(u);
    edges.emplace_back(u, v);
  }
  vector<int> matched = bipartite_matching(g);
  vector<pair<int, int>> match;
  rep(i, 0, n) {
    if (matched[i] != -1) {
      match.emplace_back(i, matched[i] - n);
    }
  }

  auto dm = dm_decomp(n, m, match, edges);
  const int k = dm.size() - 1;

  vector<int> cl(n), cr(m);
  for (int i = 0; i <= k; ++i) {
    for (int u : dm[i].first)  cl[u] = i;
    for (int v : dm[i].second) cr[v] = i;
  }
  
  for (const auto& [u, v] : edges) {
    if (cl[u] != cr[v]) {
      std::cout << "No\n";
    } else {
      std::cout << "Yes\n";
    }
  }
  return 0;
}
0