Cod sursa(job #3173789)

Utilizator IvanAndreiIvan Andrei IvanAndrei Data 23 noiembrie 2023 18:18:21
Problema Party Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.03 kb
#include <bits/stdc++.h>

using namespace std;

const int max_size = 2e2 + 1;

int viz[max_size], ctc[max_size], n;
vector <int> mc[max_size], invmc[max_size], topsort;

/// 1: p -> !q
/// 2: !p -> q
/// 3: !p -> !q, !q -> !p

int neg (int x)
{
    if (x > n)
    {
        return x - n;
    }
    return x + n;
}

void dfs (int nod)
{
    viz[nod] = 1;
    for (auto f : mc[nod])
    {
        if (!viz[f])
        {
            dfs(f);
        }
    }
    topsort.push_back(nod);
}

void kos (int nod, int comp)
{
    viz[nod] = 0;
    ctc[nod] = comp;
    for (auto f : invmc[nod])
    {
        if (viz[f])
        {
            kos(f, comp);
        }
    }
}

signed main ()
{
#ifdef LOCAL
    freopen("test.in", "r", stdin);
    freopen("test.out", "w", stdout);
#else
    freopen("party.in", "r", stdin);
    freopen("party.out", "w", stdout);
#endif // LOCAL
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int m;
    cin >> n >> m;
    while (m--)
    {
        int x, y, c;
        cin >> x >> y >> c;
        if (c == 1)
        {
            y = neg(y);
        }
        if (c == 2)
        {
            x = neg(x);
        }
        if (c == 3)
        {
            x = neg(x);
            y = neg(y);
        }
        mc[neg(x)].push_back(y);
        mc[neg(y)].push_back(x);
        invmc[x].push_back(neg(y));
        invmc[y].push_back(neg(x));
    }
    for (int i = 1; i <= 2 * n; i++)
    {
        if (!viz[i])
        {
            dfs(i);
        }
    }
    reverse(topsort.begin(), topsort.end());
    int comp = 1;
    for (auto f : topsort)
    {
        if (viz[f])
        {
            kos(f, comp);
            comp++;
        }
    }
    vector <int> ans;
    for (int i = 1; i <= n; i++)
    {
        if (ctc[i] > ctc[i + n])
        {
            ans.push_back(i);
        }
    }
    cout << ans.size() << '\n';
    for (auto f : ans)
    {
        cout << f << '\n';
    }
    return 0;
}