Pagini recente » Cod sursa (job #1635445) | Cod sursa (job #2662064) | Cod sursa (job #1769469) | Cod sursa (job #12228) | Cod sursa (job #3164487)
#include <bits/stdc++.h>
#define dbg(x) cerr << (#x) << ": " << x << "\n\n";
using namespace std;
const int NMAX = 1e4 + 5, MOD = 1e9 + 7;
class InParser
{
vector<char> str;
int ptr;
ifstream fin;
char getChar ()
{
if (ptr == int(str.size()))
{
fin.read(str.data(), str.size());
ptr = 0;
}
return str[ptr++];
}
template<class T>
T getInt ()
{
char chr = getChar();
while (!isdigit(chr) && chr != '-')
chr = getChar();
int sgn = +1;
if (chr == '-')
{
sgn = -1;
chr = getChar();
}
T num = 0;
while (isdigit(chr))
{
num = num * 10 + chr - '0';
chr = getChar();
}
return sgn * num;
}
public:
InParser (const char* name) : str(1e5), ptr(str.size()), fin(name) {}
~InParser ()
{
fin.close();
}
template<class T>
friend InParser& operator>> (InParser& in, T& num)
{
num = in.getInt<T>();
return in;
}
};
class OutParser
{
vector<char> str;
int ptr;
ofstream fout;
void putChar (char chr)
{
if (ptr == int(str.size()))
{
fout.write(str.data(), str.size());
ptr = 0;
}
str[ptr++] = chr;
}
template<class T>
void putInt (T num)
{
if (num < 0)
{
putChar('-');
num *= -1;
}
if (num > 9)
putInt(num / 10);
putChar(num % 10 + '0');
}
public:
OutParser (const char* name) : str(1e5), ptr(0), fout(name) {}
~OutParser ()
{
fout.write(str.data(), ptr);
fout.close();
}
template<class T>
friend OutParser& operator<< (OutParser& out, const T num)
{
out.putInt(num);
return out;
}
friend OutParser& operator<< (OutParser& out, const char chr)
{
out.putChar(chr);
return out;
}
friend OutParser& operator<< (OutParser& out, const char* str)
{
for (int i = 0; str[i]; i++)
out.putChar(str[i]);
return out;
}
};
bool viz[NMAX], used[NMAX];
int n, m, e, mt[NMAX], lmao[NMAX], sz;
vector<int> v[NMAX];
bool kuhn (int x)
{
viz[x] = true;
for (auto u : v[x])
if (!viz[mt[u]] && (!mt[u] || kuhn(mt[u])))
{
mt[u] = x;
used[x] = true;
return true;
}
return false;
}
signed main ()
{
#ifndef MOOLAMP
InParser cin("cuplaj.in");
OutParser cout("cuplaj.out");
#endif
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m >> e;
while (e--)
{
int x, y; cin >> x >> y;
v[x].push_back(y);
}
for (int i = 1; i <= n; i++)
lmao[i] = i;
sort(lmao + 1, lmao + n + 1, [&] (int x, int y)
{
return v[x].size() < v[y].size();
});
for (int i = 1; i <= n; i++)
for (auto u : v[lmao[i]])
if (!mt[u])
{
sz++;
mt[u] = lmao[i];
used[lmao[i]] = true;
break;
}
bool ce_algoritm_trash;
do
{
ce_algoritm_trash = false;
memset(viz, false, sizeof(viz));
for (int i = 1; i <= n; i++)
if (!viz[i] && !used[i] && kuhn(i))
sz++, ce_algoritm_trash = true;
} while (ce_algoritm_trash);
cout << sz << '\n';
for (int i = 1; i <= m; i++)
if (mt[i])
cout << mt[i] << ' ' << i << '\n';
}