結果

問題 No.1745 Selfish Spies 2 (à la Princess' Perfectionism)
ユーザー hashiryohashiryo
提出日時 2023-03-07 17:47:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 6,593 bytes
コンパイル時間 2,540 ms
コンパイル使用メモリ 215,760 KB
実行使用メモリ 11,944 KB
最終ジャッジ日時 2023-10-18 05:33:55
合計ジャッジ時間 8,341 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 RE -
testcase_04 AC 2 ms
4,348 KB
testcase_05 RE -
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 1 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 2 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 6 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 3 ms
4,348 KB
testcase_28 AC 25 ms
5,656 KB
testcase_29 AC 3 ms
4,348 KB
testcase_30 AC 3 ms
4,348 KB
testcase_31 AC 3 ms
4,348 KB
testcase_32 AC 3 ms
4,348 KB
testcase_33 AC 25 ms
5,656 KB
testcase_34 AC 25 ms
5,656 KB
testcase_35 AC 30 ms
5,656 KB
testcase_36 AC 30 ms
5,656 KB
testcase_37 AC 28 ms
5,656 KB
testcase_38 AC 27 ms
5,656 KB
testcase_39 AC 8 ms
4,348 KB
testcase_40 AC 14 ms
4,644 KB
testcase_41 AC 14 ms
4,644 KB
testcase_42 AC 28 ms
5,992 KB
testcase_43 AC 41 ms
7,292 KB
testcase_44 AC 55 ms
8,640 KB
testcase_45 AC 72 ms
9,968 KB
testcase_46 AC 77 ms
9,968 KB
testcase_47 AC 78 ms
11,944 KB
testcase_48 AC 78 ms
11,944 KB
testcase_49 AC 73 ms
8,296 KB
testcase_50 AC 68 ms
8,296 KB
testcase_51 AC 17 ms
4,644 KB
testcase_52 AC 15 ms
4,988 KB
testcase_53 AC 17 ms
5,252 KB
testcase_54 AC 58 ms
8,640 KB
testcase_55 AC 57 ms
8,640 KB
testcase_56 AC 83 ms
8,680 KB
testcase_57 AC 79 ms
11,944 KB
testcase_58 AC 76 ms
11,944 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In member function 'void DulmageMendelsohn::build()':
main.cpp:41:30: warning: narrowing conversion of '((DulmageMendelsohn*)this)->DulmageMendelsohn::blg[0].std::vector<int>::size()' from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing]
   41 |   const int n[]= {blg[0].size(), blg[1].size()};
      |                   ~~~~~~~~~~~^~
main.cpp:41:45: warning: narrowing conversion of '((DulmageMendelsohn*)this)->DulmageMendelsohn::blg[1].std::vector<int>::size()' from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing]
   41 |   const int n[]= {blg[0].size(), blg[1].size()};
      |                                  ~~~~~~~~~~~^~

ソースコード

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

