Cod sursa(job #2604845)

Utilizator TheNextGenerationAyy LMAO TheNextGeneration Data 23 aprilie 2020 17:20:44
Problema Felinare Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.57 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in("felinare.in");
ofstream out("felinare.out");
const int N = 1<<13;
vector<int> v[N];
int L[N],R[N];
bool viz[N],useL[N],useR[N];
bool Match(int x)
{
    if (viz[x])
        return 0;
    viz[x] = 1;
    for (auto it: v[x])
        if (!R[it])
        {
            L[x] = it;
            R[it] = x;
            return 1;
        }
    for (auto it: v[x])
        if (Match(R[it]))
        {
            L[x] = it;
            R[it] = x;
            return 1;
        }
    return 0;
}
void dfs(int x)
{
    for (auto it: v[x])
        if (!useL[it])
        {
            useL[it] = 1;
            useR[R[it]] = 0;
            dfs(R[it]);
        }
}
int main()
{
    int n,m;
    in >> n >> m;
    for (int i = 1; i<=m; i++)
    {
        int x,y;
        in >> x >> y;
        v[x].push_back(y);
    }
    bool ok = 1;
    while (ok)
    {
        ok = 0;
        memset(viz,0,sizeof(viz));
        for (int i = 1; i<=n; i++)
            if (!R[i])
                ok|=Match(i);
    }
    for (int i = 1; i<=n; i++)
        if (L[i])
            useR[i] = 1;
    int ans = 2*n;
    for (int i = 1; i<=n; i++)
    {
        if (!L[i])
            dfs(i);
        else
            ans--;
    }
    out << ans << "\n";
    for (int i = 1; i<=n; i++)
    {
        if (useL[i] && useR[i])
            out << "0\n";
        else if (useR[i])
            out << "1\n";
        else if (useL[i])
            out << "2\n";
        else
            out << "3\n";
    }
}