Pagini recente » Cod sursa (job #2486649) | Cod sursa (job #1409503) | Cod sursa (job #3139939) | Cod sursa (job #2944890) | Cod sursa (job #2736638)
#include <bits/stdc++.h>
#define INF 1e9
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
int L, R, M;
vector <int> st[10005], dr[10005];
int lleft[10005], rright[10005];
bool vis[10005];
bool pairup(int node)
{
if(vis[node] == true)
return false;
vis[node] = true;
for(int i = 0; i < st[node].size(); i++)
if(rright[st[node][i]] == 0)
{
rright[st[node][i]] = node;
lleft[node] = st[node][i];
return true;
}
for(int i = 0; i < st[node].size(); i++)
if(pairup(rright[st[node][i]]) == true)
{
rright[st[node][i]] = node;
lleft[node] = st[node][i];
return true;
}
return false;
}
int main()
{
fin >> L >> R >> M;
for(int i = 1; i <= M; i++)
{
int x, y;
fin >> x >> y;
st[x].push_back(y);
dr[y].push_back(x);
}
bool ok = true;
int ct = 0;
while(ok)
{
memset(vis + 1, 0, L);
ok = false;
for(int i = 1; i <= L; i++)
if(lleft[i] == 0)
ok |= pairup(i);
}
for(int i = 1; i <= L; i++)
if(lleft[i] != 0)
ct++;
fout << ct << '\n';
for(int i = 1; i <= L; i++)
if(lleft[i] != 0)
fout << i << " " << lleft[i] << '\n';
return 0;
}