Pagini recente » Cod sursa (job #1215767) | Cod sursa (job #1238746) | Cod sursa (job #1379351) | Cod sursa (job #1396986) | Cod sursa (job #3304969)
#include <unordered_map>
#include <fstream>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
const string txt = "2sat";
const int nmax = 2e5 + 5;
ifstream f(txt + ".in");
ofstream g(txt + ".out");
int n, m, k, c[nmax], fr[nmax];
vector<int> v[5][nmax], aux;
static void dfs(int nod, int tip)
{
fr[nod] = tip + 1;
for (auto x : v[tip][nod])
if (fr[x] != tip + 1)
dfs(x, tip);
if (!tip) aux.push_back(nod);
else c[nod] = k;
}
int main()
{
f >> n >> m;
for (int i = 1; i <= m; i++)
{
int x, y; f >> x >> y;
v[0][-x + n].push_back(y + n);
v[0][-y + n].push_back(x + n);
v[1][x + n].push_back(-y + n);
v[1][y + n].push_back(-x + n);
}
for(int i = 1; i <= 2 * n; i ++)
if(!fr[i])
dfs(i, 0);
for (int i = aux.size() - 1; i >= 0; i--)
if (fr[aux[i]] != 2)
k++, dfs(aux[i], 1);
int oki = 1;
for (int i = 1; i <= n; i++)
if (c[i + n] == c[-i + n]) oki = 0;
if (!oki) {
g << -1;
return 0;
}
for (int i = 1; i <= n; i++)
g << (c[i + n] > c[-i + n]) << " ";
return 0;
}