Cod sursa(job #3304526)

Utilizator Mihai_OctMihai Octavian Mihai_Oct Data 24 iulie 2025 15:33:15
Problema Party Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.99 kb
#include <bits/stdc++.h>

using namespace std;

#define ST_DIO 0
#if ST_DIO
    #define fin cin
    #define fout cout
#else
    ifstream fin("party.in");
    ofstream fout("party.out");
#endif  // ST_DIO

int n, m, i, j, comp, nrRasp;
vector<int> gr[2][2 * 1002];
stack<int> st;

bool rasp[2 * 1002];

int scc[2 * 1002];

bool fr[2 * 1002];

static inline int NodToNot(int a) {
    if(a > n) return n - a;
	return a;
}

static inline int NotToNod(int a) {
    if(a < 0) return -a + n;
    return a;
}

static inline void DFS1(int nod) {
    fr[nod] = true;
    for(int urm : gr[0][nod]) {
        if(!fr[urm]) DFS1(urm);
    }
    st.push(nod);
}

static inline void DFS2(int nod, int cmp) {
    fr[nod] = true;
    scc[nod] = cmp;

    rasp[NotToNod(nod)] = true;
    rasp[NotToNod(-NodToNot(nod))] = false;

    for(int urm : gr[1][nod]) {
        if(!fr[urm]) DFS2(urm, cmp);
    }
}

int main() {
    fin >> n >> m;
    for(int i = 1; i <= m; i++) {
        int tip, a, b, x, y;

        fin >> a >> b >> tip;
             if(tip == 0) x =  a, y =  b;
        else if(tip == 1) x =  a, y = -b;
        else if(tip == 2) x = -a, y =  b;
        else if(tip == 3) x = -a, y = -b;

        gr[0][NotToNod(-x)].push_back(NotToNod( y));
        gr[0][NotToNod(-y)].push_back(NotToNod( x));
        gr[1][NotToNod( y)].push_back(NotToNod(-x));
        gr[1][NotToNod( x)].push_back(NotToNod(-y));
    }

    for(i = 1; i <= 2 * n; i++) {
        if(!fr[i]) DFS1(i);
    }

    memset(fr, 0, sizeof(fr));
    while(!st.empty()) {
        int nod = st.top();
        st.pop();

        if(!fr[nod]) {
            DFS2(nod, ++comp);
        }
    }

    for(i = 1; i <= n; i++) {
        nrRasp += rasp[i];
        /*if(scc[i] == scc[i + n]) {
            fout << -1;
            return 0;
        }*/
    }

    fout << nrRasp << "\n";
    for(i = 1; i <= n; i++) {
        if(scc[i] > scc[i + n]) fout << i << "\n";
    }

    return 0;
}