Cod sursa(job #2484059)

Utilizator PredescuSebastianIonPredescu Sebastian Ion PredescuSebastianIon Data 30 octombrie 2019 17:30:48
Problema Felinare Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.87 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("felinare.in");
ofstream g("felinare.out");
int n,m,x,y,i,l[10005],r[10005];
bool marker[10005],st[10005],dr[10005];
vector <int> v[10005];
bool cupleaza(int nod)
{
    marker[nod]=true;
    for(auto it : v[nod])
    {
        if(!r[it])
        {
            r[it]=nod;
            l[nod]=it;
            return true;
        }
    }
    for(auto it : v[nod])
    {
        if(!marker[r[it]] && cupleaza(r[it]))
        {
            r[it]=nod;
            l[nod]=it;
            return true;
        }
    }
    return false;
}
void solve(int x)
{
    for(auto it : v[x])
    {
        if(!dr[it])
        {
            dr[it]=true;
            st[r[it]]=false;
            solve(r[it]);
        }
    }
}
int main()
{
    f>>n>>m;
    for(i=1; i<=m; i++)
    {
        f>>x>>y;
        v[x].push_back(y);
    }
    bool ok=false;
    while(!ok)
    {
        ok=true;
        for(int i=1; i<=n; i++)
        {
            marker[i]=false;
        }
        for(i=1; i<=n; i++)
        {
            if(!l[i] && !marker[i])
            {
                if(cupleaza(i))
                {
                    ok=false;
                }
            }
        }
    }
    int s=0;
    for(int i=1;i<=n;i++)
    {
        if(l[i])st[i]=true;
    }
    for(int i=1;i<=n;i++)
    {
        if(!st[i])solve(i);
    }
    for(i=1;i<=n;i++)
    {
        s+=st[i]+dr[i];
    }
    g<<n*2-s<<'\n';
    for(i=1;i<=n;i++)
    {
        if(st[i] && dr[i])
        {
            g<<"0"<<'\n';
        }
        else if(!st[i] && dr[i])
        {
            g<<"1"<<'\n';
        }
        else if(st[i] && !dr[i])
        {
            g<<"2"<<'\n';
        }
        else if(!st[i] && !dr[i])
        {
            g<<"3"<<'\n';
        }
    }
    return 0;
}