Cod sursa(job #3240228)

Utilizator Sasha_12454Eric Paturan Sasha_12454 Data 13 august 2024 12:29:57
Problema Party Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.39 kb
#include <bits/stdc++.h>

const std :: string FILENAME = "party";

std :: ifstream in (FILENAME + ".in");

std :: ofstream out (FILENAME + ".out");

const int NMAX = 1e2 + 5;

int n;

int m;

int cer;

int x;

int y;

int nx;

int ny;

int cnt;

int comp[NMAX];

std :: vector <int> v[NMAX];

std :: vector <int> t[NMAX];

std :: bitset <NMAX> visited;

std :: vector <int> tout;

std :: vector <int> ans;

void dfs(int nod)
{
    for(int i : v[nod])
    {
        if(visited[i] == false)
        {
            visited[i] = true;

            dfs(i);
        }
    }

    tout.push_back(nod);
}

void dfs1(int nod)
{
    comp[nod] = cnt;

    for(int i : t[nod])
    {
        if(visited[i] == false)
        {
            visited[i] = true;

            dfs1(i);
        }
    }
}

int main()
{

    in >> n >> m;

    for(int i = 1; i <= m; i ++)
    {
        in >> x >> y >> cer;

        nx = x + n;

        ny = y + n;

        if(cer == 0)// x /\ y
        {
            v[nx].push_back(y);

            t[y].push_back(nx);

            v[ny].push_back(x);

            t[x].push_back(ny);
        }
        else if(cer == 1)// x /\ ny
        {
            v[nx].push_back(ny);

            t[ny].push_back(nx);

            v[y].push_back(x);

            t[x].push_back(y);
        }
        else if(cer == 2)// nx /\ y
        {
            v[x].push_back(y);

            t[y].push_back(x);

            v[ny].push_back(nx);

            t[nx].push_back(ny);
        }
        else// nx /\ ny
        {
            v[x].push_back(ny);

            t[ny].push_back(x);

            v[y].push_back(nx);

            t[nx].push_back(y);
        }
    }

    for(int i = 1; i <= 2 * n; i ++)
    {
        if(visited[i] == false)
        {
            visited[i] = true;

            dfs(i);
        }
    }

    std :: reverse(tout.begin(), tout.end());

    visited &= 0;

    for(int i : tout)
    {
        if(visited[i] == false)
        {
            cnt ++;

            visited[i] = true;

            dfs1(i);
        }
    }

    for(int i = 1; i <= n; i ++)
    {
        if(comp[i] > comp[i + n])
        {
            ans.push_back(i);
        }
    }

    out << ans.size() << '\n';

    for(int i : ans)
    {

        out << i << " ";
    }

    return 0;
}