Cod sursa(job #1882339)

Utilizator LucianTLucian Trepteanu LucianT Data 17 februarie 2017 09:41:02
Problema Felinare Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.73 kb
#include <bits/stdc++.h>
#define maxN 8200
using namespace std;
int sol[maxN];
bool seen[maxN],ok;
int _left[maxN],_right[maxN];
int supleft[maxN],supright[maxN];
int n,m,i,j,x,y,maxmatch;
vector<int> v[maxN];
bool pairup(int nod)
{
    if(seen[nod])
        return false;
    seen[nod]=true;
    for(auto it:v[nod])
        if(!_right[it]) /* not matched */
        {
            _left[nod]=it;
            _right[it]=nod;
            return true;
        }
    for(auto it:v[nod])
        if(pairup(_right[it])) /* try to re-match */
        {
            _left[nod]=it;
            _right[it]=nod;
            return true;
        }
    return false;
}
void support(int nod) /* building minimum support */
{
    for(auto it:v[nod])
        if(!supright[it])
        {
            supright[it]=1;
            supleft[_right[it]]=0;
            support(_right[it]);
        }
}
int main()
{
    freopen("felinare.in","r",stdin);
    freopen("felinare.out","w",stdout);
    scanf("%d %d",&n,&m);
    for(i=1;i<=m;i++)
        scanf("%d %d",&x,&y),
        v[x].push_back(y);
    ok=true;
    while(ok) /* run max-matching */
    {
        ok=false;
        memset(seen,false,sizeof(seen));
        for(i=1;i<=n;i++)
            if(!_left[i] && pairup(i))
                ok=true;
    }
    for(i=1;i<=n;i++)
        if(_left[i])
            supleft[i]=1,
            maxmatch++;
    printf("%d\n",2*n-maxmatch);
    for(i=1;i<=n;i++)
        if(!_left[i])
            support(i);
    for(i=1;i<=n;i++)
    {
        if(!supleft[i])
            sol[i]++;
        if(!supright[i])
            sol[i]+=2; /* if both sol[i]=3 */
    }
    for(i=1;i<=n;i++)
        printf("%d\n",sol[i]);
    return 0;
}