class DulmageMendelsohn {
 std::vector<int> blg[2];
 std::vector<std::array<int, 2>> es;
public:
 DulmageMendelsohn(int L, int R): blg{std::vector(L, -3), std::vector(R, -3)} {}
 void add_edge(int l, int r) { es.push_back({l, r}); }
 void build() {
  const int n[]= {blg[0].size(), blg[1].size()};
  std::vector<int> g[2], pos[2], rt, pre, mate[2], que(std::max(n[0], n[1]));

  g[0].resize(es.size()), g[1].resize(es.size()), pos[0].resize(n[0] + 1), pos[1].resize(n[1] + 1);
  mate[0].assign(n[0], -1), mate[1].assign(n[1], -1);
  for (auto [l, r]: es) ++pos[0][l], ++pos[1][r];
  std::partial_sum(pos[0].begin(), pos[0].end(), pos[0].begin()), std::partial_sum(pos[1].begin(), pos[1].end(), pos[1].begin());
  for (auto [l, r]: es) g[0][--pos[0][l]]= r, g[1][--pos[1][r]]= l;

  int t;

  for (bool upd= true; upd;) {
   upd= false, rt.assign(n[0], -1), pre.assign(n[0], -1), t= 0;
   for (int l= n[0]; l--;)
    if (mate[0][l] == -1) que[t++]= rt[l]= pre[l]= l;
   for (int i= 0; i < t; ++i)
    if (int l= que[i]; mate[0][rt[l]] == -1)
     for (int j= pos[0][l], r, nl; j < pos[0][l + 1]; ++j) {
      if (nl= mate[1][r= g[0][j]]; nl == -1) {
       for (upd= true; r != -1; l= pre[l]) mate[1][r]= l, std::swap(mate[0][l], r);
       break;
      }
      if (pre[nl] == -1) rt[que[t++]= nl]= rt[pre[nl]= l];
     }
  }

  for (int l= n[0]; l--;)
   if (mate[0][l] == -1) blg[0][l]= -1;
  for (int r= n[1]; r--;)
   if (mate[1][r] == -1) blg[1][r]= -2;

  for (int s= n[0], i= t= 0; s--;)
   if (mate[0][s] == -1)
    for (que[t++]= s; i < t; ++i)
     for (int v= que[i], j= pos[0][v], u, w; j < pos[0][v + 1]; ++j)
      if (blg[1][u= g[0][j]] == -3)
       if (blg[1][u]= -1, w= mate[1][u]; w != -1 && blg[0][w] == -3) blg[0][que[t++]= w]= -1;

  for (int s= n[1], i= t= 0; s--;)
   if (mate[1][s] == -1)
    for (que[t++]= s; i < t; ++i)
     for (int v= que[i], j= pos[1][v], u, w; j < pos[1][v + 1]; ++j)
      if (blg[0][u= g[1][j]] == -3)
       if (blg[0][u]= -2, w= mate[0][u]; w != -1 && blg[1][w] == -3) blg[1][que[t++]= w]= -2;

  std::vector<int> ei(pos[0].begin(), pos[0].begin() + n[0]);
  int oi= 0;
  for (int s= n[0], v; s--;)
   if (blg[0][s] == -3)
    for (blg[0][v= s]= n[0] * 2; v < n[0];) {
     if (ei[v] == pos[0][v + 1]) rt[oi++]= v, v= blg[0][v] - n[0];
     else if (int u= g[0][ei[v]++]; blg[1][u] == -3)
      if (int w= mate[1][u]; blg[0][w] == -3) blg[0][w]= v + n[0], v= w;
    }

  int k= 0;
  for (int i= t= 0; oi--;)
   if (int s= rt[oi]; blg[0][s] >= n[0]) {
    int p= mate[0][s];
    que[t++]= p, blg[0][s]= blg[1][p]= ++k;
    for (; i < t; ++i) {
     for (int v= que[i], j= pos[1][v], u, w; j < pos[1][v + 1]; ++j) {
      if (blg[0][u= g[1][j]] >= n[0]) {
       int w= mate[0][u];
       que[t++]= w, blg[0][u]= blg[1][w]= k;
      }
     }
    }
   }
  ++k;
  for (int l= n[0]; l--;) {
   if (blg[0][l] == -1) blg[0][l]= k;
   else if (blg[0][l] == -2) blg[0][l]= 0;
  }
  for (int r= n[1]; r--;) {
   if (blg[1][r] == -1) blg[1][r]= k;
   else if (blg[1][r] == -2) blg[1][r]= 0;
  }
 }
 int left_belong(int l) const { return blg[0][l]; }
 int right_belong(int r) const { return blg[1][r]; }
};
using namespace std;
namespace yukicoder1745 {
signed main() {
 cin.tie(0);
 ios::sync_with_stdio(0);
 int N, M, L;
 cin >> N >> M >> L;
 DulmageMendelsohn dm(N, M);
 int S[L], T[L];
 for (int i= 0; i < L; ++i) {
  cin >> S[i] >> T[i];
  dm.add_edge(--S[i], --T[i]);
 }
 dm.build();
 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