Pagini recente » Cod sursa (job #1206521) | Cod sursa (job #2501066) | Cod sursa (job #1244683) | Cod sursa (job #60210) | Cod sursa (job #1619284)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
class Match {
private:
constexpr static int lim = 10007;
int N, M, E;
vector<int>muc[lim];
bitset<lim>used;
array<int, lim>boy, gurl;
public:
Match() {
fin >> N >> M >> E;
fill(begin(boy), end(boy), -1);
fill(begin(gurl), end(gurl), -1);
for(int i = 0; i < E; ++i) {
int x, y;
fin >> x >> y;
x--; y--;
muc[x].push_back(y);
}
}
// st baieti dr fete
bool cupleaza(int vertex, bitset<lim>&used) {
if(used[vertex]) return false;
used[vertex] = 1;
for(auto itr : muc[vertex]) {
if(boy[itr] == -1) {
boy[itr] = vertex;
gurl[vertex] = itr;
return 1;
}
}
for(auto itr : muc[vertex]) {
if(cupleaza(boy[itr], used)) {
boy[itr] = vertex;
gurl[vertex] = itr;
return 1;
}
}
return false;
}
void Run() {
bool have = 1;
while(have) {
have = false;
used.reset();
for(int i = 0; i < N; ++i) {
if(gurl[i] != -1) continue;
have |= cupleaza(i, used);
}
}
int cat = 0;
for(int i = 0; i < N; ++i)
cat += gurl[i] != -1;
fout << cat << '\n';
for(int i = 0; i < N; ++i)
if(gurl[i] != -1)
fout << i + 1 << ' ' << gurl[i] + 1<< '\n';
}
};
int main() {
Match G;
G.Run();
return 0;
}