Pagini recente » Cod sursa (job #1108949) | Cod sursa (job #1610901) | Cod sursa (job #2042005) | Cod sursa (job #906484) | Cod sursa (job #3349796)
#include<iostream>
#include<fstream>
#include<vector>
#include<cstring>
using namespace std;
const int INF = 10001;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
vector<int> graph[INF];
int n,m,e;
int used[INF];
int match1[INF],match2[INF];
int match(int ind){
if(used[ind]){
return 0;
}
used[ind]=1;
for(auto val:graph[ind])
if(match2[val]==0){
match1[ind]=val;
match2[val]=ind;
return 1;
}
for(auto val:graph[ind]){
if(match(match2[val])){
match1[ind]=val;
match2[val]=ind;
return 1;
}
}
return 0;
}
void citire(){
fin>>n>>m>>e;
for(int i=0;i<e;++i){
int x,y;
fin>>x>>y;
graph[x].push_back(y);
}
}
void solve(){
int wasChanged=1;
while(wasChanged){
wasChanged=0;
memset(used,0,sizeof(used));
for(int i=1;i<=n;++i){
if(!match1[i]){
wasChanged+=match(i);
}
}
}
}
void afis(){
int matches=0;
for(int i=1;i<=n;i++){
if(match1[i]){
matches++;
}
}
fout<<matches<<'\n';
for(int i=1;i<=n;i++){
if(match1[i]){
fout<<i<<" "<<match1[i]<<'\n';
}
}
}
int main(){
citire();
solve();
afis();
}