Pagini recente » Cod sursa (job #2774911) | Cod sursa (job #2049018) | Cod sursa (job #1152910) | Cod sursa (job #568958) | Cod sursa (job #780817)
Cod sursa(job #780817)
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int kMaxN = 200005;
vector<int> G[kMaxN], GT[kMaxN];
int N, V[kMaxN], TSort[kMaxN], Sol[kMaxN];
inline int Non(const int X) {
return X <= N ? X+N : X-N;
}
void DFP(const int X) {
++V[X];
for (vector<int>::iterator Y = G[X].begin(); Y != G[X].end(); ++Y)
if (!V[*Y])
DFP(*Y);
TSort[++TSort[0]] = X;
}
void DFM(const int X) {
if (Sol[X]) {
Sol[0] = -1;
return;
}
Sol[Non(X)] = 1;
--V[X];
for (vector<int>::iterator Y = GT[X].begin(); Y != GT[X].end(); ++Y)
if (V[*Y])
DFM(*Y);
}
void Kosaraju() {
for (int X = 1; X <= 2*N; ++X)
if (!V[X])
DFP(X);
reverse(TSort+1, TSort+2*N+1);
for (int i = 1; i <= 2*N; ++i) {
int X = TSort[i];
if (V[X] && V[Non(X)])
DFM(X);
}
}
inline void Imply(const int X, const int Y) {
G[X].push_back(Y);
GT[Y].push_back(X);
}
void Read () {
freopen("2sat.in", "r", stdin);
int M; scanf("%d %d", &N, &M);
for (; M; --M) {
int X, Y; scanf("%d %d", &X, &Y);
X = (X < 0 ? N-X : X);
Y = (Y < 0 ? N-Y : Y);
Imply(Non(X), Y), Imply(Non(Y), X);
}
}
void Print() {
freopen("2sat.out", "w", stdout);
if (Sol[0] == -1) {
printf ("-1\n");
return;
}
for (int X = 1; X <= N; ++X)
printf("%d ", Sol[X]);
printf("\n");
}
int main() {
Read();
Kosaraju();
Print();
return 0;
}