Cod sursa(job #883856)

Utilizator sebii_cSebastian Claici sebii_c Data 20 februarie 2013 14:40:52
Problema Felinare Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.8 kb
#include <cstdio>
#include <cstring>

#include <vector>

using namespace std;

typedef vector<int>::iterator iter;

const int MAXN = 16400;

vector<int> G[MAXN];

int l[MAXN];
int r[MAXN];
bool vis[MAXN];

int pair_up(int node)
{
    if (vis[node])
        return 0;
    vis[node] = true;

    for (iter it = G[node].begin(); it != G[node].end(); ++it) {
        if (!r[*it]) {
            r[*it] = node;
            l[node] = *it;
            return 1;
        }
    }
    for (iter it = G[node].begin(); it != G[node].end(); ++it) {
        if (pair_up(r[*it])) {
            r[*it] = node;
            l[node] = *it;
            return 1;
        }
    }
    return 0;
}

int deg[MAXN];

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

    int n, m;
    scanf("%d %d", &n, &m);
    for (int i = 0; i < m; ++i) {
        int x, y;
        scanf("%d %d", &x, &y);
        y += n;
        G[x].push_back(y);
        deg[x]++; deg[y]++;
    }

    for (int poss = 1; poss != 0; ) {
        poss = 0;
        memset(vis, false, sizeof(vis));
        for (int i = 1; i <= n; ++i) 
            if (!l[i]) 
                poss |= pair_up(i);
    }

    int coupling = 0;
    for (int i = 1; i <= n; ++i)
        if (l[i] != 0) ++coupling;

    printf("%d\n", 2 * n - coupling);
    vector<int> action(n + 1, 3);
    for (int i = 1; i <= n; ++i) {
        if (l[i] != 0) {
            if (deg[i] > deg[l[i]]) {
                if (action[i] == 1)
                    action[i] = 0;
                else action[i] = 2;
            } else if (deg[i] <= deg[l[i]]) {
                if (action[l[i] - n] == 2)
                    action[i] = 2;
                else action[l[i] - n] = 1;
            }
        }
    }

    for (int i = 1; i <= n; ++i)
        printf("%d\n", action[i]);

    return 0;
}