Cod sursa(job #1390040)

Utilizator Al3ks1002Alex Cociorva Al3ks1002 Data 16 martie 2015 20:13:58
Problema Felinare Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.87 kb
#include<cstdio>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<bitset>
#include<deque>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<unordered_map>

#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second

using namespace std;

const int nmax = 8205;

int n, m, x, y, ok, i, cuplaj, l[nmax], r[nmax], a[nmax], b[nmax];

vector<int> v[nmax];
bitset<nmax> viz;

bool pairup(int x)
{
    if(viz[x]) return 0;
    viz[x] = 1;

    for(auto it : v[x])
        if(!r[it] || pairup(r[it]))
        {
            l[x] = it;
            r[it] = x;
            return 1;
        }

    return 0;
}

void check(int x)
{
    for(auto it : v[x])
        if(!b[it])
        {
            b[it] = 1;
            a[r[it]] = 0;
            check(r[it]);
        }
}

int main()
{
    freopen("felinare.in", "r", stdin);
    freopen("felinare.out", "w", stdout);

    scanf("%d%d", &n, &m);

    for(; m; m--)
    {
        scanf("%d%d", &x, &y);
        v[x].pb(y);
    }

    for(ok = 1; ok;)
    {
        ok = 0;
        viz = 0;

        for(i = 1; i <= n; i++)
            if(!l[i] && pairup(i))
            {
                ok = 1;
                cuplaj++;
            }
    }

    for(i = 1; i <= n; i++)
        if(l[i])
            a[i] = 1;

    for(i = 1; i <= n; i++)
        if(!l[i])
            check(i);

    printf("%d\n", 2 * n - cuplaj);

    for(i = 1; i <= n; i++)
    {
        if(a[i] && b[i]) printf("0\n");
        else if(b[i]) printf("1\n");
        else if(a[i]) printf("2\n");
        else printf("3\n");
    }

    return 0;
}