Advertisement
anoosykh95

Untitled

Jul 22nd, 2016
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define P(x,y) make_pair(x,y)
  3. using namespace std;
  4. const int MX=(1<<20);
  5. class TwoSAT{
  6.     public :
  7.     int n , timer , cnt;
  8.     vector < vector < int > > v ;
  9.     vector < vector < int > > adj;
  10.     vector < int > low , vi , comp , depth ;
  11.     stack < int > S;
  12.     void Fix(vector < int > &vec , int V){
  13.         vec.resize(n*2);
  14.         fill(vec.begin() , vec.end() , V);
  15.     }
  16.     void init(int N){
  17.         n = N; timer=0; cnt=-1;
  18.         v.clear(); v.resize(n*2); for(int j=0;j<n*2;j++) v[j].clear();
  19.         adj.clear(); adj.resize(n*2); for(int j=0;j<n*2;j++) adj[j].clear();
  20.         Fix(depth , -1);
  21.         Fix(comp , 0);
  22.         Fix(low , 0);
  23.         Fix(vi , 0);
  24.     }
  25.     void imply(int a , int na , int b , int nb){
  26.         a--; b--;
  27.         a*=2; b*=2;
  28.         a+=na; b+=nb;
  29.         v[a].push_back(b);
  30.         v[b^1].push_back(a^1);
  31.     }
  32.     void addor(int a , int na ,  int b , int nb){
  33.         imply(a , na^1 , b , nb);
  34.     }
  35.     void addxor(int a , int na , int b , int nb){
  36.         compadd(a , na , b , nb^1 , a , na^1 , b , nb);
  37.     }
  38.     void compadd(int a , int na , int b , int nb , int c , int nc , int d , int nd){
  39.         addor(a , na , c , nc);
  40.         addor(a , na , d , nd);
  41.         addor(b , nb , c , nc);
  42.         addor(b , nb , d , nd);
  43.     }
  44.     void dfs(int x){
  45.         depth[x] = low[x] = ++timer; vi[x] = 1; S.push(x);
  46.         for(auto nxt : v[x]){
  47.             if(depth[nxt] == -1){
  48.                 dfs(nxt);
  49.                 low[x] = min(low[x] , low[nxt]);
  50.             }
  51.             else if(vi[nxt]) low[x] = min(low[x] , depth[nxt]);
  52.         }
  53.         if(depth[x] != low[x]) return;
  54.         ++cnt;
  55.         while(1){
  56.             int node = S.top(); S.pop();
  57.             comp[node] = cnt; vi[node] = 0;
  58.             if(node == x) break;
  59.         }
  60.     }
  61.     bool Satisfable(){
  62.         for(int j=0;j<n*2;j++)
  63.             if(depth[j] == -1)
  64.                 dfs(j);
  65.         for(int j=0;j<n;j++)
  66.             if(comp[j] == comp[j^1])
  67.                 return false;
  68.         return true;
  69.     }
  70.     void build_graph(){
  71.         fill(vi.begin() , vi.end() , 0);
  72.         for(int x=0;x<n*2;x++)
  73.             for(auto nxt : v[x])
  74.                 if(comp[x] != comp[nxt])
  75.                     adj[comp[x]].push_back(comp[nxt]);
  76.         for(int x=0;x<=cnt;x++)
  77.             for(auto nxt : adj[x])
  78.                 vi[nxt]++;
  79.     }
  80.     void Topo(){
  81.         queue < int > Q;
  82.         for(int j=0;j<=cnt;j++)
  83.             if(vi[j] == 0)
  84.                 Q.push(j);
  85.         fill(depth.begin() , depth.end() , 0);
  86.         timer=0;
  87.         while(!Q.empty()){
  88.             int x = Q.front(); Q.pop();
  89.             depth[x] = ++timer;
  90.             for(auto nxt : adj[x]){
  91.                 vi[nxt] -- ;
  92.                 if(vi[nxt] == 0) Q.push(nxt);
  93.             }
  94.         }
  95.     }
  96.     void solution(vector < int > &sol){
  97.         build_graph();
  98.         Topo();
  99.         sol.resize(n);
  100.         for(int j=0;j<n*2;j+=2){
  101.             if(depth[comp[j]] < depth[comp[j^1]])
  102.                 sol[j/2] = 0;
  103.             else sol[j/2] =1;
  104.         }
  105.     }
  106. }G;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement