Pagini recente » Cod sursa (job #319607) | Cod sursa (job #2354197) | Cod sursa (job #2284716) | Cod sursa (job #2797439) | Cod sursa (job #1157219)
#include <cstdio>
#include <vector>
#define nmax 100005
using namespace std;
vector <int> g[nmax];
int c1[nmax];
int c2[nmax];
bool viz[nmax];
int n,m,e;
bool cuplaj(int x) {
if (viz[x]) return false;
viz[x] = true;
for (vector <int> :: iterator it = g[x].begin();it != g[x].end();it++) {
if (c2[*it] == 0) {
c2[*it] = x;
c2[c1[x]]= 0;
c1[x]= *it;
return true;
}
}
for (vector <int> :: iterator it = g[x].begin();it != g[x].end();it++) {
if (cuplaj(c2[*it])) {
c1[x] = *it;
c2[*it] = x;
return true;
}
}
return false;
}
int main() {
freopen("cuplaj.in","r",stdin);
freopen("cuplaj.out","w",stdout);
scanf("%d %d %d",&n,&m,&e);
for (int i=1;i<=e;i++) {
int a,b;
scanf("%d %d",&a,&b);
g[a].push_back(b);
}
bool cuplat = true;
while (cuplat) {
cuplat = false;
for (int j=1;j<=n;j++) viz[j] = false;
for (int i=1;i<=n;i++) {
if (c1[i] == 0) {
if (cuplaj(i)) cuplat = true;
}
}
}
for (int i=1;i<=n;i++) {
if (c1[i] != 0) printf("%d %d\n",i,c1[i]);
}
return 0;
}