結果

問題 No.1744 Selfish Spies 1 (à la Princess' Perfectionism)
ユーザー suisensuisen
提出日時 2022-12-25 01:16:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 28 ms / 5,000 ms
コード長 6,714 bytes
コンパイル時間 2,834 ms
コンパイル使用メモリ 230,228 KB
実行使用メモリ 8,204 KB
最終ジャッジ日時 2024-11-18 08:29:20
合計ジャッジ時間 4,793 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 2 ms
6,820 KB
testcase_04 AC 2 ms
6,820 KB
testcase_05 AC 2 ms
6,816 KB
testcase_06 AC 2 ms
6,816 KB
testcase_07 AC 2 ms
6,816 KB
testcase_08 AC 2 ms
6,816 KB
testcase_09 AC 2 ms
6,816 KB
testcase_10 AC 2 ms
6,816 KB
testcase_11 AC 2 ms
6,816 KB
testcase_12 AC 2 ms
6,820 KB
testcase_13 AC 2 ms
6,820 KB
testcase_14 AC 2 ms
6,820 KB
testcase_15 AC 2 ms
6,820 KB
testcase_16 AC 2 ms
6,816 KB
testcase_17 AC 3 ms
6,816 KB
testcase_18 AC 3 ms
6,816 KB
testcase_19 AC 2 ms
6,820 KB
testcase_20 AC 2 ms
6,820 KB
testcase_21 AC 2 ms
6,816 KB
testcase_22 AC 2 ms
6,820 KB
testcase_23 AC 3 ms
6,820 KB
testcase_24 AC 7 ms
6,816 KB
testcase_25 AC 3 ms
6,820 KB
testcase_26 AC 3 ms
6,820 KB
testcase_27 AC 4 ms
6,816 KB
testcase_28 AC 28 ms
8,076 KB
testcase_29 AC 4 ms
6,820 KB
testcase_30 AC 4 ms
6,816 KB
testcase_31 AC 4 ms
6,820 KB
testcase_32 AC 4 ms
6,820 KB
testcase_33 AC 28 ms
8,204 KB
testcase_34 AC 27 ms
8,076 KB
testcase_35 AC 28 ms
8,060 KB
testcase_36 AC 27 ms
8,060 KB
testcase_37 AC 28 ms
8,060 KB
testcase_38 AC 28 ms
8,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#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/abc223_g.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]) {
            cout << "Yes\n";
        } else {
            const int i = cl[u];
            if (i == 0 or i == k) {
                cout << "Yes\n";
            } else if (dm[i].first.size() >= 2) {
                cout << "Yes\n";
            } else {
                cout << "No\n";
            }
        }
    }
    return 0;
}
0