Cod sursa(job #2831391)

Utilizator rapidu36Victor Manz rapidu36 Data 11 ianuarie 2022 12:08:29
Problema 2SAT Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 2.98 kb
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define N 200001
#define M 400001

typedef struct element element;

struct element
{
    int vf, urm;
};

element succesori[M], predecesori[M];

int stiva[N], vf, n, nrs, nrp, nrc, lst_s[N], lst_p[N], ctc[N], nc;

bool viz[N], sol[N], exista_sol;

int negatie(int x)
{
    if (x > n)
    {
        return x - n;
    }
    return x + n;
}

void adauga_succesor(int x, int y)
{
    ///il adaug pe y in lista de succesori a lui x
    ++nrs;
    succesori[nrs].vf = y;
    succesori[nrs].urm = lst_s[x];
    lst_s[x] = nrs;
}

void adauga_predecesor(int y, int x)
{
    ///il adauga pe x in lista de predecesori a lui y
    ++nrp;
    predecesori[nrp].vf = x;
    predecesori[nrp].urm = lst_p[y];
    lst_p[y] = nrp;
}

void dfs(int x)
{
    viz[x] = true;
    int p = lst_s[x];
    while (p != 0)
    {
        int y = succesori[p].vf;
        if (!viz[y])
        {
            dfs(y);
        }
        p = succesori[p].urm;
    }
    stiva[++vf] = x;///x e adaugat in stiva cand m-am blocat in el (nu mai exista vf. neviz. in care pot ajunge din x)
}

void dfs_t(int x)
{
    viz[x] = true;
    ctc[x] = nc;///il adauga pe x in lista coresp. componentei nc
    if (ctc[negatie(x)] == ctc[x])
    {
        exista_sol = false;
    }
    else if (ctc[negatie(x)] == 0)
    {
        sol[x] = false;
        sol[negatie(x)] = true;
    }
    int p = lst_p[x];
    while (p != 0)
    {
        int y = predecesori[p].vf;
        if (!viz[y])
        {
            dfs_t(y);
        }
        p = predecesori[p].urm;
    }
}

int main()
{
    FILE *in, *out;
    in = fopen("2sat.in", "r");
    out = fopen("2sat.out", "w");
    int m;
    fscanf(in, "%d%d", &n, &m);
    for (int i = 0; i < m; i++)
    {
        int x, y;
        fscanf(in, "%d%d", &x, &y);
        if (x < 0)
        {
            x = -x + n;
        }
        if (y < 0)
        {
            y = -y + n;
        }
        adauga_succesor(negatie(x), y);
        adauga_predecesor(y, negatie(x));///listele pred. sunt necesare pentru dfs pe graful transpus
        adauga_succesor(negatie(y), x);
        adauga_predecesor(x, negatie(y));///listele pred. sunt necesare pentru dfs pe graful transpus
    }
    fclose(in);

    ///construiesc stiva -> sortare topologica
    for (int i = 1; i <= 2*n; i++)
    {
        if (!viz[i])
        {
            dfs(i);
        }
    }

    memset(viz, 0, N);
    exista_sol = true;
    while (vf > 0)
    {
        int x = stiva[vf--];///am scos x din stiva
        if (!viz[x])
        {
            ++nc;
            dfs_t(x);///viziteaza (folosind graful transpus) ctc a lui x
        }
    }

    if (!exista_sol)
    {
        fprintf(out, "-1");
    }
    else
    {
        for (int i = 1; i <= n; i++)
        {
            fprintf(out, "%d ", sol[i]);
        }
    }
    fclose(out);
    return 0;
}