Pagini recente » Cod sursa (job #794940) | Cod sursa (job #1571204) | Cod sursa (job #3317091) | Cod sursa (job #2022704) | Cod sursa (job #3328605)
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1e4;
int capacitate[NMAX + 1][NMAX + 1];
int flux[NMAX + 1][NMAX + 1];
int vis[NMAX + 1], p[NMAX + 1];
vector<int> G[NMAX + 1];
int n, m;
int bfs(int s, int d) {
for(int i = 0; i <= n; i ++) {
vis[i] = 0;
}
queue <int> q;
q.push(s);
vis[s] = 1;
while(!q.empty()) {
int nod = q.front();
q.pop();
for(auto& v : G[nod]) {
if(!vis[v] && capacitate[nod][v] - flux[nod][v] > 0) {
vis[v] = 1;
p[v] = nod;
q.push(v);
}
}
}
if(!vis[d]) {
return 0;
}
vector<int> path;
int x = d;
while(x != 0) {
path.push_back(x);
x = p[x];
}
reverse(path.begin(), path.end());
int flow = 1e9;
for(int i = 0; i < path.size() - 1; i ++) {
int a = path[i];
int b = path[i + 1];
flow = min(flow, capacitate[a][b] - flux[a][b]);
}
for(int i = 0; i < path.size() - 1; i ++) {
int a = path[i];
int b = path[i + 1];
flux[a][b] += flow;
flux[b][a] -= flow;
}
return flow;
}
int main() {
ifstream cin("cuplaj.in");
ofstream cout("cuplaj.out");
int l, r, e;
cin >> l >> r >> e;
n = l + r + 2;
for(int i = 1; i <= e; i ++) {
int x, y;
cin >> x >> y;
y += l;
G[x].push_back(y);
G[y].push_back(x);
capacitate[x][y] = 1;
}
int flux_maxim = 0;
int s = 0;
int d = l + r + 1;
for(int i = 1; i <= l; i ++) {
G[s].push_back(i);
G[i].push_back(s);
capacitate[s][i] = 1;
}
for(int i = l + 1; i < d; i ++) {
G[d].push_back(i);
G[i].push_back(d);
capacitate[i][d] = 1;
}
int flow;
while((flow = bfs(s, d)) > 0) {
flux_maxim += flow;
}
cout << flux_maxim << "\n";
for(int i = 1; i <= l; i ++) {
for(int j = l + 1; j <= l + r; j ++) {
if(flux[i][j] == 1) {
cout << i << ' ' << j - l << '\n';
}
}
}
return 0;
}