Pagini recente » Cod sursa (job #2821223) | Cod sursa (job #1670792) | Cod sursa (job #1919379) | Cod sursa (job #1100182) | Cod sursa (job #2593160)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("2sat.in");
ofstream fout("2sat.out");
int off;
inline int no(int x) {
if (x > off)
return x - off;
return x + off;
}
class Graph {
private:
int n;
vector<vector<int>> ad;
vector<pair<int, int>> edg;
void dfs(int node, vector<bool>& vis, vector<int>& topo) {
vis[node] = true;
for (int nghb : ad[node])
if (!vis[nghb])
dfs(nghb, vis, topo);
topo.push_back(node);
}
public:
Graph(int n) :
n(n), ad(n + 1) { }
void addEdge(int x, int y) {
ad[x].push_back(y);
edg.emplace_back(x, y);
}
vector<int> topoSort() {
vector<int> topo;
vector<bool> vis(n + 1);
for (int i = 1; i <= n; i++)
if (!vis[i])
dfs(i, vis, topo);
reverse(topo.begin(), topo.end());
return topo;
}
vector<bool> solve() {
vector<bool> val(n + 1);
auto topo = topoSort();
for (int node : topo)
if (!val[node] && !val[no(node)])
val[no(node)] = true;
for (auto e : edg)
if (val[e.first] && !val[e.second])
return vector<bool>();
val.resize(n / 2 + 1);
return val;
}
};
int main() {
int n, m; fin >> n >> m;
off = n;
Graph graph(2 * n);
for (int i = 0; i < m; i++) {
int x, y; fin >> x >> y;
if (x < 0) x = n - x;
if (y < 0) y = n - y;
graph.addEdge(no(x), y);
graph.addEdge(no(y), x);
}
auto ans = graph.solve();
if (ans.empty())
fout << -1;
else
for (int i = 1; i <= n; i++)
fout << ans[i] << ' ';
fout << '\n';
fout.close();
return 0;
}