Pagini recente » Cod sursa (job #857777) | Cod sursa (job #1172224) | Cod sursa (job #221268) | Cod sursa (job #1795956) | Cod sursa (job #2821769)
#include<iostream>
#include<vector>
#include<fstream>
using namespace std;
int n, m, i, k, l, fr[20005],x,y, cuplat[20005],nr,cnt;
vector<vector<int>>mu;
void cupleaza(int a, int b) {
cuplat[cuplat[a]] = 0;
cuplat[cuplat[b]] = 0;
cuplat[a] = b;
cuplat[b] = a;
}
bool DFS(int nod) {
fr[nod] = nr;
for(auto j:mu[nod])
if (fr[j] < nr) {
fr[j] = nr;
if (cuplat[j]) {
if (DFS(cuplat[j])) {
cupleaza(nod, j);
return 1;
}
}
else {
cupleaza(nod, j);
return 1;
}
}
return 0;
}
int main() {
ifstream cin("cuplaj.in");
ofstream cout("cuplaj.out");
cin >> n >> m>>k;
mu.resize(n+m+2);
for (i = 1;i <=k;i++) {
cin >> x >> y;
mu[x].emplace_back(y + n);
mu[y + n].emplace_back(x);
}
bool ok=1;
while (ok)
{
nr++;
ok = 0;
for (i = 1;i <= n+m;i++)
if(cuplat[i]==0 and fr[i]<nr)
ok |=DFS(i);
}
for (i = 1;i <= n;i++)
if (cuplat[i] != 0) cnt++;
cout << cnt << '\n';
for (i = 1;i <= n;i++)
if (cuplat[i] != 0)
cout << i << ' ' << cuplat[i] - n << '\n';
}