Pagini recente » Cod sursa (job #907844) | Cod sursa (job #1719998) | Cod sursa (job #1885091) | Cod sursa (job #2471769) | Cod sursa (job #2201303)
#include <bits/stdc++.h>
using namespace std;
ifstream in("cuplaj.in");
ofstream out("cuplaj.out");
typedef long long I64;
struct Cuplaj
{
static const int NMAX = 10000;
int cuplate;
vector<int> G[NMAX+2];
bool viz[NMAX+2];
int st[NMAX+2], dr[NMAX+2];
int N, M, E;
Cuplaj(int n, int m)
{
cuplate = 0;
N = n, M = m;
memset(st, 0, sizeof(st));
memset(dr, 0, sizeof(dr));
}
addEdge(int x, int y)
{
G[x].push_back(y);
}
bool cuplaj(int nod)
{
viz[nod] = 1;
for( auto x : G[nod] ) {
if( !dr[x] ) {
dr[x] = nod;
st[nod] = x;
++cuplate;
return 1;
}
}
for( auto x : G[ nod ] ) {
if( !viz[ dr[x] ] && cuplaj(dr[x]) ) {
st[nod] = x;
dr[x] = nod;
return 1;
}
}
return 0;
}
vector<pair<int,int>> make_cuplaj()
{
vector<pair<int,int>> sol;
bool ok = 0;
while( !ok ) {
ok = 1;
memset(viz, 0, sizeof(viz));
for( int i = 1; i <= N; ++i ) {
if( !st[i] && cuplaj(i) )
ok = 0;
}
}
for( int i = 1; i <= N; ++i ) if( st[i] )
sol.push_back({i, st[i]});
return sol;
}
};
int N, M, E;
int main()
{
in >> N >> M >> E;
Cuplaj problema(N, M);
for( int i = 1; i <= E; ++i ) {
int x,y;
in >> x >> y;
problema.addEdge(x, y);
}
auto sol = problema.make_cuplaj();
out << sol.size() << '\n';
for( auto p : sol ) out << p.first << ' ' << p.second << '\n';
return 0;
}