Pagini recente » Cod sursa (job #2313660) | Cod sursa (job #35515) | Cod sursa (job #2853580) | Cod sursa (job #334038) | Cod sursa (job #2833084)
#include <bits/stdc++.h>
#define make_h(x , y) (1LL * x * N + y)
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
int const N = 2e4 + 3;
int n , m , e , a , b;
vector<int> v[N];
unordered_map<long long , int> f;
queue<int> q;
int lv[N] , t[N];
bool bfs(){
q.push(0);
lv[0] = 1;
while(!q.empty()){
auto x = q.front();
q.pop();
for(auto y : v[x]){
if(lv[y] || f[make_h(x , y)] <= 0)
continue;
lv[y] = 1 + lv[x];
q.push(y);
}
}
return lv[m + 1] > 0;
}
int dfs(int x , int r){
if(x == m + 1)
return r;
for(auto y : v[x]){
auto z = f[make_h(x , y)];
if(lv[x] + 1 != lv[y] || z <= 0)
continue;
int flow = dfs(y , min(r , z));
if(flow > 0){
f[make_h(x , y)] -= flow;
t[y] = x;
f[make_h(y , x)] += flow;
return flow;
}
lv[y] = 2e9;
}
return 0;
}
int maxflow(){
int r = 0 , flow;
while(true){
fill(lv , lv + 2 + m , 0);
if(!bfs()){
break;
}
do{
flow = dfs(0 , 2e9);
r += flow;
}while(flow);
}
return r;
}
int main()
{
fin >> n >> m >> e , m += n;
for(int i = 1 ; i <= e ; ++ i){
fin >> a >> b , b += n;
v[a].push_back(b);
v[b].push_back(a);
v[0].push_back(a);
v[a].push_back(0);
v[b].push_back(m + 1);
v[m + 1].push_back(b);
f[1LL * a * N + b] = 1;
f[1LL * b * N + a] = 0;
f[a] = 1;
f[1LL * a * N] = 0;
f[1LL * b * N + m + 1] = 1;
f[1LL * (m + 1) * N + b] = 0;
}
fout << maxflow() << '\n';
for(int i = n + 1 ; i <= m ; ++ i)
if(t[i])
fout << t[i] << ' ' << i - n << '\n';
return 0;
}