Pagini recente » Cod sursa (job #298962) | Cod sursa (job #2857359) | Cod sursa (job #2374099) | Cod sursa (job #1049781) | Cod sursa (job #1653834)
#include <stdio.h>
#define MAXN 100000
#define MAXM 200000
int nd1[2 * MAXM], nxt1[2 * MAXM], ut1[2 * MAXN], nd2[2 * MAXM], nxt2[2 * MAXM], ut2[2 * MAXN];
char viz[2 * MAXN];
int vn[2 * MAXN], dv;
char val[2 * MAXN];
int n;
inline int anod(int x){
if(x >= n)
return -(x - n + 1);
return x + 1;
}
inline int nod(int x){
if(x < 0)
return -x - 1 + n;
return x - 1;
}
void topo(int x){
viz[x] = 1;
int poz = ut1[x];
while(poz != -1){
if(!viz[nd1[poz]])
topo(nd1[poz]);
poz = nxt1[poz];
}
vn[dv] = x;
dv++;
}
void atrib(int x){
val[x] = 1;
viz[nod(-anod(x))] = 1;
viz[x] = 1;
int poz = ut2[x];
while(poz != -1){
if(!viz[nd2[poz]])
atrib(nd2[poz]);
poz = nxt2[poz];
}
}
int main(){
FILE *in = fopen("2sat.in", "r");
int m, x, y, i, dr = 0;
fscanf(in, "%d%d", &n, &m);
memset(ut1, -1, sizeof ut1);
memset(ut2, -1, sizeof ut2);
for(i = 0; i < m; i++){
fscanf(in, "%d%d", &x, &y);
nd1[dr] = nod(y);
nxt1[dr] = ut1[nod(-x)];
ut1[nod(-x)] = dr;
nd2[dr] = nod(-x);
nxt2[dr] = ut2[nod(y)];
ut2[nod(y)] = dr;
dr++;
nd1[dr] = nod(x);
nxt1[dr] = ut1[nod(-y)];
ut1[nod(-y)] = dr;
nd2[dr] = nod(-y);
nxt2[dr] = ut2[nod(x)];
ut2[nod(x)] = dr;
dr++;
}
fclose(in);
for(i = 0; i < 2 * n; i++){
if(!viz[i]){
topo(i);
}
}
memset(viz, 0, sizeof viz);
for(i = 2 * n - 1; i >= 0; i--){
if(!viz[vn[i]]){
atrib(vn[i]);
}
}
FILE *out = fopen("2sat.out", "w");
for(i = 0; i < n; i++)
fprintf(out, "%d ", !val[i]);
fclose(out);
return 0;
}