Pagini recente » Cod sursa (job #3276526) | Borderou de evaluare (job #1579551) | Cod sursa (job #107692) | Cod sursa (job #3242459) | Cod sursa (job #1970532)
#include<cstdio>
#include<vector>
using namespace std;
const int NMAX = 10000;
int n, m, e;
vector<int> adj[2][1 + NMAX];
int atr[2][1 + NMAX];
int vis[2][1 + NMAX];
int res[1 + NMAX];
int ct;
int connect(int nod)
{
if(vis[0][nod] == 1)
{
return 0;
}
vis[0][nod] = 1;
for(vector<int>::iterator it = adj[0][nod].begin(); it != adj[0][nod].end(); it++)
{
if(atr[1][*it] == 0)
{
atr[0][nod] = *it;
atr[1][*it] = nod;
return 1;
}
}
for(vector<int>::iterator it = adj[0][nod].begin(); it != adj[0][nod].end(); it++)
{
if(connect(atr[1][*it]) == 1)
{
atr[0][nod] = *it;
atr[1][*it] = nod;
return 1;
}
}
return 0;
}
void clear_vis()
{
for(int i = 1; i <= n; i++)
{
vis[0][i] = 0;
}
for(int i = 1; i <= m; i++)
{
vis[1][i] = 0;
}
}
int main()
{
freopen("cuplaj.in", "r", stdin);
freopen("cuplaj.out", "w", stdout);
scanf("%d %d %d", &n, &m, &e);
int x, y;
for(int i = 1; i <= e; i++)
{
scanf("%d %d", &x, &y);
adj[0][x].push_back(y);
adj[1][y].push_back(x);
}
bool flag = 1;
while(flag == 1)
{
flag = 0;
ct = 0;
for(int i = 1; i <= n; i++)
{
if(atr[0][i] == 0)
{
if(connect(i) == 1)
{
flag = 1;
}
}
else
{
ct++;
res[ct] = i;
}
}
clear_vis();
}
printf("%d\n", ct);
for(int i = 1; i <= ct; i++)
{
printf("%d %d\n", res[i], atr[0][res[i]]);
}
}