Pagini recente » Cod sursa (job #1613050) | Cod sursa (job #3042089)
#include <bits/stdc++.h>
#define MMAX 10008
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
bool cuplat[MMAX], used[MMAX];
int match[MMAX];
vector <int> G[MMAX];
bool DFS(int node) {
if(used[node]) return false;
used[node] = true;
for(auto x : G[node])
if(match[x] == -1 || DFS(match[x])) {
match[x] = node;
cuplat[node] = true;
return true;
}
return false;
}
int main() {
ios_base::sync_with_stdio(false);
fin.tie(NULL); fout.tie(NULL);
int n, m, e; fin >> n >> m >> e;
for(int i = 1; i <= e; i ++) {
int u, v; fin >> u >> v;
G[u].push_back(v);
}
for(int i = 1; i <= m; i ++)
match[i] = -1;
bool found = true;
while(found) {
found = false;
for(int i = 1; i <= n; i ++)
used[i] = false;
for(int i = 1; i <= n; i ++)
if(cuplat[i] == 0) found = DFS(i);
}
int ans = 0;
for(int i = 1; i <= m; i ++) if(match[i] != -1) ans ++;
fout << ans << '\n';
for(int i = 1; i <= m; i ++)
if(match[i] != -1) fout << match[i] << ' ' << i << '\n';
return 0;
}