Pagini recente » Cod sursa (job #3156272) | Cod sursa (job #322363) | Cod sursa (job #1046457) | Cod sursa (job #2048194) | Cod sursa (job #2695509)
#include <bits/stdc++.h>
using namespace std;
constexpr int Nmax = 10010;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
vector<int> v[Nmax];
int n, m, k, dr[Nmax], st[Nmax];
bitset<Nmax> visited;
bool cuplaj(const int x) {
if(visited[x]) {
return false;
}
visited[x] = true;
for(const auto y : v[x]) {
if(!st[y]) {
dr[x] = y;
st[y] = x;
return true;
}
}
for(const auto y : v[x]) {
if(cuplaj(st[y])) {
dr[x] = y;
st[y] = x;
return true;
}
}
return false;
}
void mk_cuplaj() {
bool notDone = true;
while(notDone) {
visited = 0;
notDone = false;
for(int i = 1; i <= n; ++i)
if(!dr[i])
notDone = (cuplaj(i) || notDone);
}
}
int main() {
fin >> n >> m >> k;
for(int i = 0, x, y; i < k; ++i) {
fin >> x >> y;
v[x].push_back(y);
}
mk_cuplaj();
fout << Nmax - count(dr, dr + Nmax, 0) << endl;
for(int i = 1; i <= n; ++i) {
if(dr[i]) {
fout << i << ' ' << dr[i] << '\n';
}
}
}