Pagini recente » Cod sursa (job #1067793) | Cod sursa (job #2872222) | Cod sursa (job #2602532) | Cod sursa (job #2946199) | Cod sursa (job #3236717)
#include <bits/stdc++.h>
using namespace std;
#define INFILE "2sat.in"
#define OUTFILE "2sat.out"
const int N_MAX = 2e5 + 2;
int n, m, ctc;
bool visited[N_MAX];
vector<int> adj[N_MAX], revAdj[N_MAX], topoSort, comp;
int transform(int value){
return (value < 0 ? -value + n : value);
}
int negated(int value){
return (value <= n ? value + n : value - n);
}
void dfs(int node){
visited[node] = true;
for(int i = 0; i < adj[node].size(); ++i){
int to = adj[node][i];
if(!visited[to]){
dfs(to);
}
}
topoSort.push_back(node);
}
void revDfs(int node){
comp[node] = ctc;
for(int i = 0; i < revAdj[node].size(); ++i){
int to = revAdj[node][i];
if(!comp[to]){
revDfs(to);
}
}
}
void solve(){
cin >> n >> m;
comp.resize(2 * (n + 1));
for(int i = 0; i < m; ++i){
int x, y; cin >> x >> y;
x = transform(x);
y = transform(y);
adj[negated(x)].push_back(y);
adj[negated(y)].push_back(x);
revAdj[x].push_back(negated(y));
revAdj[y].push_back(negated(x));
}
for(int i = 1; i <= 2 * n; ++i)
if(!visited[i]) dfs(i);
reverse(topoSort.begin(), topoSort.end());
for(auto &it : topoSort){
if(comp[it]) continue;
++ctc;
revDfs(it);
}
bool ok = true;
for(int i = 1; i <= n && ok; ++i){
if(comp[i] == comp[i + n]) ok = false;
}
if(!ok) cout << -1 << '\n';
else {
for(int i = 1; i <= n; ++i){
cout << (comp[i] > comp[n + i] ? true : false) << " ";
}
cout << '\n';
}
}
int main(){
ios_base::sync_with_stdio(false);
freopen(INFILE, "r", stdin);
freopen(OUTFILE, "w", stdout);
cin.tie(0), cout.tie(0);
solve();
return 0;
}