Cod sursa(job #2593483)

Utilizator IulianOleniucIulian Oleniuc IulianOleniuc Data 3 aprilie 2020 23:55:42
Problema Andrei Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.5 kb
#include <bits/stdc++.h>
using namespace std;

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

class TwoSat {
  private:
    int n;
    vector<vector<int>> ad;

    vector<int> topo;
    vector<bool> vis, ans;

    inline int non(int x) {
        return (x > n ? x - n : x + n);
    }

    void dfs(int node) {
        vis[node] = true;
        for (int nghb : ad[node])
            if (!vis[nghb])
                dfs(nghb);
        topo.push_back(node);
    }

  public:
    TwoSat(int n) :
        n(n), ad(2 * n + 1) { }

    void addProp(int x, int y) {
        if (x < 0) x = n - x;
        if (y < 0) y = n - y;
        ad[non(x)].push_back(y);
        ad[non(y)].push_back(x);
    }

    void solve() {
        vis.resize(2 * n + 1);
        for (int i = 1; i <= 2 * n; i++)
            if (!vis[i])
                dfs(i);
        reverse(topo.begin(), topo.end());

        ans.resize(2 * n + 1);
        for (int node : topo)
            if (!ans[node] && !ans[non(node)])
                ans[node] = true;

        for (int i = 1; i <= n; i++)
            fout << ans[i] << ' ';
        fout << '\n';
    }
};

int main() {
    int n, m; fin >> n >> m;
    TwoSat graph(n);
    for (int i = 0; i < m; i++) {
        int x, y, t; fin >> x >> y >> t;
        if (t == 0)
            graph.addProp(+x, +y);
        else if (t == 1)
            graph.addProp(-x, -y);
        else {
            graph.addProp(-x, +y);
            graph.addProp(+x, -y);
        }
    }
    graph.solve();

    fout.close();
    return 0;
}