Cod sursa(job #3171692)

Utilizator Kln1000Ciobanu Bogdan Kln1000 Data 19 noiembrie 2023 13:51:26
Problema 2SAT Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.89 kb
#include <fstream>
#include <vector>
#include <bitset>
#include <cstring>

#define NM 100010

using namespace std;

class parser
{
public:
    parser() {}
    parser(const char *file_name)
    {
        input_file.open(file_name,ios::in | ios::binary);
        input_file.sync_with_stdio(false);
        index&=0;
        input_file.read(buffer,SIZE);
    }
    inline parser &operator >>(int &n)
    {
        for (; buffer[index]<'0' or buffer[index]>'9'; inc());
        n&=0;
        sign&=0;
        sign|=(buffer[index-1]=='-');
        for (; '0'<=buffer[index] and buffer[index]<='9'; inc())
            n=10*n+buffer[index]-'0';
        n^=((n^-n)&-sign);
        return *this;
    }
    ~parser()
    {
        input_file.close();
    }
private:
    fstream input_file;
    static const int SIZE=0x400000;
    char buffer[SIZE];
    unsigned index,sign;
    inline void inc()
    {
        if(++index==SIZE)
            index&=0,input_file.read(buffer,SIZE);
    }
};

class writer
{
public:
    writer() {};
    writer(const char *file_name)
    {
        output_file.open(file_name,ios::out | ios::binary);
        output_file.sync_with_stdio(false);
        index=0;
    }
    inline writer &operator <<(int target)
    {
        aux=0;
        n=target;
        if (!n)
            nr[aux++]='0';
        for (; n; n/=10)
            nr[aux++]=n%10+'0';
        for(; aux; inc())
            buffer[index]=nr[--aux];
        return *this;
    }
    inline writer &operator <<(const char *target)
    {
        aux=0;
        while (target[aux])
            buffer[index]=target[aux++],inc();
        return *this;
    }
    ~writer()
    {
        output_file.write(buffer,index);
        output_file.close();
    }
private:
    fstream output_file;
    static const int SIZE=0x200000;
    int index=0,aux,n;
    char buffer[SIZE],nr[24];
    inline void inc()
    {
        if(++index==SIZE)
        {
            index=0,output_file.write(buffer,SIZE);
            memset(buffer,0,sizeof(buffer));
        }
    }
};

parser f("2sat.in");
writer t("2sat.out");

vector <int> v[NM<<1];
int n,m,val[NM<<1],E1[NM<<1],E2[NM<<1],viz[NM<<1],top[NM<<1];

#define viz (viz+NM)
#define val (val+NM)
#define v (v+NM)

void dfs(int nod)
{
    viz[nod]=true;
    for(auto i:v[nod])
        if(!viz[i])
            dfs(i);
    top[++top[0]]=nod;
}

int main ()
{
    f>>n>>m;
    for (int x,y,i=1; i<=m; ++i)
        f>>x>>y,
        E1[i]=x,
        E2[i]=y,
        v[x].push_back(-y),
        v[y].push_back(-x);
    for (int i=1; i<=n; ++i)
        if(!viz[i])
            dfs(i);
    for (int i=1; i<=(n<<1); ++i)
        if(!val[top[i]] and !val[-top[i]])
            val[-top[i]]=1;
    for (int x,y,i=1; i<=m; ++i)
    {
        x=E1[i];
        y=E2[i];
        if((val[-x] and !val[y]) or (val[-y] and !val[x]))
        {
            t<<"-1";
            return 0;
        }
    }
    for (int i=1; i<=n; ++i)
        t<<val[i]<<" ";
    return 0;
}