Pagini recente » Cod sursa (job #1594656) | Cod sursa (job #2894446) | Cod sursa (job #657898) | Cod sursa (job #402646) | Cod sursa (job #2730153)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <stack>
#include <cstdlib>
using namespace std;
ifstream in("2sat.in");
ofstream out("2sat.out");
const int N=100005,M=200005;
vector<int> aux[2*N],aux_t[2*N];
vector<int> *a,*a_t;
stack<int> v;
bool aux_vazut[2*N],aux_vazut_t[2*N],aux_val[2*N+1], aux_completat[2*N+1];
bool *vazut,*vazut_t,*val,*completat;
void dfs(int x)
{
vazut[x]=true;
for (auto y:a[x])
{
if (vazut[y]!=true)
dfs(y);
}
v.push(x);
}
void dfs_t(int x)
{
vazut_t[x] = true;
if (!completat[x])
{
completat[x] = true;
val[x] = false;
completat[-x] = true;
val[-x] = true;
}
else
{
if (val[x])
{
out << -1;
out.close();
exit(0);
}
}
for (auto y:a_t[x])
{
if (vazut_t[y]!=true)
dfs_t(y);
}
}
int main()
{
int n,m;
in>>n>>m;
int x,y;
a = aux + N;
a_t = aux_t + N;
vazut = aux_vazut + N;
vazut_t = aux_vazut_t + N;
val = aux_val + N;
completat = aux_completat + N;
for (int i=1;i<=m;i++)
{
in>>x>>y;
a[-x].push_back(y);
a[-y].push_back(x);
a_t[x].push_back(-y);
a_t[y].push_back(-x);
}
for (int i=-n;i<=n;i++)
{
if (vazut[i]!=true)
dfs(i);
}
while (!v.empty())
{
int x=v.top();
v.pop();
if (completat[x])
{
continue;
}
if (!vazut_t[x])
{
dfs_t(x);
}
}
for (int i=1;i<=n;i++)
{
out<<val[i]<<" ";
}
in.close();
out.close();
return 0;
}