結果

問題 No.1745 Selfish Spies 2 (à la Princess' Perfectionism)
ユーザー 👑 kmjpkmjp
提出日時 2021-11-16 02:17:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,497 bytes
コンパイル時間 3,406 ms
コンパイル使用メモリ 227,572 KB
実行使用メモリ 76,436 KB
最終ジャッジ日時 2023-08-21 21:45:26
合計ジャッジ時間 12,844 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
20,444 KB
testcase_01 AC 10 ms
20,296 KB
testcase_02 AC 10 ms
20,288 KB
testcase_03 AC 10 ms
20,280 KB
testcase_04 AC 9 ms
20,308 KB
testcase_05 AC 9 ms
20,364 KB
testcase_06 AC 10 ms
20,360 KB
testcase_07 AC 9 ms
20,304 KB
testcase_08 AC 10 ms
20,276 KB
testcase_09 AC 9 ms
20,528 KB
testcase_10 AC 9 ms
20,396 KB
testcase_11 AC 10 ms
20,320 KB
testcase_12 AC 10 ms
20,520 KB
testcase_13 AC 10 ms
20,384 KB
testcase_14 AC 12 ms
20,480 KB
testcase_15 AC 12 ms
20,516 KB
testcase_16 AC 13 ms
20,704 KB
testcase_17 AC 15 ms
20,844 KB
testcase_18 AC 15 ms
20,808 KB
testcase_19 AC 10 ms
20,320 KB
testcase_20 AC 10 ms
20,448 KB
testcase_21 AC 11 ms
20,516 KB
testcase_22 AC 10 ms
20,408 KB
testcase_23 AC 10 ms
20,468 KB
testcase_24 AC 44 ms
22,224 KB
testcase_25 AC 13 ms
20,596 KB
testcase_26 AC 13 ms
20,752 KB
testcase_27 AC 20 ms
20,912 KB
testcase_28 AC 170 ms
28,444 KB
testcase_29 AC 20 ms
21,152 KB
testcase_30 AC 20 ms
20,932 KB
testcase_31 AC 21 ms
20,952 KB
testcase_32 AC 20 ms
21,148 KB
testcase_33 AC 173 ms
28,424 KB
testcase_34 AC 167 ms
28,468 KB
testcase_35 AC 173 ms
27,860 KB
testcase_36 AC 179 ms
28,092 KB
testcase_37 AC 176 ms
27,848 KB
testcase_38 AC 170 ms
27,912 KB
testcase_39 AC 64 ms
23,708 KB
testcase_40 AC 109 ms
26,660 KB
testcase_41 AC 102 ms
25,516 KB
testcase_42 AC 222 ms
33,216 KB
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 AC 136 ms
25,620 KB
testcase_52 AC 123 ms
29,972 KB
testcase_53 AC 142 ms
30,948 KB
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
権限があれば一括ダウンロードができます

ソースコード

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+R+1][i].cap==0) 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[101010],T[101010];


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