Pagini recente » Cod sursa (job #1599668) | Cod sursa (job #1115641) | Cod sursa (job #2975419) | Cod sursa (job #2878004) | Cod sursa (job #1114164)
#include <fstream>
#include <ctime>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define exp pair<int, int>
ifstream fin ("2sat.in");
ofstream fout ("2sat.out");
const int NMAX = 100009;
const int TRY = 20;
exp V[2 * NMAX]; int C[NMAX];
int N; int M;
int abs(int X){
if(X < 0) return -X;
return X;
}
bool compute(int ind) {
int a = C[abs(V[ind].first)];
int b = C[abs(V[ind].second)];
if(V[ind].first < 0) a ^= 1;
if(V[ind].second < 0) b ^= 1;
return (a | b);
}
void print() {
for(int i = 1; i <= N; ++i)
fout << C[i] << " ";
}
int main() {
srand(time(0));
fin >> N >> M;
for(int i = 1; i <= M; ++i){
int a, b; fin >> a >> b;
V[i] = make_pair(a, b);
}
for(int i = 1; i <= N; ++i)
C[i] = rand() % 2;
for(int j = 1; j <= TRY * N; ++j) {
int problem = 0;
for(int i = 1; i <= M; ++i)
if(!compute(i)) {
problem = i;
break;
}
if(!problem){ print(); return 0;}
if(rand() % 2 == 0)
C[abs(V[problem].first)] ^= 1;
else
C[abs(V[problem].second)] ^= 1;
}
fout << -1 << '\n';
return 0;
}