QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#92209 | #898. 二分图最大匹配 | lg_zhou# | TL | 0ms | 0kb | C++14 | 1.8kb | 2023-03-30 14:19:03 | 2023-03-30 14:19:19 |
Judging History
answer
#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
const int maxm = 1e6+5;
const int maxn = 3e5+5;
int edge[maxm], ver[maxm], nxt[maxm], head[maxn];
int now[maxn];
int cnt = 1;
void add(int x, int y, int z){
edge[++cnt] = z, ver[cnt] = y, nxt[cnt] = head[x], head[x] = cnt;
}
int d[maxn];
int l,r,m,s,t;
bool bfs(){
queue<int> q;
memset(d,0,sizeof d);
q.push(s);
d[s] = 1;
while(!q.empty()){
int f = q.front();
q.pop();
for (int i = head[f]; i; i = nxt[i]){
int to = ver[i];
if (edge[i] && !d[to]){
q.push(to);
d[to] = d[f]+1;
if (to == t) return 1;
}
}
}
return 0;
}
int dinic(int x, int flow){
if (x == t) return flow;
int rst = flow;
for (int i = head[x]; i && rst; i = nxt[i]){
now[x] = i;
int y = ver[i], z = edge[i];
if (edge[i] && d[y] == d[x]+1){
int to = dinic(y,min(rst,z));
if (!to) d[y] = 0;
rst -= to;
edge[i] -= to;
edge[i^1] += to;
}
}
return flow-rst;
}
signed main(){
// freopen("a.in","r",stdin);
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> l >> r >> m;
for (int i = 1; i <= m; i++){
int x,y;
cin >> x >> y;
x++,y++;
add(x,y+l,1);
add(y+l,x,0);
}
s = 0, t = l+r+1;
for (int i = 1; i <= l; i++) add(s,i,1), add(i,s,0);
for (int i = l+1; i <= l+r; i++) add(i,t,1), add(t,i,0);
int maxflow = 0;
while(bfs()) maxflow += dinic(s,1e9);
cout << maxflow << endl;
for (int i = 1; i <= l; i++){
for (int j = head[i]; j; j = nxt[j]){
if (ver[j] > l && !edge[j]){
cout << i-1 << " " << ver[j]-l-1 << endl;
}
}
}
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...