Pagini recente » Cod sursa (job #30711) | Cod sursa (job #63240) | Cod sursa (job #561707) | Cod sursa (job #2864143) | Cod sursa (job #533647)
Cod sursa(job #533647)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
const char iname[] = "felinare.in";
const char oname[] = "felinare.out";
ifstream fin(iname);
ofstream fout(oname);
int n, m, i, x, y, cuplat, c, e, viz[10900], R[10900], L[10900], SL[10900], SR[10900];
vector<int> Gr[10900];
int pair_up(int nod)
{
if(viz[nod] == 1)
return 0;
viz[nod] = 1;
for(vector<int>::iterator it = Gr[nod].begin(); it != Gr[nod].end(); ++it)
if(pair_up(R[*it]))
{
L[nod] = *it;
R[*it] = nod;
return 1;
}
for(vector<int>::iterator it = Gr[nod].begin(); it != Gr[nod].end(); ++it)
if(R[*it] == 0)
{
L[nod] = *it;
R[*it] = nod;
return 1;
}
return 0;
}
void suport(int nod)
{
for(vector<int>::iterator it = Gr[nod].begin(); it != Gr[nod].end(); ++it)
{
if(SL[R[*it]])
{
SL[R[*it]] = 0;
SR[*it] = 1;
suport(R[*it]);
}
}
}
int main()
{
fin >> n >> m;
for(i = 1; i <= m; i ++)
{
fin >> x >> y;
Gr[x].push_back(y);
}
cuplat = 1;
while(cuplat)
{
for(i = 1; i <= n; i ++)
viz[i] = 0;
cuplat = 0;
for(i = 1; i <= n; i ++)
if(L[i] == 0)
if(pair_up(i))
++c, cuplat = 1;
}
fout << 2 * n - c << "\n";
for(i = 1; i <= n; i ++)
if(L[i])
SL[i] = 1;
for(i = 1; i <= n; i ++)
if(!SL[i])
suport(i);
for(i = 1; i <= n; i ++)
{
if(SL[i] && SR[i])
fout << "0\n";
if(!SL[i] && SR[i])
fout << "1\n";
if(!SR[i] && SL[i])
fout << "2\n";
if(!SL[i] && !SR[i])
fout << "3\n";
}
return 0;
}