Pagini recente » Cod sursa (job #1437904) | Cod sursa (job #530576) | Cod sursa (job #1092909) | oji-2005-ix | Cod sursa (job #2961988)
#include <bits/stdc++.h>
using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
#define cin f
#define cout g
const int Max = 20003;
int N, M, E;
int n;
unordered_map<int, int> capacity[Max];
vector<int> adj[Max];
int bfs(int s, int t, vector<int> &parent)
{
fill(parent.begin(), parent.end(), -1);
parent[s] = -2;
queue<pair<int, int>> Q;
Q.push({s, INT_MAX});
while(! Q.empty())
{
int cur = Q.front().first;
int flow = Q.front().second;
Q.pop();
for(int next : adj[cur])
{
if(parent[next] == -1 and capacity[cur][next] != 0)
{
parent[next] = cur;
int new_flow = min(flow, capacity[cur][next]);
if(next == t)
return new_flow;
Q.push({next, new_flow});
}
}
}
return 0;
}
int maxflow(int s, int t)
{
int flow = 0, new_flow;
vector<int> parent(n + 1);
while(new_flow = bfs(s, t, parent))
{
flow += new_flow;
int cur = t;
while(cur != s)
{
int prev = parent[cur];
capacity[cur][prev] += new_flow;
capacity[prev][cur] -= new_flow;
cur = prev;
}
}
return flow;
}
int main()
{
int s, t;
cin >> N >> M >> E;
n = N + M + 2;
s = n - 1, t = n;
for(int i = 1; i <= E; i++)
{
int x, y;
cin >> x >> y;
adj[x].push_back(y + N);
adj[y + N].push_back(x);
capacity[x][y + N] = 1;
capacity[y + N][x] = 0;
}
for(int i = 1; i <= N; i++)
{
adj[s].push_back(i);
adj[i].push_back(s);
capacity[s][i] = 1;
capacity[i][s] = 0;
}
for(int i = 1; i <= M; i ++)
{
adj[i + N].push_back(t);
adj[t].push_back(i + N);
capacity[i + N][t] = 1;
capacity[t][i + N] = 0;
}
cout<<maxflow(s, t)<<'\n';
for(int i = 1; i <= N; i ++)
for(auto it : adj[i])
if(it != s and capacity[i][it] == 0)
{
cout<<i<<' '<<it - N<<'\n';
break;
}
return 0;
}