Pagini recente » Cod sursa (job #389261) | Cod sursa (job #1336056) | Cod sursa (job #1210656) | Cod sursa (job #3342879) | Cod sursa (job #3328609)
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1e3;
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 muchii[NMAX+1];
int n, m,i, j, e, numar;
int bfs(int s, int d) {
for(int i = 1; i <= e+2; i++) {
vis[i] = 0;
p[i] = 0;
}
queue<int> q;
q.push(s);
vis[s] = 1;
while(!q.empty()) {
int nod = q.front();
q.pop();
for(auto vecin : G[nod]) {
if(!vis[vecin] && capacitate[nod][vecin] - flux[nod][vecin] > 0) {
vis[vecin] = 1;
p[vecin] = nod;
q.push(vecin);
}
}
}
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");
cin >> n >> m>>e;
for(int i = 1; i <= e; i++) {
int x, y;
int ok=0;
int ok2=0;
cin >> x >> y ;
capacitate[x+1][y+n+1] = 1;
G[x+1].push_back(y+n+1);
G[y+n+1].push_back(x+1);
for(auto vf: G[1])
if(vf==x+1)
ok=1;
for(auto vf: G[y+n+1])
if(vf==e+2)
ok2=1;
if(ok==0)
{
G[1].push_back(x+1);
capacitate[1][x+1]=1;
}
if(ok2==0)
{
G[y+n+1].push_back(e+2);
capacitate[y+n+1][e+2]=1;
}
}
int maxflow = 0;
while(true) {
int flow = bfs(1, e+2);
if(flow == 0) {
break;
}
maxflow += flow;
}
cout << maxflow<<endl;
for(i=2;i<=n+1;++i)
for(j=n+2;j<=n+m+1;++j)
if(flux[i][j]==1)
cout<<i-1<<' '<<j-(n+1)<<endl;
}