QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#92043 | #898. 二分图最大匹配 | lg_zhou# | TL | 0ms | 0kb | C++14 | 1.6kb | 2023-03-30 09:36:49 | 2023-03-30 09:36:51 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = (int)4e5+5;
int n,m,s,t,L,R,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<=n;al++) lev[al] = -1, h[al] = 0;
lev[s] = 0; q.push(s);
while(!q.empty()){
int u = q.front(); q.pop();
for(auto X:v[u])if(X.c && lev[X.to]==-1)
lev[X.to] = lev[u]+1, q.push(X.to);
if(~lev[t]) return 1;
}
return lev[t]!=-1;
}
int dfs(int u,int k){
if(u==t || !k) return k;
int ret = 0, sz = v[u].size();
for(int al=h[u];al<sz;al++,h[u]=al)if(v[u][al].c){
int to = v[u][al].to;
if(lev[to]==lev[u]+1){
int cur = dfs(to,min(k,v[u][al].c));
v[u][al].c -= cur, v[to][v[u][al].rev].c += cur;
k -= cur, ret += cur;
if(!k) break;
}
}
if(!ret) lev[u] = -1;
return ret;
}
int main(){
// freopen("a.in","r",stdin);
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> L >> R >> m;
s = 0, t = n = L+R+1;
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++){
cin >> be >> ga;
addEdge(be+1,ga+L+1,1);
}
ll ans = 0;
while(bfs()) ans += dfs(s,INT_MAX);
cout << ans << endl;
for(int al=1;al<=L;al++)
for(auto X:v[al])
if(X.to>L && !X.c){
cout << al-1 << " " << X.to-L-1 << endl;
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...