Pagini recente » Cod sursa (job #1158956) | Cod sursa (job #1613099) | Cod sursa (job #784097) | Cod sursa (job #1360734) | Cod sursa (job #1236099)
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <deque>
using namespace std;
const char infile[] = "cuplaj.in";
const char outfile[] = "cuplaj.out";
ifstream fin(infile);
ofstream fout(outfile);
const int MAXN = 100005;
const int oo = 0x3f3f3f3f;
typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;
const inline int min(const int &a, const int &b) { if( a > b ) return b; return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b; return a; }
const inline void Get_min(int &a, const int b) { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b) { if( a < b ) a = b; }
class BipartiteGraph {
public:
BipartiteGraph() {
}
BipartiteGraph(int _N) {
adj.resize(_N + 1);
N = _N;
match.resize(_N + 1);
_pair.resize(_N + 1);
used.resize(_N + 1);
}
void addEdge(int x, int y) {
adj[x].push_back(y);
}
bool pairUp(int node) {
if(used[node])
return false;
used[node] = true;
for(It it = adj[node].begin(), fin = adj[node].end(); it != fin ; ++ it)
if(!_pair[*it] || pairUp(_pair[*it])) {
_pair[*it] = node;
match[node] = *it;
return true;
}
return false;
}
void getMaxMatching() {
int sol = 0;
for(bool change = true ; change ; ) {
change = false;
fill(used.begin(), used.end(), 0);
for(int i = 1 ; i <= N ; ++ i)
if(!match[i] && pairUp(i))
change = true,
++ sol;
}
fout << sol << '\n';
for(int i = 1 ; i <= N ; ++ i)
if(match[i])
fout << i << ' ' << match[i] << '\n';
}
private:
vector <vector <int> > adj;
vector <bool> used;
vector <int> _pair, match;
int N;
};
int N, M, cnt;
int main() {
fin >> N >> M >> cnt;
BipartiteGraph G(N);
for(int i = 1 ; i <= cnt ; ++ i) {
int x, y;
fin >> x >> y;
G.addEdge(x, y);
}
G.getMaxMatching();
fin.close();
fout.close();
return 0;
}