Pagini recente » Cod sursa (job #1828455) | Cod sursa (job #745007) | Cod sursa (job #2835715) | Cod sursa (job #1835100) | Cod sursa (job #2510015)
#include<bits/stdc++.h>
using namespace std;
ifstream fin("2sat.in");
ofstream fout("2sat.out");
const int DIM = 2e5 + 5;
int N, M, cod, niv[DIM], low[DIM], f[DIM], ans[DIM];
bool ok = true;
stack<int> st;
vector<int> edge[DIM];
set<int> S;
inline int convert( int x ){
if( x < 0 )
return -x;
return x + N;
}
inline int inv( int a ){
if( a <= N )
return a + N;
return a - N;
}
void dfs( int nod ){
st.push( nod );
f[nod] = 1;
niv[nod] = low[nod] = ++cod;
for( int i = 0; i < edge[nod].size(); i++ ){
int v = edge[nod][i];
if( niv[v] == 0 ){
dfs( v );
low[nod] = min( low[nod], low[v] );
}else{
if( f[v] == 1 )
low[nod] = min( low[nod], low[v] );
}
}
if( low[nod] == niv[nod] ){
S.clear();
while( st.top() != nod ){
if( S.find( inv(st.top()) ) != S.end() )
ok = false;
if( ans[ st.top() ] == -1 ){
ans[ st.top() ] = 1;
ans[ inv( st.top() ) ] = 0;
}
f[ st.top() ] = 0;
st.pop();
}
if( S.find( inv(st.top()) ) != S.end() )
ok = false;
if( ans[ st.top() ] == -1 ){
ans[ st.top() ] = 1;
ans[ inv( st.top() ) ] = 0;
}
f[ st.top() ] = 0;
st.pop();
}
}
int main(){
fin >> N >> M;
for( int i = 1; i <= M; i++ ){
int a, b; fin >> a >> b;
a = convert( a );
b = convert( b );
edge[inv(a)].push_back( b );
edge[inv(b)].push_back( a );
}
memset( ans, -1, sizeof(ans) );
for( int i = 1; i <= N; i++ ){
if( niv[i] == 0 )
dfs( i );
}
if( ok == false )
fout << "-1\n";
else{
for( int i = N + 1; i <= N + N; i++ )
fout << ans[i] << " ";
}
return 0;
}