Cod sursa(job #2586073)

Utilizator Alex_BubBuburuzan Alexandru Alex_Bub Data 19 martie 2020 18:29:45
Problema Felinare Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.77 kb
#include <fstream>
#include <vector>
#include <cstring>

using namespace std;

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

const int NMax = 26;

int N, M, L[NMax + 5], R[NMax + 5];
char x;
bool Use[NMax + 5], useL[NMax + 5], useR[NMax + 5];

vector <int> G[NMax + 5];

bool Cuplaj(int Nod)
{
    if(Use[Nod]) return 0;
    Use[Nod] = 1;

    for(auto Vecin : G[Nod])
        if(R[Vecin] == 0)
        {
            L[Nod] = Vecin;
            R[Vecin] = Nod;
            return 1;
        }

    for(auto Vecin : G[Nod])
        if(R[Vecin] && Cuplaj(R[Vecin]))
        {
            L[Nod] = Vecin;
            R[Vecin] = Nod;
            return 1;
        }
    return 0;
}

void Solve()
{
    bool ok;

    do
    {
        ok = 0; memset(Use, 0, sizeof(Use));

        for(int i = 1; i <= N; i++)
            if(!L[i])
                ok |= Cuplaj(i);
    }
    while(ok);
}

void PairUp(int Nod)
{
    for(auto Vecin : G[Nod])
        if(!useR[Vecin])
        {
            useR[Vecin] = 1, useL[R[Vecin]] = 0;
            PairUp(R[Vecin]);
        }
}

void Suport()
{
    for(int i = 1; i <= N; i++)
        useL[i] = (L[i] != 0);

    for(int i = 1; i <= N; i++)
        if(!useL[i])
            PairUp(i);
}

int main()
{
    fin >> N >> M;

    for(int i = 1; i <= N; i++)
        for(int j = 1; j <= M; j++)
        {
            fin >> x;

            if(x == '1')
                G[i].push_back(j);
        }
    Solve();
    Suport();

    for(int i = 1; i <= N; i++)
        if(useL[i])
            fout << (char)('A' + i - 1);

    for(int i = 1; i <= M; i++)
        if(useR[i])
            fout << (char)('a' + i - 1);

    fin.close();
    fout.close();

    return 0;
}