Pagini recente » Cod sursa (job #1951731) | Cod sursa (job #158531) | Cod sursa (job #174071) | Cod sursa (job #1855216) | Cod sursa (job #554530)
Cod sursa(job #554530)
// http://infoarena.ro/problema/cuplaj
#include <fstream>
#include <bitset>
#include <vector>
using namespace std;
const int maxSize = 10001;
ifstream in("cuplaj.in");
ofstream out("cuplaj.out");
int matchLeft[maxSize];
int matchRight[maxSize];
bitset<maxSize> visit;
vector<int> graph[maxSize];
bool match(int node);
int main() {
int leftSize,rightSize,edges;
in >> leftSize >> rightSize >> edges;
int from,to;
for(int i=1;i<=edges;i++) {
in >> from >> to;
graph[from].push_back(to);
}
int maxFlow = 0;
for(int i=1;i<=leftSize;i++) {
visit.reset();
if(match(i))
maxFlow++;
}
out << maxFlow << "\n";
for(int i=1;i<=leftSize;i++)
if(matchLeft[i])
out << i << " " << matchLeft[i] << "\n";
return (0);
}
bool match(int node) {
vector<int>::iterator it,end;
end = graph[node].end();
for(it=graph[node].begin();it!=end;++it)
if(!visit[*it]) {
visit[*it] = true;
// daca nodul it nu este cuplat sau am reusit
// sa-l cuplam (se incearca cuplarea si
// in cazul in care nodul este cuplat)
if(!matchRight[*it] || match(matchRight[*it])) {
matchLeft[node] = *it;
matchRight[*it] = node;
return true;
}
}
return false;
}