Cod sursa(job #1029041)

Utilizator sziliMandici Szilard szili Data 14 noiembrie 2013 22:39:15
Problema 2SAT Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 3.38 kb
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <stack>

using namespace std;

typedef struct node{
    int index;
    int lowest;
} NODE;

int n,m;
int currentIndex = 1;
int isInStack[200002];

NODE visitedOrder[200002];

vector<int> adjacencyList[200002];

int componentsNr = 0;
vector<int> components[200002];

int decidedValue[200002];

stack<int> st;

int getVariable(int a){
    return n-a;
}

int transformVariable(int a){
    if (a < 0){
        return n-a;
    } else {
        return a;
    }
}

int negateVariable(int a){
    if (a<=n){
        return a+n;
    } else {
        return a-n;
    }
}

int isNegative(int a){
    if (a>n){
        return 1;
    } else
        return 0;
}

void readData(){
    freopen("2sat.in", "r", stdin);
    freopen("2sat.out", "w", stdout);

    scanf("%d %d", &n, &m);

    int term1, term2;
    for (int i=0; i<m; i++){
        scanf("%d %d", &term1, &term2);

        term1 = transformVariable(term1);
        term2 = transformVariable(term2);

        adjacencyList[negateVariable(term1)].push_back(term2);
        adjacencyList[negateVariable(term2)].push_back(term1);
    }
}

int getMin(int a, int b){
    if (a<b){
        return a;
    }
    else
        return b;
}

void dfs(int currentNode){
    isInStack[currentNode] = 1;
    visitedOrder[currentNode].index = currentIndex;
    visitedOrder[currentNode].lowest = currentIndex;
    currentIndex++;

    //go through all of the adjacent nodes;
    for (int i=0; i<adjacencyList[currentNode].size(); i++){
        int nodeToBeVisited = adjacencyList[currentNode][i];

        if (visitedOrder[nodeToBeVisited].index == 0){
            st.push(nodeToBeVisited);
            dfs(nodeToBeVisited);
            visitedOrder[currentNode].lowest = getMin(visitedOrder[nodeToBeVisited].lowest, visitedOrder[currentNode].lowest);
        } else if (isInStack[nodeToBeVisited]){
            visitedOrder[currentNode].lowest = getMin(visitedOrder[nodeToBeVisited].index, visitedOrder[currentNode].lowest);
        }
    }

    //check if a new connected component has been found
    if (visitedOrder[currentNode].index == visitedOrder[currentNode].lowest){
        int stackNode;

        do {
            stackNode = st.top();
            st.pop();
            isInStack[stackNode] = 0;

            components[componentsNr].push_back(stackNode);

        } while(stackNode != currentNode);

        componentsNr++;
    }

}


int main()
{
    readData();

    int ok = 1;

    for (int i=0; i<=2*n; i++){
        if (i!=n){
            if (!visitedOrder[i].index){
                st.push(i);
                dfs(i);
            }
        }
    }

    for (int i=componentsNr-1; i>=0; i--){
        if (decidedValue[components[i][0]] != 1){
            for (int j=0; j<components[i].size(); j++){
                int currentNode = components[i][j];
                decidedValue[negateVariable(currentNode)] = 1;
            }
        }
    }

    for (int i=0; i<=n; i++){
        if (i != n && decidedValue[i] == decidedValue[negateVariable(i)]){
            ok = 0;
            break;
        }
    }

    if (ok){
        for (int i=n+1; i<=2*n; i++){
            printf("%d ", decidedValue[i]);
        }
    } else {
        printf("-1");
    }

    return 0;
}