NonWhite

SAT-2

Jul 25th, 2012
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstring>
  5. #include <set>
  6. #include <map>
  7. #include <vector>
  8. #include <algorithm>
  9. #include <queue>
  10. #include <stack>
  11. #include <sstream>
  12. #include <cmath>
  13. #include <cstdlib>
  14. #include <cctype>
  15. #define clr( x , y ) memset( x ,y , sizeof x )
  16. #define FOR(i,A) for(typeof (A).begin() i = (A).begin() ; i != (A).end() ; i++)
  17. #define mp make_pair
  18. #define TAM 2010
  19. #define debug( x ) cout << #x << " = " << x << endl
  20. #define f(i,n) for(int i = 0 ; i < n ; i++)
  21. #define ff(i,a,b) for(int i = a ; i < b ; i++)
  22. #define all( x ) x.begin() , x.end()
  23. #define ral( x ) x.rbegin() , x.rend()
  24.  
  25. using namespace std ;
  26.  
  27. typedef pair<int,int> ii ;
  28. typedef pair<ii,int> pii ;
  29. typedef long long ll ;
  30. typedef long double ld ;
  31.  
  32. int n , m ;
  33. vector<int> g1[ TAM ] , g2[ TAM ] ;
  34. stack<int> st ;
  35. int v[ TAM ] ;
  36. int scc[ TAM ] ;
  37. int cmp ;
  38.  
  39. int neg( int x ){ return (x<<1) + 1 ;}
  40. int pos( int x ){ return (x<<1) ; }
  41.  
  42. void init(){
  43.     n *= 2 ;
  44.     for(int i = 0 ; i < n ; i++) g1[ i ].clear() , g2[ i ].clear() ;
  45.     clr( scc , -1 ) ;
  46.     cmp = 0 ;
  47.     stack<int> aux ;
  48.     st = aux ;
  49. }
  50.  
  51. void dfs1( int x ){
  52.     v[ x ] = true ;
  53.     for(int i = 0 ; i < g1[ x ].size() ; i++)
  54.         if( !v[ g1[ x ][ i ] ] )
  55.             dfs1( g1[ x ][ i ] ) ;
  56.     st.push( x ) ; 
  57. }
  58.  
  59. void dfs2( int x ){
  60.     scc[ x ] = cmp ;
  61.     v[ x ] = true ;
  62.     for(int i = 0 ; i < g2[ x ].size() ; i++)
  63.         if( !v[ g2[ x ][ i ] ] )
  64.             dfs2( g2[ x ][ i ] ) ;
  65. }
  66.  
  67. int main(){
  68.  
  69.     while( scanf("%d%d" , &n , &m ) == 2 ){
  70.         init() ;
  71.         for(int i = 0 ; i < m ; i++){
  72.             int a , b ;
  73.             scanf("%d%d" , &a , &b ) ;
  74.             if( a > 0 ){
  75.                 a-- ;
  76.                 if( b > 0 ){
  77.                     b-- ;
  78.                     g1[ neg( a ) ].push_back( pos( b ) ) , g1[ neg( b ) ].push_back( pos( a ) ) ;
  79.                     g2[ pos( b ) ].push_back( neg( a ) ) , g2[ pos( a ) ].push_back( neg( b ) ) ;
  80.                 }
  81.                 else{
  82.                     b *= -1 ; b-- ;
  83.                     g1[ neg( a ) ].push_back( neg( b ) ) , g1[ pos( b ) ].push_back( pos( a ) ) ;
  84.                     g2[ neg( b ) ].push_back( neg( a ) ) , g2[ pos( a ) ].push_back( pos( b ) ) ;
  85.                 }
  86.             }else{
  87.                 a *= -1 ; a-- ;
  88.                 if( b > 0 ){
  89.                     b-- ;
  90.                     g1[ pos( a ) ].push_back( pos( b ) ) , g1[ neg( b ) ].push_back( neg( a ) ) ;
  91.                     g2[ pos( b ) ].push_back( pos( a ) ) , g2[ neg( a ) ].push_back( neg( b ) ) ;
  92.                 }
  93.                 else{
  94.                     b *= -1 ; b-- ;
  95.                     g1[ pos( a ) ].push_back( neg( b ) ) , g1[ pos( b ) ].push_back( neg( a ) ) ;
  96.                     g2[ neg( b ) ].push_back( pos( a ) ) , g2[ neg( a ) ].push_back( pos( b ) ) ;
  97.                 }
  98.             }
  99.         }
  100.         clr( v , 0 ) ;
  101.         for(int i = 0 ; i < n ; i++) if( !v[ i ] ) dfs1( i ) ;
  102.         clr( v , 0 ) ;
  103.  
  104.         while( !st.empty() ){
  105.             int node = st.top() ; st.pop() ;
  106.             if( scc[ node ] == -1 ) dfs2( node ) , cmp++ ;
  107.         }
  108.  
  109.         bool resp = false ;
  110.         n /= 2 ;
  111.         for(int i = 0 ; i < n ; i++) resp |= ( scc[ pos( i ) ] == scc[ neg( i ) ] ) ;
  112. //      for(int i = 0 ; i < n ; i++) cout << scc[ i ] << ' ' ; cout << endl ;
  113.         cout << !resp << endl ;
  114.  
  115.     }
  116.     return 0 ;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment