結果

問題 No.1745 Selfish Spies 2 (à la Princess' Perfectionism)
ユーザー hashiryohashiryo
提出日時 2023-03-07 15:49:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 91 ms / 5,000 ms
コード長 10,309 bytes
コンパイル時間 2,800 ms
コンパイル使用メモリ 222,308 KB
実行使用メモリ 15,060 KB
最終ジャッジ日時 2023-10-18 05:31:22
合計ジャッジ時間 10,782 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 3 ms
4,348 KB
testcase_18 AC 3 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 7 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 4 ms
4,348 KB
testcase_28 AC 29 ms
7,444 KB
testcase_29 AC 3 ms
4,348 KB
testcase_30 AC 3 ms
4,348 KB
testcase_31 AC 4 ms
4,348 KB
testcase_32 AC 3 ms
4,348 KB
testcase_33 AC 29 ms
7,444 KB
testcase_34 AC 27 ms
7,444 KB
testcase_35 AC 32 ms
7,444 KB
testcase_36 AC 31 ms
7,444 KB
testcase_37 AC 31 ms
7,444 KB
testcase_38 AC 30 ms
7,444 KB
testcase_39 AC 8 ms
4,348 KB
testcase_40 AC 16 ms
5,640 KB
testcase_41 AC 14 ms
4,848 KB
testcase_42 AC 31 ms
8,044 KB
testcase_43 AC 47 ms
10,928 KB
testcase_44 AC 63 ms
12,540 KB
testcase_45 AC 73 ms
10,436 KB
testcase_46 AC 75 ms
10,436 KB
testcase_47 AC 83 ms
14,532 KB
testcase_48 AC 86 ms
14,532 KB
testcase_49 AC 76 ms
11,932 KB
testcase_50 AC 67 ms
8,764 KB
testcase_51 AC 17 ms
4,848 KB
testcase_52 AC 16 ms
5,192 KB
testcase_53 AC 17 ms
5,184 KB
testcase_54 AC 64 ms
12,540 KB
testcase_55 AC 58 ms
9,108 KB
testcase_56 AC 91 ms
12,700 KB
testcase_57 AC 83 ms
15,060 KB
testcase_58 AC 82 ms
15,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
// clang-format off
std::ostream&operator<<(std::ostream&os,std::int8_t x){return os<<(int)x;}
std::ostream&operator<<(std::ostream&os,std::uint8_t x){return os<<(int)x;}
std::ostream&operator<<(std::ostream&os,const __int128_t &v){if(!v)os<<"0";__int128_t tmp=v<0?(os<<"-",-v):v;std::string s;while(tmp)s+='0'+(tmp%10),tmp/=10;return std::reverse(s.begin(),s.end()),os<<s;}
std::ostream&operator<<(std::ostream&os,const __uint128_t &v){if(!v)os<<"0";__uint128_t tmp=v;std::string s;while(tmp)s+='0'+(tmp%10),tmp/=10;return std::reverse(s.begin(),s.end()),os<<s;}

#define checkpoint() (void(0))
#define debug(x) (void(0))
#define debugArray(x,n) (void(0))
#define debugMatrix(x,h,w) (void(0))
// clang-format on
#ifdef __LOCAL
// clang-format off
#undef checkpoint
#undef debug
#undef debugArray
#undef debugMatrix
template<class T, class U>std::ostream &operator<<(std::ostream&os,const std::pair<T,U>&x){return os<<"("<<x.first<<", "<<x.second<<")";}
template<typename T>std::ostream &operator<<(std::ostream&os,const std::vector<T>&vec){os<<'[';for(int _=0,__= vec.size();_<__;++_)os<<(_ ?", ":"")<<vec[_];return os<<']';}
template<typename T>std::ostream &operator<<(std::ostream&os,const std::set<T>&s){os<<'{';int _=0;for(const auto &x:s)os<<(_++ ? ", " : "")<<x; return os << '}';}
template<typename T,std::size_t _Nm>std::ostream&operator<<(std::ostream &os,const std::array<T, _Nm> &arr) {os<<'['<<arr[0];for(std::size_t _=1;_<_Nm;++_)os<<", "<<arr[_];return os<<']';}
template<class Tup,std::size_t... I>void print(std::ostream&os,const Tup &x,std::index_sequence<I...>){(void)(int[]){(os<<std::get<I>(x)<<", ",0)...};}
template<class... Args>std::ostream &operator<<(std::ostream&os,const std::tuple<Args...> &x) {static constexpr std::size_t N = sizeof...(Args);os<<"(";if constexpr(N>=2)print(os,x,std::make_index_sequence<N-1>());return os<<std::get<N-1>(x)<<")";}
const std::string COLOR_RESET="\033[0m",BRIGHT_GREEN="\033[1;32m",BRIGHT_RED="\033[1;31m",BRIGHT_CYAN="\033[1;36m",NORMAL_CROSSED="\033[0;9;37m",ITALIC="\033[3m",BOLD="\033[1m",RED_BACKGROUND="\033[1;41m",NORMAL_FAINT="\033[0;2m";
#define func_LINE_FILE  NORMAL_FAINT<<" in "<<BOLD<<__func__<<NORMAL_FAINT<<ITALIC<<" (L"<<__LINE__<<") "<< __FILE__<<COLOR_RESET
#define checkpoint() std::cerr<<BRIGHT_RED<<"< check point! >"<<func_LINE_FILE<<'\n'
#define debug(x) std::cerr<<BRIGHT_CYAN<<#x<<COLOR_RESET<<" = "<<(x)<<func_LINE_FILE<<'\n'
#define debugArray(x, n) do{std::cerr<<BRIGHT_CYAN<<#x<<COLOR_RESET<<" = ["<<x[0];for(int _=1;_<(int)(n);++_)std::cerr<<", "<<x[_];std::cerr<<"]"<<func_LINE_FILE<<'\n';}while(0)
#define debugMatrix(x, h, w) do{std::cerr<<BRIGHT_CYAN<<#x<<"\n"<<COLOR_RESET<<"= ";for(int _=0;(_)<(int)(h);++_){std::cerr<<((_?"   [":"[["));for(int __=0;__<(int)(w);++__)std::cerr<<((__?", ":""))<<x[_][__];std::cerr<<"]"<<(_+1==(int)(h)?"]":",\n");}std::cerr<<func_LINE_FILE<<'\n';}while(0)
#endif
// clang-format on

template <class T> struct ListRange {
 using Iterator= typename std::vector<T>::const_iterator;
 Iterator bg, ed;
 Iterator begin() const { return bg; }
 Iterator end() const { return ed; }
 size_t size() const { return std::distance(bg, ed); }
 const T &operator[](int i) const { return bg[i]; }
};
template <class T> class CsrArray {
 std::vector<T> csr;
 std::vector<int> pos;
public:
 CsrArray()= default;
 CsrArray(const std::vector<T> &c, const std::vector<int> &p): csr(c), pos(p) {}
 size_t size() const { return pos.size() - 1; }
 const ListRange<T> operator[](int i) const { return {csr.begin() + pos[i], csr.begin() + pos[i + 1]}; }
};
class StronglyConnectedComponents {
 std::vector<std::array<int, 2>> es;
 std::vector<int> csr, pos, id;
public:
 StronglyConnectedComponents(int n): csr(n, -2), id(n) {}
 void add_edge(int src, int dst) { es.push_back({src, dst}); }
 void build() {
  const int n= id.size();
  std::vector<int> g(es.size()), sep(n + 1), ord(n);
  for (auto [s, d]: es) ++sep[s];
  for (int i= 0; i < n; ++i) sep[i + 1]+= sep[i];
  for (auto [s, d]: es) g[--sep[s]]= d;
  std::vector<int> dat(sep.begin(), sep.begin() + n);
  int k= n, p;
  for (int s= 0; s < n; ++s)
   if (csr[s] == -2)
    for (csr[p= s]= -1; p >= 0;) {
     if (dat[p] == sep[p + 1]) ord[--k]= p, p= csr[p];
     else if (int q= g[dat[p]++]; csr[q] == -2) csr[q]= p, p= q;
    }
  sep.assign(n + 1, 0), pos= {p= 0};
  for (auto [s, d]: es) ++sep[d];
  for (int i= 0; i < n; ++i) sep[i + 1]+= sep[i];
  for (auto [s, d]: es) g[--sep[d]]= s;
  for (int s: ord)
   if (dat[s] >= 0) {
    for (csr[k++]= s, dat[s]= -1; p < k; ++p)
     for (int v= csr[p], j= sep[v], u; j < sep[v + 1]; ++j)
      if (dat[u= g[j]] >= 0) dat[u]= -1, csr[k++]= u;
    pos.push_back(k);
   }
  for (int i= pos.size() - 1; i--;)
   while (k > pos[i]) id[csr[--k]]= i;
 }
 int components_num() const { return pos.size() - 1; }
 const ListRange<int> block(int k) const { return {csr.begin() + pos[k], csr.begin() + pos[k + 1]}; }
 int belong(int i) const { return id[i]; }
 const CsrArray<int> dag() const {
  std::vector<std::array<int, 2>> es_;
  for (auto [s, d]: es)
   if (s= id[s], d= id[d]; s != d) es_.push_back({s, d});
  std::sort(es_.begin(), es_.end()), es_.erase(std::unique(es_.begin(), es_.end()), es_.end());
  std::vector<int> g(es_.size()), p(pos.size());
  for (auto [s, d]: es_) ++p[s];
  std::partial_sum(p.begin(), p.end(), p.begin());
  for (auto [s, d]: es_) g[--p[s]]= d;
  return {g, p};
 }
};
class BipartiteMatching {
 std::vector<std::array<int, 2>> es;
 std::vector<int> lmate, rmate;
public:
 BipartiteMatching() {}
 BipartiteMatching(int L, int R): lmate(L, -1), rmate(R, -1) {}
 void add_edge(int l, int r) { es.push_back({l, r}); }
 void erase_edge(int l, int r) {
  auto it= std::find(es.begin(), es.end(), std::array{l, r});
  if (assert(it != es.end()), es.erase(it); lmate[l] == r) lmate[l]= rmate[r]= -1;
 }
 template <bool lex= false> void build() {
  const int n= lmate.size();
  std::vector<int> g(es.size()), pos(n + 1), rt, pre, que(n);
  if constexpr (lex) std::sort(es.rbegin(), es.rend());
  for (auto [l, r]: es) ++pos[l];
  for (int i= 0; i < n; ++i) pos[i + 1]+= pos[i];
  for (auto [l, r]: es) g[--pos[l]]= r;
  for (bool upd= true; upd;) {
   upd= false, rt.assign(n, -1), pre.assign(n, -1);
   int t= 0;
   for (int l= n; l--;)
    if (lmate[l] == -1) que[t++]= rt[l]= pre[l]= l;
   for (int i= 0; i < t; ++i)
    if (int l= que[i]; lmate[rt[l]] == -1)
     for (int j= pos[l], r, nl; j < pos[l + 1]; ++j) {
      if (nl= rmate[r= g[j]]; nl == -1) {
       for (upd= true; r != -1; l= pre[l]) rmate[r]= l, std::swap(lmate[l], r);
       break;
      }
      if (pre[nl] == -1) rt[que[t++]= nl]= rt[pre[nl]= l];
     }
  }
  if constexpr (lex) {
   rt.assign(n, 1);
   for (int v= 0, l, r; v < n; ++v)
    if (int u= lmate[v]; u != -1) {
     for (pre.assign(n, lmate[v]= rmate[u]= -1), pre[l= v]= -2, que.assign(pos.begin(), pos.begin() + n);;) {
      if (que[l] == pos[l + 1]) l= pre[l];
      else if (r= g[que[l]++], u= rmate[r]; u == -1) {
       for (; r != -1; l= pre[l]) rmate[r]= l, std::swap(lmate[l], r);
       break;
      } else if (rt[u] && pre[u] == -1) pre[u]= l, l= u;
     }
     rt[v]= 0;
    }
  }
 }
 inline size_t left_size() const { return lmate.size(); }
 inline size_t right_size() const { return rmate.size(); }
 inline int l_to_r(int l) const { return lmate[l]; }
 inline int r_to_l(int r) const { return rmate[r]; }
 std::vector<std::array<int, 2>> edges() const { return es; }
 std::vector<std::array<int, 2>> max_matching() const {
  std::vector<std::array<int, 2>> ret;
  for (int l= 0, n= lmate.size(); l < n; ++l)
   if (int r= lmate[l]; r != -1) ret.push_back({l, r});
  return ret;
 }
};
class DulmageMendelsohn {
 std::vector<int> lblg, rblg;
public:
 DulmageMendelsohn(const BipartiteMatching &bm) {
  const auto es= bm.edges();
  const int n= bm.left_size(), m= bm.right_size();
  std::vector<int> g(es.size()), sep(n + 1), que(n + m);
  lblg.assign(n, -3), rblg.assign(m, -3);
  for (int l= n; l--;)
   if (bm.l_to_r(l) == -1) lblg[l]= -1;
  for (int r= m; r--;)
   if (bm.r_to_l(r) == -1) rblg[r]= -2;

  for (auto [l, r]: es) ++sep[l];
  for (int i= 0; i < n; ++i) sep[i + 1]+= sep[i];
  for (auto [l, r]: es) g[--sep[l]]= r;
  for (int l= n, i= 0, t= 0; l--;)
   if (bm.l_to_r(l) == -1)
    for (que[t++]= l; i < t; ++i)
     for (int v= que[i], j= sep[v], u, w; j < sep[v + 1]; ++j)
      if (rblg[u= g[j]] == -3)
       if (rblg[u]= -1, w= bm.r_to_l(u); w != -1 && lblg[w] == -3) lblg[que[t++]= w]= -1;

  sep.assign(m + 1, 0);
  for (auto [l, r]: es) ++sep[r];
  for (int i= 0; i < m; ++i) sep[i + 1]+= sep[i];
  for (auto [l, r]: es) g[--sep[r]]= l;
  for (int r= m, i= 0, t= 0; r--;)
   if (bm.r_to_l(r) == -1)
    for (que[t++]= r; i < t; ++i)
     for (int v= que[i], j= sep[v], u, w; j < sep[v + 1]; ++j)
      if (lblg[u= g[j]] == -3)
       if (lblg[u]= -2, w= bm.l_to_r(u); w != -1 && rblg[w] == -3) rblg[que[t++]= w]= -2;

  int t= 0;
  for (int l= n; l--;)
   if (lblg[l] == -3) lblg[l]= t, que[t++]= l;
  for (int r= m; r--;)
   if (rblg[r] == -3) rblg[r]= t, que[t++]= r + n;
  StronglyConnectedComponents scc(t);
  for (int i= t, r;;) {
   if (r= que[--i] - n; r < 0) break;
   scc.add_edge(i, lblg[bm.r_to_l(r)]);
   for (int j= sep[r]; j < sep[r + 1]; ++j)
    if (int l= g[j], k= lblg[l]; k >= 0) scc.add_edge(k, i);
  }
  scc.build(), t= scc.components_num();
  debug(lblg);
  debug(rblg);
  for (int l= n, v; l--;) v= lblg[l], lblg[l]= v == -1 ? t + 1 : v == -2 ? 0 : scc.belong(v) + 1;
  for (int r= m, v; r--;) v= rblg[r], rblg[r]= v == -1 ? t + 1 : v == -2 ? 0 : scc.belong(v) + 1;
 }
 int componetns_num() const { return -1; /*todo*/ }
 int left_belong(int l) const { return lblg[l]; }
 int right_belong(int r) const { return rblg[r]; }
};
using namespace std;
namespace yukicoder1745 {
signed main() {
 cin.tie(0);
 ios::sync_with_stdio(0);
 int N, M, L;
 cin >> N >> M >> L;
 BipartiteMatching bm(N, M);
 int S[L], T[L];
 for (int i= 0; i < L; ++i) {
  cin >> S[i] >> T[i];
  bm.add_edge(--S[i], --T[i]);
 }
 bm.build();
 DulmageMendelsohn dm(bm);

 for (int i= 0; i < L; ++i) {
  cout << (dm.left_belong(S[i]) == dm.right_belong(T[i]) ? "Yes" : "No") << '\n';
 }
 return 0;
}
}
signed main() {
 cin.tie(0);
 ios::sync_with_stdio(0);
 yukicoder1745::main();
 return 0;
}
0