Cod sursa(job #2514201)

Utilizator Alex_BubBuburuzan Alexandru Alex_Bub Data 24 decembrie 2019 19:10:43
Problema Party Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.88 kb
#include <fstream>
#include <vector>
#include <cstring>

using namespace std;

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

const int NMax = 200;

vector <int> G[NMax + 5], GT[NMax + 5], V;
bool Use[NMax + 5];
int N, M, Sol[NMax + 5], Nr;

int Non(int x)
{
    return ((x > N) ? x - N : x + N);
}

void AddEdge(int x, int y)
{
    G[x].push_back(y);
    GT[y].push_back(x);
}

void DFSP(int Nod)
{
    Use[Nod] = true;

    for(auto Vecin : G[Nod])
        if(!Use[Vecin])
            DFSP(Vecin);

    V.push_back(Nod);
}

bool DFSM(int Nod)
{
    if(Sol[Nod])
    {
        Sol[0] = -1;
        return 0;
    }
    Sol[Non(Nod)] = 1;
    Use[Nod] = true;

    for(auto Vecin : GT[Nod])
        if(!Use[Vecin])
        {
            if(DFSM(Vecin) == 0)
                return 0;
        }
    return 1;
}

void Kosaraju()
{
    for(int i = 1; i <= 2 * N; i++)
        if(!Use[i])
            DFSP(i);

    for(int i = 1; i <= 2 * N; i++)
        Use[i] = 0;

    while(!V.empty())
    {
        int Nod = V.back(); V.pop_back();

        if(!Use[Nod] && !Use[Non(Nod)])
        {
            if(DFSM(Nod) == 0)
                return;
        }
    }
}
int main()
{
    fin >> N >> M;

    for(int i = 1, type, x, y; i <= M; i++)
    {
        fin >> type >> x >> y;
        x += (type & 2) / 2 * N;
        y += (type & 1) * N;

        int nx = Non(x), ny = Non(y);

        G[nx].push_back(y);
        G[ny].push_back(x);

        GT[x].push_back(ny);
        GT[y].push_back(nx);
    }
    Kosaraju();

    if(Sol[0] == -1)
    {
        fout << "0\n";
        return 0;
    }

    for(int i = 1; i <= N; i++)
        Nr += Sol[i];

    fout << Nr << '\n';

    for(int i = 1; i <= N; i++)
        if(Sol[i])
            fout << i << '\n';

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

    return 0;
}