Pagini recente » Cod sursa (job #2615085) | Cod sursa (job #2847333)
#include <bits/stdc++.h>
using namespace std;
class InParser {
private:
FILE *fin;
char *buff;
int sp;
char read_ch() {
++sp;
if (sp == 4096) {
sp = 0;
fread(buff, 1, 4096, fin);
}
return buff[sp];
}
public:
InParser(const char* nume) {
fin = fopen(nume, "r");
buff = new char[4096]();
sp = 4095;
}
InParser& operator >> (int &n) {
char c;
while (!isdigit(c = read_ch()) && c != '-');
int sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
InParser& operator >> (long long &n) {
char c;
n = 0;
while (!isdigit(c = read_ch()) && c != '-');
long long sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
};
ofstream fout("cuplaj.out");
int t,i,j,n,m,e,x,y,l[10005],r[10005],cnt;
bool u[10005];
vector<int> g[10005];
bool pairup(int nod)
{
if(u[nod])return 0;
u[nod]=1;
for(auto it:g[nod])
if(!r[it])
{
l[nod]=it;
r[it]=nod;
return 1;
}
for(auto it:g[nod])
if(pairup(r[it]))
{
l[nod]=it;
r[it]=nod;
return 1;
}
return 0;
}
signed main()
{
InParser fin("cuplaj.in");
//fin>>t;
t=1;
while(t--)
{
fin>>n>>m>>e;
cnt=0;
for(i=1;i<=n;i++)
{
g[i].clear();
u[i]=0;
l[i]=0;
}
for(i=1;i<=m;i++)
r[i]=0;
for(i=1;i<=e;i++)
{
fin>>x>>y;
g[x].push_back(y);
}
bool ok=1;
while(ok)
{
ok=0;
for(i=1;i<=n;i++)
u[i]=0;
for(i=1;i<=n;i++)
if(!l[i])
ok|=pairup(i);
}
for(i=1;i<=n;i++)
if(l[i])
cnt++;
fout<<cnt<<'\n';
for(i=1;i<=n;i++)
if(l[i])
fout<<i<<' '<<l[i]<<'\n';
}
return 0;
}