Pagini recente » Cod sursa (job #2214691) | Cod sursa (job #2600682) | Cod sursa (job #1507933) | Cod sursa (job #2833456) | Cod sursa (job #2674924)
#include <bits/stdc++.h>
using namespace std;
ifstream r("cuplaj.in");
ofstream w("cuplaj.out");
vector<int>g[10005];
bool viz[10005];
int st[10005], dr[10005];
bool cuplaj(int a){
if(viz[a]==1){
return false;
}
viz[a]=1;
for(auto it: g[a]){
if(dr[it]==0){
dr[it]=a;
st[a]=it;
return true;
}
}
for(auto it: g[a]){
if(cuplaj(dr[it])==1){
dr[it]=a;
st[a]=it;
return true;
}
}
return false;
}
int main()
{
int n, m, e;
r>>n>>m>>e;
for(int i=0;i<e;i++){
int x, y;
r>>x>>y;
g[x].push_back(y);
}
bool ok=false;
while(ok==false){
memset(viz, 0, sizeof(viz));
ok=true;
for(int i=1;i<=n;i++){
if(st[i]!=0){
continue;
}
if(cuplaj(i)==1){
ok =false;
}
}
}
int cnt=0;
for(int i=1;i<=n;i++){
if(st[i]){
cnt++;
}
}
w<<cnt<<"\n";
for(int i=1;i<=n;i++){
if(st[i]!=0){
w<<i<<" "<<st[i]<<"\n";
}
}
return 0;
}