Pagini recente » Istoria paginii utilizator/carinuta2502 | Cod sursa (job #89953) | Cod sursa (job #2743092) | Cod sursa (job #3285551) | Cod sursa (job #966421)
Cod sursa(job #966421)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
#define N 10005
int n, m, p, sol;
vector <int> a[N], s(N), dr(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++)
{
int y = a[x][i];
if(!dr[y] || leaga(dr[y]))
{
s[x] = y; dr[y] = x;
return 1;
}
}
return 0;
}
int main()
{
fin>>n>>m>>p;
while(p--)
{
int x, y;
fin>>x>>y;
a[x].push_back(y);
}
bool ok = 1;
while(ok)
{
ok = 0;
for(int i=1; i<=n; i++) viz[i] = 0;
for(int i=1; i<=n; i++)
if(!s[i])
if(leaga(i))
sol++, ok = 1;
}
fout<<sol<<'\n';
for(int i=1; i<=n; i++)
if(s[i]) fout<<i<<' '<<s[i]<<'\n';
return 0;
}