Pagini recente » Cod sursa (job #2697995) | Cod sursa (job #1946521) | Cod sursa (job #3273831) | Cod sursa (job #2568190) | Cod sursa (job #3283115)
#include <bits/stdc++.h>
using namespace std;
#define INFILE "cuplaj.in"
#define OUTFILE "cuplaj.out"
const int N_MAX = 1e5;
int nodesLeft, nodesRight, edges;
int lft[N_MAX + 1], rgt[N_MAX + 1];
bool visited[N_MAX + 1];
vector<int> adj[N_MAX + 1];
void connect(int node1, int node2){
lft[node1] = node2;
rgt[node2] = node1;
}
bool link(int node){
if(visited[node]) return false;
visited[node] = true;
for(auto &to : adj[node]){
if(!rgt[to]) {
connect(node, to);
return true;
}
}
for(auto &to : adj[node]){
if(link(rgt[to])){
connect(node, to);
return true;
}
}
return false;
}
void solve(){
cin >> nodesLeft >> nodesRight >> edges;
for(int i = 0; i < edges; ++i){
int node1, node2; cin >> node1 >> node2;
adj[node1].push_back(node2);
}
bool ok = true;
while(ok) {
ok = false;
memset(visited, false, sizeof visited);
for(int i = 1; i <= nodesLeft; ++i){
if(!lft[i]) ok |= link(i);
}
}
int ans = 0;
for(int i = 1; i <= nodesLeft; ++i){
if(lft[i]) ++ans;
}
cout << ans << '\n';
for(int i = 1; i <= nodesLeft; ++i){
if(lft[i]) cout << i << " " << lft[i] << '\n';
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
freopen(INFILE, "r", stdin);
freopen(OUTFILE, "w", stdout);
solve();
return 0;
}