Cod sursa(job #3287236)

Utilizator Edyci123Bicu Codrut Eduard Edyci123 Data 16 martie 2025 23:41:49
Problema Party Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.05 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <stack>
#include <bitset>

#define NMAX 105

using namespace std;

ifstream f("party.in");
ofstream g("party.out");

int variables, expressions;
int no_components;
int components[2 * NMAX];
vector<int> edges[2 * NMAX], edges1[2 * NMAX];
bitset<2 * NMAX> visited;
stack<int> s;

int neg(int x) {
    return  x + variables * (x > variables ? -1 : 1);
}


void dfs(int node) {
    visited[node] = 1;

    for (auto child : edges[node])
        if (!visited[child])
            dfs(child);
    
    s.push(node);
}

void dfs1(int node) {
    components[node] = no_components;

    for (auto child : edges1[node])
        if (!components[child])
            dfs1(child);
}

int main() {
    f >> variables >> expressions;

    for (int i = 1; i <= expressions; i++) {
        int x, y, z;
        bool x_neg, y_neg;
        f >> x >> y >> z;
        if (z == 0) { // (x or y) must be true
            x_neg = 0;
            y_neg = 0;
        } else if (z == 1) { // (x or !y) must be true
            x_neg = 0;
            y_neg = 1;
        } else if (z == 2) { // (!x or y) must be true
            x_neg = 1;
            y_neg = 0;
        } else { // (!x or !y) must be true
            x_neg = 1;
            y_neg = 1;
        }
        
        x = x + variables * x_neg;
        y = y + variables * y_neg;
        int neg_x = neg(x), neg_y = neg(y);
        edges[neg_x].push_back(y);
        edges[neg_y].push_back(x);
        edges1[x].push_back(neg_y);
        edges1[y].push_back(neg_x);
    }

    for (int i = 1; i <= 2 * variables; i++)
        if (!visited[i])
            dfs(i);


    while (!s.empty()) {
        int node = s.top();
        s.pop();
        if (!components[node]) {
            no_components++;
            dfs1(node);
        }
    }


    vector<int> ans;
    for (int i = 1; i <= variables; i++) {
        if (components[i] > components[i + variables]) {
            ans.push_back(i);
        }
    }

    g << ans.size() << "\n";
    for (auto x : ans) {
        g << x << "\n";
    }

    return 0;
}