Pagini recente » Cod sursa (job #307291) | Cod sursa (job #1966370) | Cod sursa (job #2358017) | Cod sursa (job #513437) | Cod sursa (job #1970693)
#include <bits/stdc++.h>
using namespace std;
#define FILE_IO
typedef pair<int, int> pii;
class MaximumMatchings
{
public:
int N = 0;
vector <pii> edges;
vector <int> edg[20005];
int mtc[20005];
int t, tr[20005];
MaximumMatchings()
{
N = 0;
edges.clear();
t = 0;
memset(tr, 0, sizeof(tr));
memset(mtc, 0, sizeof(mtc));
}
void addEdge(int x, int y)
{
edges.push_back({x, y});
edg[x].push_back(y);
edg[y].push_back(x);
N = max(N, max(x, y));
}
void matchNodes(int x, int y) { mtc[x] = y; mtc[y] = x; }
bool match(int nod)
{
if(tr[nod] == t) return false;
tr[nod] = t;
for(auto nxt: edg[nod])
if( mtc[nxt] == 0 || match(mtc[nxt]) ) { matchNodes(nod, nxt); return true; }
return false;
}
vector<pii> getMaximumMatchings()
{
vector<pii> ans;
bool newMatching = true;
t = 0;
while( newMatching )
{
t++;
newMatching = false;
for(int i = 1; i <= N; i++)
if(!mtc[i])
if( match(i) )
newMatching = true;
}
for(int i = 1; i <= N; i++)
if(mtc[i] && mtc[i] > i)
ans.push_back({i, mtc[i]});
return ans;
}
};
MaximumMatchings m;
int main()
{
#ifdef FILE_IO
freopen("cuplaj.in", "r", stdin);
freopen("cuplaj.out", "w", stdout);
#endif
int N, M, K;
scanf("%d%d%d", &N, &M, &K);
for(int i = 1; i <= K; i++)
{
int x, y;
scanf("%d%d", &x, &y);
m.addEdge(x, y + N);
}
auto ans = m.getMaximumMatchings();
printf("%d\n", ans.size());
for(auto mtc: ans)
{
int x = mtc.first;
int y = mtc.second;
if(x > y) swap(x, y);
printf("%d %d\n", x, y - N);
}
return 0;
}