Cod sursa(job #3302079)

Utilizator iulia_morariuIuli Morariu iulia_morariu Data 2 iulie 2025 18:46:50
Problema Felinare Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.74 kb
#include <algorithm>
#include <iostream>
#include <fstream>
#include <climits>
#include <vector>
#include <stack>
#include <cmath>
// #include <bits/stdc++.h>
#define in  fin
#define out fout

using namespace std;

ifstream fin("felinare.in");
ofstream fout("felinare.out");

int cuplaj[17000];
vector<int> g[17000], newG[17000];
bool mr[17000];
bool in_mvc[17000];

bool pair_up(int nod){
    if(mr[nod]) return 0;
    mr[nod] = 1;

    for(const int &cop : g[nod]){
        if(!cuplaj[nod]){
            cuplaj[nod] = cop;
            cuplaj[cop] = nod;
            return 1;
        }
    }

    for(const int &cop : g[nod]){
        if(pair_up( cuplaj[cop] )){
            cuplaj[nod] = cop;
            cuplaj[cop] = nod;
            return 1;
        }
    }

    return 0;
}

bool scos[17000];
void dfs(int nod){ // minimum vertex cover
    if(mr[nod]) return;
    mr[nod] = 1;
    for(const int &cop : g[nod]){
        if(cuplaj[cop] != nod && !scos[cop]){
            scos[cop] = 1;
            dfs(cuplaj[cop]);
        }
    }
}

signed main(){
    ios_base::sync_with_stdio(false);
    in.tie(NULL);

    int n, m; in >> n >> m;

    for(int i = 0; i < m; i++){
        int a, b; in >> a >> b;
        g[a].push_back(b + n);
    }

    bool mai_bine = 1;
    while(mai_bine){
        mai_bine = 0;
        for(int i = 1; i <= n; i++) mr[i] = 0;
        for(int i = 1; i <= n; i++){
            if(!cuplaj[i] && pair_up(i)){
                mai_bine = 1;
            }
        }
    }

    int cnt = 2 * n;
    for(int i = 1; i <= 2 * n; i++) mr[i] = 0;
    for(int i = 1; i <= n; i++){
        if(cuplaj[i]) cnt--;
        else dfs(i);
    }
    out << cnt << '\n';
    for(int i = 1; i <= n; i++){
        out << mr[i] + 2 * (1 - scos[i]) << '\n';
    }

    return 0;
}