Cod sursa(job #1956884)

Utilizator mariakKapros Maria mariak Data 7 aprilie 2017 09:35:28
Problema Felinare Scor 2
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.76 kb
#include <bits/stdc++.h>
#define maxN 8193

FILE *fin  = freopen("felinare.in", "r", stdin);
FILE *fout = freopen("felinare.out", "w", stdout);

using namespace std;
int N, M;
int card;
int Left[maxN], Right[maxN];
bool suppLeft[maxN], suppRight[maxN];
bitset <maxN> used;
vector <int> G[maxN];

bool PairUp(int node){
    if(used.test(node)) return 0;
    used.set(node);
    for(int son : G[node])
        if(!Left[son]){
            suppLeft[node] = true;
            ++ card;
            Right[node] = son;
            Left[son] = node;
            return 1;
        }
    for(int son : G[node])
        if(PairUp(Left[node])){
            suppLeft[node] = true;
            Right[node] = son;
            Left[son] = node;
            return 1;
        }
    return 0;
}

void Support(int node){
    for(int son : G[node])
        if(!suppRight[node]){
            suppRight[node] = true;
            suppLeft[Left[son]] = false;
            Support(Left[son]);
        }
}

void MvC(){
    bool change;
    do{
        change = false;
        used.reset();
        for(int i = 1; i <= N; ++ i)
            if(!suppLeft[i])
                change |= PairUp(i);
    }while(change);

    for(int i = 1; i <= N; ++ i)
        if(!suppLeft[i])
            Support(i);

}

int main(){
    scanf("%d %d", &N, &M);
    for(int i = 1; i <= M; ++ i){
        int x, y;
        scanf("%d %d", &x, &y);
        G[x].push_back(y);
        //G[y].push_back(x);
    }

    MvC();
    printf("%d\n", 2*N - card);
    for(int i = 1; i <= N; i++) {
        int val = 3;
        if(suppLeft[i]){
            val--;
        }
        if(suppRight[i]){
            val -= 2;
        }
        printf("%d\n", val);
    }
    return 0;
}