Pagini recente » Cod sursa (job #3289491) | Cod sursa (job #1126277) | Cod sursa (job #1878922) | Cod sursa (job #385365) | Cod sursa (job #1779465)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("felinare.in");
ofstream fout("felinare.out");
int supportLeft[10000], supportRight[10000], stanga[10000], dreapta[10000];
int N, M, a, b, cardCuplaj;
bool ok = true;
bitset<10000>v;
vector<int>L[10000];
inline bool cuplaj(const int &node)
{
v[node] = 1;
for(int i = 0; i < L[node].size(); i ++)
{
if(dreapta[ L[node][i] ] == 0)
{
dreapta[ L[node][i] ] = node;
stanga[node] = L[node][i];
supportLeft[node] = 1;
return true;
}
}
for(int i = 0; i < L[node].size(); i ++)
{
if(v[ dreapta[ L[node][i] ] ] == 0 && cuplaj( dreapta[ L[node][i] ]) == true )
{
dreapta[ L[node][i] ] = node;
stanga[node] = L[node][i];
supportLeft[node] = 1;
return true;
}
}
return false;
}
inline void computeSupport(const int &node)
{
for(int i = 0; i < L[node].size(); i ++)
{
if(supportRight[ L[node][i] ] == 0)
{
supportRight[ L[node][i] ] = 1;
supportLeft [ stanga[ L[node][i] ] ] = 0;
computeSupport( stanga[ L[node][i] ] );
}
}
}
int main()
{
fin >> N >> M;
for(int i = 1; i <= M; i ++)
{
fin >> a >> b;
L[a].push_back(b);
}
while(ok)
{
ok = false;
v.reset();
for(int i = 1; i <= N; i ++)
{
if(stanga[i] == 0 && cuplaj(i) == true)
{
cardCuplaj ++;
ok = true;
}
}
}
for(int i = 1; i <= N; i ++)
{
if(supportLeft[i] == 0)
computeSupport(i);
}
fout << 2 * N - cardCuplaj << '\n';
for(int i = 1; i <= N; i ++)
{
fout << (1 - supportLeft[i]) + 2 * (1 - supportRight[i]) << '\n';
}
return 0;
}