Cod sursa(job #3282270)

Utilizator BuzdiBuzdugan Rares Andrei Buzdi Data 4 martie 2025 21:59:48
Problema Party Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.33 kb
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ll long long
#define ld long double

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

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

const int NMAX = 1000;

int n, m, ind, components;
vector<int> answer;
vector<int> g[NMAX + 1], gt[NMAX + 1];
int component[NMAX + 1];
int order[NMAX + 1];
bool visited[NMAX + 1];

void AddDisjunction(int a, bool na, int b, bool nb) {
    a = 2 * a - !na;
    b = 2 * b - !nb;
    int neg_a = a + (na ? -1 : 1);
    int neg_b = b + (nb ? -1 : 1);
    g[neg_a].push_back(b);
    g[neg_b].push_back(a);
}

void DFS1(int node) {
    visited[node] = 1;
    for(int next_node : g[node]) {
        if(!visited[next_node]) {
            DFS1(next_node);
        }
    }
    order[++ind] = node;
}

void DFS2(int node) {
    component[node] = components;
    for(int next_node : gt[node]) {
        if(!component[next_node]) {
            DFS2(next_node);
        }
    }
}

int main() {
    ios::sync_with_stdio(0);
    fin.tie(0);
    cout.tie(0);

    fin >> n >> m;
    for(int i = 1; i <= m; i++) {
        int type, x, y;
        fin >> x >> y >> type;
        if(type == 0) {
            AddDisjunction(x, 0, y, 0);
        }
        else if(type == 1) {
            g[2 * x].push_back(2 * y); 
        }
        else if(type == 2) {
            g[2 * y].push_back(2 * x);
        }
        else {
            AddDisjunction(x, 1, y, 1);
        }
    }

    for(int i = 1; i <= 2 * n; i++) {
        for(int j : g[i]) {
            gt[j].push_back(i);
        }
    }

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

    for(int i = 2 * n; i >= 1; i--) {
        if(!component[order[i]]) {
            components++;
            DFS2(order[i]);
        }
    }

    for(int i = 1; i <= n; i++) {
        assert(component[2 * i] != component[2 * i - 1]);
        if(component[2 * i] < component[2 * i - 1]) {
            answer.push_back(i);
        }
    }

    fout << answer.size() << '\n';
    for(int x : answer) {
        fout << x << '\n';
    }

    return 0;
}