Cod sursa(job #2670678)

Utilizator dimi999Dimitriu Andrei dimi999 Data 10 noiembrie 2020 14:59:27
Problema 2SAT Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.65 kb
#include <bits/stdc++.h>
using namespace std;

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

vector <int> v[200005];

int n, k;

int llink[200005], idx[200005], ctc[200005], ct;

stack<int> st;

bool inst[200005];

int NEG(int x)
{
    return 2 * n - x;
}

void tarjan(int node)
{
    llink[node] = idx[node] = ++ k;

    inst[node] = true;

    st.push(node);

    for(int i = 0; i < v[node].size(); i++)
        if(idx[v[node][i]] == 0)
    {
        tarjan(v[node][i]);

        llink[node] = min(llink[node], llink[v[node][i]]);
    }
    else
        if(inst[v[node][i]] == true)
            llink[node] = min(llink[node], llink[v[node][i]]);

    if(idx[node] == llink[node])
       {
           int naux = st.top();

           ctc[naux] = ++ct;

           inst[naux] = false;

           st.pop();

           while(naux != node)
           {
                naux = st.top();

                ctc[naux] = ct;

                inst[naux] = false;

                st.pop();
           }
       }
}

int main()
{
    int m;

    fin >> n >> m;

    for(int i = 1; i <= m; i++)
    {
        int x, y;

        fin >> x >> y;

        v[NEG(x + n)].push_back(y + n);
        v[NEG(y + n)].push_back(x + n);
    }

    for(int i = 0; i <= 2 * n; i++)
        if(idx[i] == 0 && i != n)
        tarjan(i);

    for(int i = n - 1; i >= 0; i--)
        if(ctc[i] == ctc[2 * n - i])
    {
        fout << -1;
        return 0;
    }

    for(int i = n - 1; i >= 0; i--)
        if(ctc[i] < ctc[2 * n - i])
            fout << 0 << " ";
    else
        fout << 1 <<  ' ';
    return 0;
}