Pagini recente » Cod sursa (job #710924) | Cod sursa (job #3031006) | Cod sursa (job #2038072) | Cod sursa (job #3032147) | Cod sursa (job #971639)
Cod sursa(job #971639)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <cstring>
#define newn a[x][i]
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
const int N = 10010;
int t, n, m, e, sol, st[N], dr[N];
vector <int> a[N];
vector <bool> viz(N);
bool leaga(int x)
{
if(viz[x]) return 0;
viz[x] = 1;
for(unsigned i=0; i<a[x].size(); i++)
if(!dr[newn])
{
st[x] = newn; dr[newn] = x;
return 1;
}
for(unsigned i=0; i<a[x].size(); i++)
if(leaga(dr[newn]))
{
st[x] = newn; dr[newn] = x;
return 1;
}
return 0;
}
int main()
{
sol = 0;
fin>>n>>m>>e;
while(e--)
{
int x, y;
fin>>x>>y;
a[x].push_back(y);
}
bool ok = 1;
while(ok)
{
ok = 0;
for(int i=0; i<=n; i++) viz[i] = 0;
for(int i=1; i<=n; i++)
if(!st[i] && leaga(i)) ok = 1, sol++;
}
fout<<sol<<'\n';
for(int i=1; i<=n; i++)
if(st[i]) fout<<i<<' '<<st[i]<<'\n';
return 0;
}