Cod sursa(job #3195162)

Utilizator PetraPetra Hedesiu Petra Data 20 ianuarie 2024 10:49:11
Problema Felinare Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.97 kb
#include <iostream>
#include <fstream>
#include <bitset>
#include <vector>

#define NMAX 8193

using namespace std;
typedef vector<int>::iterator iter;

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

struct noduri {
    int nod, index;
};
struct muchie {
    int x, y;
};

int n, m, index_l[NMAX], index_r[NMAX];
vector<int> nod_l[NMAX], nod_r[NMAX], G[NMAX];
vector<muchie> muchii;
bitset<NMAX> viz;

void citire()
{
    fin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        int x, y;
        fin >> x >> y;
        G[x].push_back(y);
        muchii.push_back({x, y});
    }
}

int match(int nod)
{
    if (viz[nod])
        return false;

    viz[nod] = true;

    for (iter it = G[nod].begin(); it != G[nod].end(); it++)
    {
        if (index_l[*it] == 0)
        {
            index_l[nod] = 1;
            index_r[*it] = 1;

            return true;
        }
        if (match(*it))
        {
            index_l[nod] = 1;
            index_r[*it] = 1;

            return true;
        }
    }

    return false;
}

int main()
{
    citire();

    bool ok = true;
    while (ok)
    {
        ok = false;
        viz.reset();

        for (int i = 1; i <= n; i++)
        {
            if (index_l[i] == 0)
                ok |= match(i);
        }
    }

    int sol = 0;
    for (int i = 0; i < muchii.size(); i++)
    {
        sol += (index_l[muchii[i].x] + index_r[muchii[i].y]);
    }
    fout << sol << "\n";

    for (int i = 0; i < muchii.size(); i++)
    {
        if (index_l[muchii[i].x] == 0 && index_r[muchii[i].y] == 1)
            fout << 0 << "\n";
        if (index_l[muchii[i].x] == 1 && index_r[muchii[i].y] == 0)
            fout << 1 << "\n";
        if (index_l[muchii[i].x] == 0 && index_r[muchii[i].y] == 1)
            fout << 2 << "\n";
        if (index_l[muchii[i].x] == 1 && index_r[muchii[i].y] == 1)
            fout << 3 << "\n";
    }

    return 0;
}