QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#92013#898. 二分图最大匹配DaBenZhongXiaSongKuaiDi#TL 0ms0kbC++141.4kb2023-03-30 08:56:172023-03-30 08:56:18

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-30 08:56:18]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2023-03-30 08:56:17]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)2e5+5;
int L,R,n,m,lev[maxn],h[maxn]; queue<int> q;
struct Node{int to,c,rev;}; vector<Node> v[maxn];
void addEdge(int fr,int to,int c){
	int f = v[fr].size(), g = v[to].size();
	v[fr].push_back((Node){to,c,g}),
	v[to].push_back((Node){fr,0,f});
}
bool bfs(){
	while(!q.empty()) q.pop();
	for(int al=0;al<=L+R+1;al++) lev[al] = -1, h[al] = 0;
	lev[0] = 0; q.push(0);
	while(!q.empty()){
		int u = q.front(); q.pop();
		for(auto X:v[u]){
			int to = X.to;
			if(X.c && lev[to]==-1)
				lev[to] = lev[u]+1, q.push(to);
		}
		if(~lev[L+R+1]) return 1;
	}
	return lev[L+R+1]!=-1;
}
int dfs(int u,int k){
	if(u==L+R+1 || !k) return k;
	int ret = 0, sz = v[u].size();
	for(int al=h[u];al<sz;al++,h[u]=al){
		int to = v[u][al].to;
		if(v[u][al].c && lev[to]==lev[u]+1){
			int t = dfs(to,min(k,v[u][al].c));
			ret += t, k -= t;
			v[u][al].c -= t, v[to][v[u][al].rev].c += t;
			if(!k) break;
		}
	}
	if(!ret) lev[u] = -1;
	return ret;
}
int main(){
	scanf("%d%d%d",&L,&R,&m);
	for(int al=1;al<=L;al++) addEdge(0,al,1);
	for(int al=1;al<=R;al++) addEdge(al+L,L+R+1,1);
	for(int al=1,be,ga;al<=m;al++)
		scanf("%d%d",&be,&ga), addEdge(be+1,ga+L+1,1);
	int ans = 0;
	while(bfs()) ans += dfs(0,L+R);//, printf("ft %d\n",ans);
	printf("%d\n",ans);
	for(int al=1;al<=L;al++)for(auto X:v[al])if(X.to>L && !X.c){printf("%d %d\n",al-1,X.to-L-1);break;}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

100000 100000 200000
78474 45795
32144 46392
92549 13903
73460 34144
96460 92850
56318 77066
77529 84436
76342 51542
77506 99268
76410 89381
1778 61392
43607 96135
84268 74827
14857 35966
32084 94908
19876 174
1481 94390
12423 55019
64368 92587
81295 7902
25432 46032
36293 61128
73555 84836
8418 102...

output:


result: