Pagini recente » Cod sursa (job #1825484) | Cod sursa (job #106315) | Cod sursa (job #2076719) | Istoria paginii runda/shimulare_shmecheri/clasament | Cod sursa (job #2188117)
#include <bits/stdc++.h>
#define Nmax 10001
using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
int vL[Nmax],vR[Nmax];
bool viz[Nmax];
list <int> G[Nmax];
bool match(int x)
{
if(viz[x]) return false;
viz[x]=true;
for(const auto &it:G[x])
if(!vL[it])
{
vL[it]=x;
vR[x]=it;
return true;
}
for(const auto &it:G[x])
if(match(vL[it]))
{
vL[it]=x;
vR[x]=it;
return true;
}
return false;
}
int main()
{
int n,m,e,i,j;
f>>n>>m>>e;
while(e--)
{
f>>i>>j;
G[i].push_back(j);
}
bool ok=true;
int ans=0;
while(ok)
{
ok=false;
memset(viz,false,sizeof(viz));
for(i=1;i<=n;i++)
if(!vR[i] and match(i))
{
ok=true;
++ans;
}
}
g<<ans<<'\n';
for(i=1;i<=n;i++)
if(vR[i]) g<<i<<' '<<vR[i]<<'\n';
return 0;
}