Cod sursa(job #2514180)

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

using namespace std;

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

const int NMax = 200;

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

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

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

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

    for(auto Next : G[Node])
        if(!Use[Next])
            DFSP(Next);

    Topo.push_back(Node);
}

void DFSM(int Node)
{
    Use[Node] = true;
    Sol[non(Node)] = 1;

    for(auto Next : G[Node])
        if(!Use[Next])
            DFSM(Next);
}

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

    memset(Use, 0, sizeof(Use));

    while(!Topo.empty())
    {
        int Node = Topo.back(); Topo.pop_back();

        if(!Use[Node] && !Use[non(Node)])
            DFSM(Node);
    }
}

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

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

        AddEdge(non(x), y);
        AddEdge(non(y), x);
    }
    Kosaraju();

    for(int i = 1; i <= N; i++)
        if(Sol[i])
            FinalList.push_back(i);

    fout << FinalList.size() << '\n';

    for(auto x : FinalList)
        fout << x << '\n';

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

    return 0;
}