Cod sursa(job #2218398)

Utilizator TheNextGenerationAyy LMAO TheNextGeneration Data 4 iulie 2018 13:33:48
Problema 2SAT Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.25 kb
#include <bits/stdc++.h>

using namespace std;

ifstream in("2sat.in");
ofstream out("2sat.out");

map<int, vector<int> > v,vt;
const int NMAX = 100005;
bool contr;
map<int,int> viz;
int nr;
stack<int> st;
void dfp(int x)
{
    viz[x] = 1;
    for (auto it: v[x])
        if (!viz[it])
            dfp(it);
    st.push(x);
}
void dfm(int x)
{
    viz[x] = nr;
    for (auto it: vt[x])
        if (!viz[it])
            dfm(it);
}

int main()
{
    int n,m;
    in >> n >> m;
    for (int i = 1; i<=m; i++)
    {
        int x,y;
        in >> x >> y;
        v[-x].push_back(y);
        v[-y].push_back(x);
        vt[y].push_back(-x);
        vt[x].push_back(-y);
    }
    for (int i = -n; i<=n; i++)
        if (!viz[i])
            dfp(i);
    viz.clear();
    while (!st.empty())
    {
        int now = st.top();
        if (!viz[now])
        {
            nr++;
            dfm(now);
        }
        st.pop();
    }
    for (int i = 1; i<=n; i++)
        if (viz[i] == viz[-i])
            contr = 1;
    if (contr)
        out << "-1";
    else
    {
        for (int i = 1; i<=n; i++)
            if (viz[i]<viz[-i])
                out << "0 ";
            else
                out << "1 ";
    }
}