Pagini recente » Cod sursa (job #3259480) | Cod sursa (job #3038503) | Cod sursa (job #1059820) | Cod sursa (job #2710265) | Cod sursa (job #544775)
Cod sursa(job #544775)
#include<cstdio>
#include<vector>
#include<bitset>
#define Nmax 10050
using namespace std;
vector<int> G[Nmax];
bitset<Nmax> viz;
int N, M, L[Nmax], R[Nmax], sol;
inline bool cupleaza(int nod) {
if(viz[nod])
return 0;
viz[nod]=1;
for(vector<int>:: iterator it=G[nod].begin(), it2=G[nod].end(); it!=it2; ++it)
if(!R[*it] || cupleaza(R[*it])) {
R[*it]=nod;
L[nod]=*it;
return 1;
}
return 0;
}
int main() {
freopen("cuplaj.in","r",stdin);
freopen("cuplaj.out","w",stdout);
int edges, i, x, y;
scanf("%d %d %d\n",&N,&M,&edges);
while(edges--) {
scanf("%d %d\n",&x,&y);
G[x].push_back(y);
}
bool ok=1;
while(ok) {
ok=0;
viz.reset();
for(i=1; i<=N; i++)
if(!L[i])
if(cupleaza(i))
++sol, ok = 1;
}
printf("%d\n",sol);
for(i=1; i<=N; i++)
if(L[i])
printf("%d %d\n",i,L[i]);
return 0;
}