Pagini recente » Cod sursa (job #2426663) | Cod sursa (job #829989) | Cod sursa (job #1352013) | Cod sursa (job #3224627)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
const int Nmax=1e4+5;
vector<pair<int,int>> v[2*Nmax];
pair<int,int> muchii[10*Nmax];
int f[10*Nmax];
int isin[2*Nmax];
unordered_set<int> isfree;
pair<int,int> prv[2*Nmax];
int n,m,e;
void reconst(int nod)
{
if(prv[nod].first==-1)
{
isfree.erase(nod);
isin[nod]=1;
return;
}
if(nod<=n)
{
f[prv[nod].second]=0;
}
else
{
f[prv[nod].second]=1;
}
reconst(prv[nod].first);
}
int main()
{
fin>>n>>m>>e;
for(int i=1;i<=e;i++)
{
int x,y;
fin>>x>>y;
v[x].push_back({y+n,i});
v[y+n].push_back({x,i});
muchii[i]={x,y};
}
for(int i=1;i<=n;i++)
{
isfree.insert(i);
}
int cnt=0;
while(cnt<=e && !isfree.empty())
{
for(int i=1;i<=n+m;i++) prv[i]={0,0};
queue<int> q;
for(int x:isfree)
{
q.push(x);
prv[x]={-1,-1};
}
int last=-1;
while(!q.empty())
{
int cur=q.front();
q.pop();
if(isin[cur]==0 && cur>n)
{
last=cur;
break;
}
if(cur<=n)
{
for(pair<int,int> nod:v[cur])
{
if(f[nod.second]==0 && prv[nod.first].first==0)
{
prv[nod.first]={cur,nod.second};
q.push(nod.first);
}
}
}
else
{
for(pair<int,int> nod:v[cur])
{
if(f[nod.second]==1 && prv[nod.first].first==0)
{
prv[nod.first]={cur,nod.second};
q.push(nod.first);
}
}
}
}
if(last==-1) break;
cnt++;
reconst(last);
isfree.erase(last);
isin[last]=1;
}
fout<<cnt<<'\n';
for(int i=1;i<=e;i++)
{
if(f[i]==1)
{
fout<<muchii[i].first<<' '<<muchii[i].second<<'\n';
}
}
return 0;
}