Pagini recente » Monitorul de evaluare | Cod sursa (job #2201598) | Cod sursa (job #734151) | Cod sursa (job #1424045) | Cod sursa (job #554494)
Cod sursa(job #554494)
// http://infoarena.ro/problema/cuplaj
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int maxSize = 10001;
ifstream in("cuplaj.in");
ofstream out("cuplaj.out");
int nrFirstSet,nrSecondSet,edges;
bool visit[maxSize];
int matchLeft[maxSize];
int matchRight[maxSize];
vector<int> graph[maxSize];
bool match(int node);
int main() {
in >> nrFirstSet >> nrSecondSet >> edges;
int from,to;
for(int i=1;i<=edges;i++) {
in >> from >> to;
graph[from].push_back(to);
//graph[to].push_back(from);
}
/*for(int i=1;i<=nrFirstSet;i++) {
graph[source].push_back(i);
graph[i].push_back(source);
}
for(int i=1;i<=nrSecondSet;i++) {
graph[i].push_back(sink);
graph[sink].push_back(i);
}*/
memset(matchLeft,-1,sizeof(matchLeft));
memset(matchRight,-1,sizeof(matchRight));
int maxFlow = 0;
for(int i=1;i<=nrFirstSet;i++) {
memset(visit,false,sizeof(visit));
if(match(i))
maxFlow++;
}
out << maxFlow << "\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;
if(matchRight[*it] < 0 || match(matchRight[*it])) {
matchLeft[node] = *it;
matchRight[*it] = node;
return true;
}
}
return false;
}