Pagini recente » Cod sursa (job #358528) | Cod sursa (job #615435) | Monitorul de evaluare | Cod sursa (job #2669734) | Cod sursa (job #2461465)
#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
const string file = "cuplaj";
const ll INF = 9223372036854775807ll;
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, inf = 2147483647, nmax = 10005;
int n, m, e, pairu[nmax], pairv[nmax], dist[nmax];
vector<int> v[nmax];
bool bfs()
{
queue<int> q;
for (int i = 1; i <= n; ++i)
if(pairu[i] == 0){
q.push(i);
dist[i] = 0;
}else dist[i] = inf;
dist[0] = inf;
while(!q.empty()){
int nod = q.front();
q.pop();
if(dist[0] != inf)
return 1;
for (auto u : v[nod])
if(dist[pairv[u]] == inf){
dist[pairv[u]] = dist[nod]+1;
q.push(pairv[u]);
}
}
return 0;
}
bool dfs(int x)
{
if(x == 0)
return 1;
for (auto y : v[x])
if(dist[pairv[y]] == dist[x]+1 && dfs(pairv[y])){
pairu[x] = y;
pairv[y] = x;
return 1;
}
return 0;
}
int main()
{
ifstream fin (file+".in");
ofstream fout (file+".out");
fin >> n >> m >> e;
for (int i = 1; i <= e; ++i){
int x, y;
fin >> x >> y;
v[x].push_back(y);
}
int cuplaj = 0;
while(bfs()){
for (int i = 1; i <= n; ++i)
if(pairu[i] == 0 && dfs(i))
++cuplaj;
}
fout << cuplaj << "\n";
for (int i = 1; i <= n; ++i)
if(pairu[i] != 0)
fout << i << " " << pairu[i] << "\n";
return 0;
}