Pagini recente » Cod sursa (job #2956406) | Cod sursa (job #2283731) | Cod sursa (job #2372306) | Cod sursa (job #288934) | Cod sursa (job #2848090)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
const int MAXN = 10001;
vector<int> G[MAXN];
int st[MAXN], dr[MAXN];
bool viz[MAXN];
bool Cupleaza(int node) {
if(viz[node] != 0) return 0;
viz[node] = 1;
for(auto nxt : G[node]) {
if(!st[nxt] || Cupleaza(st[nxt])) {
st[nxt] = node;
dr[node] = nxt;
return 1; // am putut cupla
}
}
return 0;
}
int main() {
int n, m, e, x, y;
fin >> n >> m >> e;
while(e--) {
fin >> x >> y;
G[x].push_back(y);
}
int found = 1;
while(found) {
memset(viz, 0, sizeof(viz));
found = 0;
for(int i = 1; i <= n && !found; i++)
if(!dr[i] && Cupleaza(i))
found = 1;
}
int nr = 0;
for(int i = 1; i <= n; i++)
nr += (dr[i] != 0);
fout << nr << '\n';
for(int i = 1; i <= n; i++)
if(dr[i] != 0)
fout << i << ' ' << dr[i] << '\n';
return 0;
}