Pagini recente » Cod sursa (job #1375465) | Cod sursa (job #295449) | Cod sursa (job #3334255) | Cod sursa (job #3329903) | Cod sursa (job #3329894)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
#include <functional>
#include <utility>
#include <climits>
using namespace std;
const int NMAX = 1e3;
int capacitate[NMAX +1][NMAX + 1], 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+m+1; 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;
q.push(vecin);
p[vecin]=nod;
}
}
}
if(vis[d]==0){
return 0;
}
vector<int> path;
while(d!=0){
path.push_back(d);
d=p[d];
}
reverse(path.begin(), path.end());
int flow = 1e9;
for(int i=0; i < path.size() - 1; i++){
int x=path[i];
int y=path[i+1];
flow = min(flow, capacitate[x][y] - flux[x][y]);
}
for(int i=0; i < path.size() - 1; i++){
int x=path[i];
int y=path[i+1];
flux[x][y]+=flow;
flux[y][x]-=flow;
}
return flow;
}
int main(){
ifstream cin("cuplaj.in");
ofstream cout("cuplaj.out");
cin>>n>>m;
int l;
cin>>l;
int d=n+m+1;
for(int i=1; i<=n; i++){
capacitate[0][i]=1;
G[0].push_back(i);
G[i].push_back(0);
}
for(int i=n+1; i<=n+m; i++){
capacitate[i][d]=1;
G[i].push_back(d);
G[d].push_back(i);
}
for(int i=1; i<=l; i++){
int x,y;
cin>>x>>y;
y+=n;
capacitate[x][y] = 1;
if(find(G[x].begin(), G[x].end(), y) == G[x].end()){
G[x].push_back(y);
G[y].push_back(x);
}
}
int maxflow=0;
while(true){
int flow = bfs(0,d);
if(flow == 0){
break;
}
maxflow+=flow;
}
cout<<maxflow<<'\n';
for(int i=1; i<=n; i++){
for(int j=n+1; j<=n+m; j++){
if(flux[i][j] > 0){
cout<<i<<" "<<(j-n)<<'\n';
}
}
}
return 0;
}