Pagini recente » Cod sursa (job #1063518) | Cod sursa (job #1477709) | Cod sursa (job #544994) | Istoria paginii runda/oni_2012_ziua1/clasament | Cod sursa (job #1494870)
#include <cstdio>
#include <vector>
#include <bitset>
#define DIM (1<<17)
using namespace std;
int N, M, K, X, Y, sol;
int Right[DIM/10], Left[DIM/10];
vector<int> Vector[DIM];
bitset<DIM/10> Marked;
bool VertexCover(int nod){
Marked[nod] = 1;
for(int i = 0; i < Vector[nod].size(); i ++){
int vec = Vector[nod][i];
if(Right[vec] == 0){
Left [nod] = vec;
Right[vec] = nod;
sol ++;
return 1;
}
}
for(int i = 0; i < Vector[nod].size(); i ++){
int vec = Vector[nod][i];
if(!Marked[Right[vec]] && VertexCover(Right[vec])){
Left [nod] = vec;
Right[vec] = nod;
return 1;
}
}
return 0;
}
int main(){
freopen("cuplaj.in" ,"r", stdin );
freopen("cuplaj.out","w", stdout);
scanf("%d %d %d", &N, &M, &K);
for(int i = 1; i <= K; i ++){
scanf("%d %d", &X, &Y);
Vector[X].push_back(Y);
}
int ok = 1;
do {
ok = 0;
Marked.reset();
for(int i = 1; i <= N; i ++)
if(Left[i] == 0)
ok = ok || VertexCover(i);
} while( ok == 1 );
printf("%d\n", sol);
for(int i = 1; i <= N; i ++)
if(Left[i] != 0)
printf("%d %d\n", i, Left[i]);
fclose(stdin );
fclose(stdout);
return 0;
}