Pagini recente » Cod sursa (job #1529370) | Cod sursa (job #1939523) | Cod sursa (job #914710) | Cod sursa (job #1500593) | Cod sursa (job #2679691)
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pf push_front
#define nmax 100005
#define inf 1000000000
using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
int N,M,E;
vector<int> adj[10005];
int l[10005],r[10005],cnt;
int viz[10005];
bool cuplaj(const int node)
{
if(viz[node]==cnt)
{
return false;
}
viz[node]=cnt;
//cout<<node<<" "<<cnt<<'\n';
for(auto x:adj[node])
{
if(l[x]==0)
{
r[node]=x;
l[x]=node;
return true;
}
}
for(auto x:adj[node])
{
if(cuplaj(l[x]))
{
r[node]=x;
l[x]=node;
return true;
}
}
return false;
}
int main()
{
f>>N>>M>>E;
while(E--)
{
int x,y;
f>>x>>y;
adj[x].pb(y);
}
bool gasit=1;
while(gasit==1)
{
gasit=0;
cnt++;
for(int i=1;i<=N;i++)
{
if(r[i]==0)
{
gasit=gasit|cuplaj(i);
}
}
// cout<<1;
}
int rez=0;
for(int i=1;i<=N;i++)
{
if(r[i])
{
rez++;
}
}
g<<rez<<'\n';
for(int i=1;i<=N;i++)
{
if(r[i])
{
g<<i<<" "<<r[i]<<'\n';
}
}
}