Cod sursa(job #2787086)

Utilizator preda.andreiPreda Andrei preda.andrei Data 22 octombrie 2021 14:28:47
Problema Party Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.59 kb
#include <algorithm>
#include <fstream>
#include <set>
#include <stack>
#include <vector>

using namespace std;

struct Node {
    set<int> edges;
    int time = -1;
    int low = -1;
    bool in_stack = false;

    bool has_value = false;
    bool value = false;
};

struct Graph {
    Graph(int vars);

    int Vars() const;

    void AddCond(int a, int b, int type);

    Node& operator[](int index);
    const Node& operator[](int index) const;

private:
    int RealIndex(int index) const;

    vector<Node> nodes_;
};

Graph::Graph(int vars) : nodes_(2 * vars) {
}

int Graph::Vars() const {
    return nodes_.size() / 2;
}

void Graph::AddCond(int a, int b, int type) {
    switch (type) {
    case 0:
        nodes_[RealIndex(-a)].edges.insert(b);
        nodes_[RealIndex(-b)].edges.insert(a);
        break;
    case 1:
        nodes_[RealIndex(-a)].edges.insert(-b);
        nodes_[RealIndex(b)].edges.insert(a);
        break;
    case 2:
        nodes_[RealIndex(-b)].edges.insert(-a);
        nodes_[RealIndex(a)].edges.insert(b);
        break;
    case 3:
        nodes_[RealIndex(a)].edges.insert(-b);
        nodes_[RealIndex(b)].edges.insert(-a);
        break;
    }
}

Node& Graph::operator[](int index) {
    return nodes_[RealIndex(index)];
}

const Node& Graph::operator[](int index) const {
    return nodes_[RealIndex(index)];
}

int Graph::RealIndex(int index) const {
    return index > 0 ? (index - 1) : (nodes_.size() + index);
}

void ExtractComp(Graph& graph, int end_node, stack<int>& st, vector<int>& order) {
    while (order.empty() || order.back() != end_node) {
        auto node = st.top();
        st.pop();
        graph[node].in_stack = false;
        order.push_back(node);
    }
}

void Tarjan(Graph& graph, int node, stack<int>& st, vector<int>& order) {
    static auto time = 0;
    graph[node].time = graph[node].low = (time += 1);

    st.push(node);
    graph[node].in_stack = true;

    for (const auto& next : graph[node].edges) {
        if (graph[next].time == -1) {
            Tarjan(graph, next, st, order);
            graph[node].low = min(graph[node].low, graph[next].low);
        }
        if (graph[next].in_stack) {
            graph[node].low = min(graph[node].low, graph[next].time);
        }
    }

    if (graph[node].low >= graph[node].time) {
        ExtractComp(graph, node, st, order);
    }
}

vector<int> FindOrder(Graph& graph) {
    vector<int> order;
    for (int i = 1; i <= graph.Vars(); i += 1) {
        for (int sign = 1; sign >= -1; sign -= 2) {
            auto node = sign * i;
            if (graph[node].time == -1) {
                stack<int> st;
                Tarjan(graph, node, st, order);
            }
        }
    }
    reverse(order.begin(), order.end());
    return order;
}

vector<int> Solve(Graph& graph) {
    for (const auto& node : FindOrder(graph)) {
        if (!graph[node].has_value) {
            graph[node].value = false;
            graph[node].has_value = true;
            graph[-node].value = true;
            graph[-node].has_value = true;
        }
    }

    vector<int> res;
    for (int i = 1; i <= graph.Vars(); i += 1) {
        if (graph[i].value) {
            res.push_back(i);
        }
    }
    return res;
}

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

    int vars, conds;
    fin >> vars >> conds;

    Graph graph(vars);
    for (int i = 0; i < conds; i += 1) {
        int a, b, type;
        fin >> a >> b >> type;
        graph.AddCond(a, b, type);
    }

    auto res = Solve(graph);
    fout << res.size() << "\n";
    for (const auto& node : res) {
        fout << node << "\n";
    }

    return 0;
}