Pagini recente » Cod sursa (job #3244258) | Cod sursa (job #3298731) | Rating toderita catalina (blueyes) | Cod sursa (job #1334343) | Cod sursa (job #3297372)
#include <stdio.h>
#include <vector>
#include <algorithm>
const int MAXN = 2e5;
std::vector<int> fwd[MAXN];
std::vector<int> back[MAXN];
bool viz[MAXN];
int ctc[MAXN];
std::vector<int> topo;
void dfs( int u ) {
viz[u] = true;
for( int v : fwd[u] )
if( !viz[v] )
dfs( v );
topo.push_back( u );
}
void paint( int u, int C ) {
ctc[u] = C;
for( int v : back[u] )
if( ctc[v] == -1 )
paint( v, C );
}
int main() {
FILE *fin = fopen( "2sat.in", "r" );
FILE *fout = fopen( "2sat.out", "w" );
int n, m;
fscanf( fin, "%d%d", &n, &m );
for( int i = 0; i < m; i++ ){
int a, b;
fscanf( fin, "%d%d", &a, &b );
if( a < 0 ){
a = (-a - 1) * 2 + 1;
}else
a = (a - 1) * 2;
if( b < 0 ){
b = (-b - 1) * 2 + 1;
}else
b = (b - 1) * 2;
fwd[a ^ 1].push_back( b );
fwd[b ^ 1].push_back( a );
back[a].push_back( b ^ 1 );
back[b].push_back( a ^ 1 );
}
for( int i = 0; i < 2 * n; i++ )
if( !viz[i] )
dfs( i );
std::reverse( topo.begin(), topo.end() );
for( int i = 0; i < 2 * n; i++ )
ctc[i] = -1;
int nctc = 0;
for( int u : topo )
if( ctc[u] == -1 )
paint( u, nctc++ );
std::vector<bool> out;
for( int i = 0; i < n; i++ ){
if( ctc[i * 2] == ctc[i * 2 + 1] ){
fputs( "-1\n", fout );
fclose( fin );
fclose( fout );
return 0;
}
out.push_back( ctc[i * 2] > ctc[i * 2 + 1] );
}
for( bool x : out )
fprintf( fout, "%d ", int(x) );
fputc( '\n', fout );
fclose( fin );
fclose( fout );
return 0;
}