結果

問題 No.1745 Selfish Spies 2 (à la Princess' Perfectionism)
ユーザー 👑 kmjpkmjp
提出日時 2021-11-16 01:57:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,059 bytes
コンパイル時間 3,174 ms
コンパイル使用メモリ 224,552 KB
実行使用メモリ 61,612 KB
最終ジャッジ日時 2023-08-21 21:20:49
合計ジャッジ時間 12,416 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
20,268 KB
testcase_01 AC 9 ms
20,284 KB
testcase_02 AC 9 ms
20,308 KB
testcase_03 AC 9 ms
20,300 KB
testcase_04 AC 9 ms
20,348 KB
testcase_05 AC 9 ms
20,280 KB
testcase_06 AC 9 ms
20,276 KB
testcase_07 AC 9 ms
20,280 KB
testcase_08 AC 9 ms
20,280 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 9 ms
20,468 KB
testcase_21 AC 11 ms
20,372 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 42 ms
21,644 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 174 ms
27,188 KB
testcase_29 AC 20 ms
21,064 KB
testcase_30 AC 20 ms
20,824 KB
testcase_31 AC 20 ms
20,812 KB
testcase_32 AC 20 ms
20,944 KB
testcase_33 AC 169 ms
26,776 KB
testcase_34 AC 167 ms
27,184 KB
testcase_35 AC 171 ms
26,524 KB
testcase_36 AC 176 ms
26,624 KB
testcase_37 AC 176 ms
26,676 KB
testcase_38 AC 167 ms
26,828 KB
testcase_39 WA -
testcase_40 AC 105 ms
25,260 KB
testcase_41 WA -
testcase_42 AC 209 ms
30,304 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 WA -
testcase_52 WA -
testcase_53 WA -
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);
	}
};

pair<vector<int>,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);
	FOR(i,L) FORR(e,E[i]) mf.add_edge(i,L+e,1);
	vector<int> match(L+R,-1); //最大マッチング先

	mf.maxflow(L+R,L+R+1);
	scc.init(L+R);
	FOR(i,L) {
		FORR(e,mf.E[i]) if(e.to<L+R&&e.cap==0) scc.add_edge(e.to,i), match[i]=e.to-L, match[e.to]=i;
	}
	FOR(i,L) FORR(e,E[i]) if(match[i]>=0&&match[e+L]>=0) scc.add_edge(i,L+e);
	scc.scc();

	vector<int> G(L+R);
	FOR(i,L+R) G[i]=scc.GR[i];
	
	return {match,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 a=DMdecomposition(A,B,E);
	vector<int> match=a.first;
	vector<int> G=a.second;
	
	FOR(i,L) {
		if(match[S[i]]>=0&&match[A+T[i]]>=0) {
			//現状最大マッチに含まれる点なら同一グループか判定
			if(G[S[i]]==G[A+T[i]]) {
				cout<<"Yes"<<endl;
			}
			else {
				cout<<"No"<<endl;
			}
			
		}
		else {
			//現状最大マッチに含まれないなら必ずok
			cout<<"Yes"<<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