Pagini recente » Cod sursa (job #2828649) | Cod sursa (job #2445921) | Cod sursa (job #3288698) | Cod sursa (job #2355672) | Cod sursa (job #3161506)
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
typedef pair<int,int> pii;
#define fi first
#define se second
#define pb push_back
const int N = 2e4+3;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
int n, cup[N];
vector<int> e[N];
bitset<N> u;
void read(), cupmax(), prt();
bool cupleaza(int);
int main()
{
read();
cupmax();
prt();
return 0;
}
void read()
{
int E, m;
f >> n >> m >> E;
while (E--){
int a, b; f >> a >> b; b += n;
e[a].pb(b); e[b].pb(a);
}
}
void cupmax(){
bool c = 1;
do {
c = 0;
u.reset();
for (int i = 1; i <= n; i++){
if (!cup[i] && !u[i])
if (cupleaza(i))
c = 1;
}
} while (c);
}
void prt(){
vector<pii> sol;
for (int i = 1; i <= n; i++)
if (cup[i]) sol.pb({i, cup[i]});
g << sol.size() << '\n';
for (auto it: sol) g << it.fi << ' ' << it.se - n << '\n';
}
bool cupleaza(int nod){
if (u[nod]) return false;
u[nod] = 1;
for (auto it: e[nod])
if (!cup[it]){
cup[it] = nod;
cup[nod] = it;
return 1;
}
for (auto it: e[nod])
if (cupleaza(cup[it])){
cup[it] = nod;
cup[nod] = it;
return 1;
}
return 0;
}