Pagini recente » Cod sursa (job #2742422) | Cod sursa (job #309453) | Cod sursa (job #1475153) | Cod sursa (job #3329900) | Cod sursa (job #3329879)
// #include <iostream>
// #include <queue>
// #include <vector>
// #include <fstream>
// #include <algorithm>
// using namespace std;
// const int NMAX =1e3;
// int n, m;
//
// int capacitate[NMAX+1][NMAX+1], flux[NMAX+1][NMAX+1];
// int vis[NMAX+1], p[NMAX+1];
//
// vector<int> G[NMAX+1];
//
// int bfs(int s, int d) {
// for (int i=1; i<=n; 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])
// 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<(int)path.size() - 1; i++) {
// int u = path[i];
// int v = path[i+1];
// flow = min(flow, capacitate[u][v] - flux[u][v]);
// }
// for (int i=0; i<(int)path.size()-1; i++) {
// int u = path[i];
// int v = path[i+1];
// flux[u][v] += flow;
// flux[v][u] -= flow;
// }
// return flow;
// }
// int main() {
// ifstream cin("maxflow.in");
// ofstream cout("maxflow.out");
// cin >> n >> m ;
// for (int i=1; i<=m; i++) {
// int u,v, c;
// cin >> u >> v >> c;
// capacitate[u][v] = c;
// G[u].push_back(v);
// G[v].push_back(u);
// }
// long long maxFlow = 0;
// while (true) {
// int flow = bfs(1, n);
// if (flow == 0) {
// break;
// }
// maxFlow += flow;
// }
// cout<<maxFlow;
// }
#include <iostream>
#include <queue>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;
const int NMAX = 1e3;
int n, m;
int capacitate[NMAX + 1][NMAX + 1], flux[NMAX + 1][NMAX + 1];
int vis[NMAX + 1], p[NMAX + 1];
vector<int> G[NMAX + 1];
int bfs(int s, int d) {
for (int i = 1; i <= n; 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])
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 < (int) path.size() - 1; i++) {
int u = path[i];
int v = path[i + 1];
flow = min(flow, capacitate[u][v] - flux[u][v]);
}
for (int i = 0; i < (int) path.size() - 1; i++) {
int u = path[i];
int v = path[i + 1];
flux[u][v] += flow;
flux[v][u] -= flow;
}
return flow;
}
int main() {
ifstream cin("cuplaj.in");
ofstream cout("cuplaj.out");
int n_st, m_dr, e_muchii;
cin >> n_st >> m_dr >> e_muchii;
n = n_st + m_dr + 2;
int s = 1;
int d = n;
for (int i = 1; i <= n_st; i++) {
capacitate[s][i + 1] = 1;
G[s].push_back(i + 1);
G[i + 1].push_back(s);
}
for (int i = 1; i <= m_dr; i++) {
int nod = n_st + 1 + i;
capacitate[nod][d] = 1;
G[nod].push_back(d);
G[d].push_back(nod);
}
for (int i = 1; i <= e_muchii; i++) {
int u, v;
cin >> u >> v;
int nod_st = u + 1;
int nod_dr = n_st + 1 + v;
capacitate[nod_st][nod_dr] = 1;
G[nod_st].push_back(nod_dr);
G[nod_dr].push_back(nod_st);
}
long long maxFlow = 0;
while (true) {
int flow = bfs(1, n);
if (flow == 0) {
break;
}
maxFlow += flow;
}
cout << maxFlow << "\n";
for (int i = 1; i <= n_st; i++) {
for (int j = 1; j <= m_dr; j++) {
if (flux[i + 1][n_st + 1 + j] == 1) {
cout << i << " " << j << "\n";
}
}
}
return 0;
}