Pagini recente » Cod sursa (job #2801466) | Cod sursa (job #1981891) | Monitorul de evaluare | Cod sursa (job #1598549) | Cod sursa (job #1962287)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("felinare.in");
ofstream fout("felinare.out");
const int Nmax=8200;
int st[Nmax],dr[Nmax],L[Nmax],R[Nmax];
vector<int>G[Nmax];
bitset<Nmax>viz;
bool pairup(int n)
{
if(viz[n]) return 0;
viz[n]=1;
for(vector<int>::iterator it=G[n].begin();it!=G[n].end();it++)
{
if(R[*it]==0)
{
R[*it]=n;
L[n]=*it;
return 1;
}
if(pairup(R[*it]))
{
R[*it]=n;
L[n]=*it;
return 1;
}
}
return 0;
}
int main()
{
int n,m,x,y,i,A=0,B=0,cuplaj=0;
fin>>n>>m;
for(i=1;i<=m;i++)
{
A++,B++;
fin>>x>>y;
G[x].push_back(y);
}
bool change=1;
while(change)
{
change=0;
viz.reset();
for(i=1;i<=n;i++)
{
if(L[i]==0) change|=pairup(i);
}
}
for(i=1;i<=n;i++)
{
if(L[i]>0) cuplaj++;
}
fout<<2*n-cuplaj;
}