Pagini recente » Cod sursa (job #2347975) | Cod sursa (job #43004) | Cod sursa (job #1360034) | Cod sursa (job #3277575) | Cod sursa (job #2450760)
#include <iostream>
#include <vector>
#include <cstdio>
#include <queue>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int nax=(int)2e4+7;
int n,m,e;
vector <int> g[nax];
int match[nax];
int dist[nax];
void bfs()
{
queue <int> q;
for(int i=1;i<=n;i++)
if(match[i]==0)
{
q.push(i);
dist[i]=0;
}
else
dist[i]=-1;
while(!q.empty())
{
int nod=q.front();
for(auto nou : g[nod])
{
nou=match[nou];
if(nou && dist[nou]==-1)
{
dist[nou]=1+dist[nod];
q.push(nou);
}
}
q.pop();
}
/// ma duc prin libere si ma intorc prin ocupate
}
bool vis[nax];
bool dfs(int nod)
{
/// cout<<nod<<"\n";
vis[nod]=1;
for(auto &nou : g[nod])
{
if(nou==0)
{
cout<<"error : \n";
for(auto &x : g[1])
cout<<x<<" ";
cout<<"\n";
cout<<nod<<"\n";
exit(0);
}
if(match[nou]==0)
{
/// cout<<" -> "<<nod<<" "<<nou<<"\n";
match[nod]=nou;
match[nou]=nod;
return 1;
}
if(dist[nod]+1==dist[match[nou]])
if(dfs(match[nou]))
{
/// cout<<" -> "<<nod<<" "<<nou<<"\n";
match[nod]=nou;
match[nou]=nod;
return 1;
}
}
return 0;
}
int cuplaj()
{
int ans=0;
while(1)
{
bfs();
int delta=0;
for(int i=1;i<=n;i++)
vis[i]=0;
for(int i=1;i<=n;i++)
delta+=(match[i]==0 && dfs(i));
if(!delta)
break;
ans+=delta;
}
return ans;
}
int main()
{
freopen("cuplaj.in","r",stdin);
freopen("cuplaj.out","w",stdout);
cin>>n>>m>>e;
while(e--)
{
int x,y;
cin>>x>>y;
y+=n;
g[x].push_back(y);
g[y].push_back(x);
}
cout<<cuplaj()<<"\n";
for(int i=1;i<=n;i++)
if(match[i])
cout<<i<<" "<<match[i]<<"\n";
return 0;
}