結果

問題 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,044 ms / 5,000 ms
コード長 6,404 bytes
コンパイル時間 2,704 ms
コンパイル使用メモリ 230,308 KB
実行使用メモリ 52,632 KB
最終ジャッジ日時 2024-11-18 08:30:56
合計ジャッジ時間 10,668 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 1 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 2 ms
5,248 KB
testcase_08 AC 2 ms
5,248 KB
testcase_09 AC 2 ms
5,248 KB
testcase_10 AC 2 ms
5,248 KB
testcase_11 AC 2 ms
5,248 KB
testcase_12 AC 2 ms
5,248 KB
testcase_13 AC 3 ms
5,248 KB
testcase_14 AC 3 ms
5,248 KB
testcase_15 AC 3 ms
5,248 KB
testcase_16 AC 3 ms
5,248 KB
testcase_17 AC 3 ms
5,248 KB
testcase_18 AC 3 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 2 ms
5,248 KB
testcase_23 AC 2 ms
5,248 KB
testcase_24 AC 9 ms
5,248 KB
testcase_25 AC 3 ms
5,248 KB
testcase_26 AC 2 ms
5,248 KB
testcase_27 AC 4 ms
5,248 KB
testcase_28 AC 25 ms
8,076 KB
testcase_29 AC 4 ms
5,248 KB
testcase_30 AC 4 ms
5,248 KB
testcase_31 AC 4 ms
5,248 KB
testcase_32 AC 4 ms
5,248 KB
testcase_33 AC 25 ms
8,076 KB
testcase_34 AC 25 ms
8,076 KB
testcase_35 AC 25 ms
7,932 KB
testcase_36 AC 26 ms
8,056 KB
testcase_37 AC 26 ms
8,056 KB
testcase_38 AC 25 ms
7,936 KB
testcase_39 AC 16 ms
6,272 KB
testcase_40 AC 23 ms
8,104 KB
testcase_41 AC 24 ms
8,232 KB
testcase_42 AC 49 ms
13,012 KB
testcase_43 AC 67 ms
16,436 KB
testcase_44 AC 93 ms
20,296 KB
testcase_45 AC 424 ms
33,364 KB
testcase_46 AC 375 ms
33,236 KB
testcase_47 AC 265 ms
52,080 KB
testcase_48 AC 272 ms
51,948 KB
testcase_49 AC 151 ms
13,880 KB
testcase_50 AC 146 ms
14,156 KB
testcase_51 AC 60 ms
7,456 KB
testcase_52 AC 37 ms
11,112 KB
testcase_53 AC 35 ms
11,108 KB
testcase_54 AC 121 ms
19,964 KB
testcase_55 AC 122 ms
19,976 KB
testcase_56 AC 1,044 ms
19,624 KB
testcase_57 AC 258 ms
52,632 KB
testcase_58 AC 253 ms
52,596 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