Pagini recente » Cod sursa (job #2392654) | Cod sursa (job #2654502) | Cod sursa (job #320780) | Cod sursa (job #2586573) | Cod sursa (job #877980)
Cod sursa(job #877980)
// Include
#include <fstream>
#include <vector>
using namespace std;
// Definitii
#define pb push_back
// Constante
const int sz = (int)1e5+8;
// Functii
int link(int node);
// Variabile
ifstream in("cuplaj.in");
ofstream out("cuplaj.out");
int leftNodes, rightNodes, edges;
int Left[sz], Right[sz];
bool Fixed[sz];
int answer;
vector<int> graph[sz];
// Main
int main()
{
in >> leftNodes >> rightNodes >> edges;
int rFrom, rTo;
while(edges--)
{
in >> rFrom >> rTo;
graph[rFrom].pb(rTo);
}
for(int i=1 ; i<=leftNodes ; ++i)
{
if(!link(i))
{
memset(Fixed, false, (leftNodes+1)*sizeof(bool));
answer += link(i);
}
else
++answer;
}
out << answer << '\n';
for(int i=1 ; i<=leftNodes ; ++i)
if(Right[i])
out << i << ' ' << Right[i] << '\n';
in.close();
out.close();
return 0;
}
int link(int node)
{
if(Fixed[node])
return 0;
Fixed[node] = true;
vector<int>::iterator it, end=graph[node].end();
for(it=graph[node].begin() ; it!=end ; ++it)
{
if(!Left[*it])
{
Left[*it] = node;
Right[node] = *it;
return 1;
}
}
for(it=graph[node].begin() ; it!=end ; ++it)
{
if(link(Left[*it]))
{
Left[*it] = node;
Right[node] = *it;
return 1;
}
}
return 0;
}