結果

問題 No.1745 Selfish Spies 2 (à la Princess' Perfectionism)
ユーザー 👑 kmjpkmjp
提出日時 2021-11-16 02:23:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,124 ms / 5,000 ms
コード長 4,488 bytes
コンパイル時間 2,900 ms
コンパイル使用メモリ 226,528 KB
実行使用メモリ 86,608 KB
最終ジャッジ日時 2023-08-21 21:53:55
合計ジャッジ時間 18,668 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
20,704 KB
testcase_01 AC 9 ms
20,608 KB
testcase_02 AC 9 ms
20,724 KB
testcase_03 AC 9 ms
20,652 KB
testcase_04 AC 9 ms
20,624 KB
testcase_05 AC 10 ms
20,668 KB
testcase_06 AC 9 ms
20,708 KB
testcase_07 AC 9 ms
20,704 KB
testcase_08 AC 10 ms
20,792 KB
testcase_09 AC 9 ms
20,732 KB
testcase_10 AC 9 ms
20,692 KB
testcase_11 AC 9 ms
20,716 KB
testcase_12 AC 10 ms
20,960 KB
testcase_13 AC 10 ms
20,736 KB
testcase_14 AC 11 ms
20,848 KB
testcase_15 AC 11 ms
20,976 KB
testcase_16 AC 13 ms
20,924 KB
testcase_17 AC 15 ms
21,124 KB
testcase_18 AC 15 ms
21,120 KB
testcase_19 AC 9 ms
20,960 KB
testcase_20 AC 10 ms
20,708 KB
testcase_21 AC 11 ms
21,032 KB
testcase_22 AC 10 ms
20,772 KB
testcase_23 AC 11 ms
20,812 KB
testcase_24 AC 43 ms
22,280 KB
testcase_25 AC 13 ms
20,976 KB
testcase_26 AC 14 ms
21,004 KB
testcase_27 AC 20 ms
21,180 KB
testcase_28 AC 172 ms
29,060 KB
testcase_29 AC 20 ms
21,500 KB
testcase_30 AC 20 ms
21,392 KB
testcase_31 AC 20 ms
21,372 KB
testcase_32 AC 20 ms
21,504 KB
testcase_33 AC 170 ms
28,404 KB
testcase_34 AC 167 ms
28,820 KB
testcase_35 AC 180 ms
28,116 KB
testcase_36 AC 178 ms
28,520 KB
testcase_37 AC 177 ms
28,464 KB
testcase_38 AC 167 ms
28,448 KB
testcase_39 AC 63 ms
24,108 KB
testcase_40 AC 111 ms
27,056 KB
testcase_41 AC 102 ms
25,912 KB
testcase_42 AC 216 ms
33,596 KB
testcase_43 AC 305 ms
38,472 KB
testcase_44 AC 418 ms
44,208 KB
testcase_45 AC 833 ms
58,456 KB
testcase_46 AC 917 ms
58,184 KB
testcase_47 AC 1,124 ms
85,108 KB
testcase_48 AC 1,076 ms
85,388 KB
testcase_49 AC 453 ms
37,292 KB
testcase_50 AC 425 ms
34,976 KB
testcase_51 AC 136 ms
26,084 KB
testcase_52 AC 121 ms
30,560 KB
testcase_53 AC 137 ms
31,124 KB
testcase_54 AC 430 ms
43,864 KB
testcase_55 AC 400 ms
40,092 KB
testcase_56 AC 732 ms
46,312 KB
testcase_57 AC 944 ms
86,608 KB
testcase_58 AC 962 ms
86,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

template<class V> class MaxFlow_dinic {
public:
	struct edge { int to,reve;V cap;};
	static const int MV = 202020;
	vector<edge> E[MV];
	int itr[MV],lev[MV];
	void add_edge(int x,int y,V cap,bool undir=false) {
		E[x].push_back((edge){y,(int)E[y].size(),cap});
		E[y].push_back((edge){x,(int)E[x].size()-1,undir?cap:0});
	}
	void bfs(int cur) {
		MINUS(lev);
		queue<int> q;
		lev[cur]=0;
		q.push(cur);
		while(q.size()) {
			int v=q.front(); q.pop();
			FORR(e,E[v]) if(e.cap>0 && lev[e.to]<0) lev[e.to]=lev[v]+1, q.push(e.to);
		}
	}
	V dfs(int from,int to,V cf) {
		if(from==to) return cf;
		for(;itr[from]<E[from].size();itr[from]++) {
			edge* e=&E[from][itr[from]];
			if(e->cap>0 && lev[from]<lev[e->to]) {
				V f=dfs(e->to,to,min(cf,e->cap));
				if(f>0) {
					e->cap-=f;
					E[e->to][e->reve].cap += f;
					return f;
				}
			}
		}
		return 0;
	}
	V maxflow(int from, int to) {
		V fl=0,tf;
		while(1) {
			bfs(from);
			if(lev[to]<0) return fl;
			ZERO(itr);
			while((tf=dfs(from,to,numeric_limits<V>::max()))>0) fl+=tf;
		}
	}
};

class SCC {
public:
	static const int MV = 225000;
	vector<vector<int> > SC; int NV,GR[MV];
private:
	vector<int> E[MV], RE[MV], NUM; int vis[MV];
public:
	void init(int NV) { this->NV=NV; for(int i=0;i<NV;i++) { E[i].clear(); RE[i].clear();}}
	void add_edge(int x,int y) { E[x].push_back(y); RE[y].push_back(x); }
	void dfs(int cu) { vis[cu]=1; for(int i=0;i<E[cu].size();i++) if(!vis[E[cu][i]]) dfs(E[cu][i]); NUM.push_back(cu); }
	void revdfs(int cu, int ind) { int i; vis[cu]=1; GR[cu]=ind; SC[ind].push_back(cu);
		FOR(i,RE[cu].size()) if(!vis[RE[cu][i]]) revdfs(RE[cu][i],ind);}
	void scc() {
		int c=0,i; SC.clear(); SC.resize(NV); NUM.clear();
		assert(NV);
		FOR(i,NV) vis[i]=0; FOR(i,NV) if(!vis[i]) dfs(i); FOR(i,NV) vis[i]=0;
		for(int i=NUM.size()-1;i>=0;i--) if(!vis[NUM[i]]){
			SC[c].clear(); revdfs(NUM[i],c); sort(SC[c].begin(),SC[c].end()); c++;
		}
		SC.resize(c);
	}
};

vector<int> DMdecomposition(int L,int R,vector<vector<int>> E) {
	static MaxFlow_dinic<int> mf; //メモリ容量対策でstatic
	static SCC scc;
	//最大マッチングを求める
	int i;
	FOR(i,L) mf.add_edge(L+R,i,1);
	FOR(i,R) mf.add_edge(L+i,L+R+1,1);
	E.resize(L+R);
	FOR(i,L) FORR(e,E[i]) mf.add_edge(i,L+e,1);
	vector<int> G(L+R,-1); 

	mf.maxflow(L+R,L+R+1);
	vector<vector<int>> E2(L+R),RE2(L+R);
	FOR(i,L) {
		if(mf.E[L+R][i].cap==1) G[i]=0;
		FORR(e,mf.E[i]) if(e.to<L+R) {
			if(e.cap==0) { //最大マッチング
				E2[i].push_back(e.to);
				E2[e.to].push_back(i);
				RE2[i].push_back(e.to);
				RE2[e.to].push_back(i);
			}
			else {
				E2[i].push_back(e.to);
				RE2[e.to].push_back(i);
			}
		}
	}
	FOR(i,R) if(mf.E[L+i][0].cap==1) G[i+L]=1;
	
	queue<int> Q;
	// マッチに含まれない左点からの到達点
	FOR(i,L) if(G[i]==0) Q.push(i);
	while(Q.size()) {
		int x=Q.front();
		Q.pop();
		FORR(e,E2[x]) if(G[e]==-1) G[e]=0, Q.push(e);
	}
	// マッチに含まれない右点からの到達点
	FOR(i,R) if(G[L+i]==1) Q.push(L+i);
	while(Q.size()) {
		int x=Q.front();
		Q.pop();
		FORR(e,RE2[x]) if(G[e]==-1) G[e]=1, Q.push(e);
	}
	
	
	scc.init(L+R);
	FOR(i,L) if(G[i]==-1) {
		FORR(e,mf.E[i]) if(e.to<L+R && G[e.to]==-1) {
			if(e.cap==0) scc.add_edge(e.to,i);
			scc.add_edge(i,e.to);
		}
	}
	scc.scc();
	FOR(i,L+R) if(G[i]==-1) G[i]=scc.GR[i]+2;
	
	return G;
}


int A,B,L;
int S[201010],T[201010];


void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>A>>B>>L;
	vector<vector<int>> E(A);
	FOR(i,L) {
		cin>>S[i]>>T[i];
		S[i]--;
		T[i]--;
		E[S[i]].push_back(T[i]);
	}
	auto G=DMdecomposition(A,B,E);
	FOR(i,L) {
		if(G[S[i]]==G[A+T[i]]) {
			cout<<"Yes"<<endl;
		}
		else {
			cout<<"No"<<endl;
		}
	}
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0