Mai intai trebuie sa te autentifici.
Cod sursa(job #2425273)
| Utilizator | Data | 24 mai 2019 17:52:01 | |
|---|---|---|---|
| Problema | Cuplaj maxim in graf bipartit | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 1.21 kb |
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
vector<vector<int> > Graph(10001);
int N, M, E, i, j, ToLeft[10001], ToRight[10001], done, Sol;
bool Check[10001];
int Parc(int i);
int main()
{
freopen("cuplaj.in", "r", stdin);
freopen("cuplaj.out", "w", stdout);
scanf("%d%d%d", &N, &M, &E);
for(i=1; i<=N; ++i) Graph[i].push_back(0);
for(i=1; i<=E; ++i){
int x, y;
scanf("%d%d", &x, &y);
++Graph[x][0];
Graph[x].push_back(y);
}
do{
done=0;
for(i=1; i<=N; ++i) Check[i]=false;
for(i=1; i<=N; ++i) if(ToRight[i]==0) if(Parc(i)==1) {++Sol; done=1;}
} while(done!=0);
printf("%d\n", Sol);
for(i=1; i<=N; ++i) if(ToRight[i]!=0) printf("%d %d\n", i, ToRight[i]);
return 0;
}
int Parc(int i){
int j;
if(Check[i]==true) return 0;
Check[i]=true;
for(j=1; j<=Graph[i][0]; ++j) if(ToLeft[Graph[i][j]]==0){
ToRight[i]=Graph[i][j];
ToLeft[Graph[i][j]]=i;
return 1;
}
for(j=1; j<=Graph[i][0]; ++j) if(Parc(ToLeft[Graph[i][j]])==1){
ToRight[i]=Graph[i][j];
ToLeft[Graph[i][j]]=i;
return 1;
}
return 0;
}
