royalsflush

Ministers' Major Mess AC - Live Archive 4452

Sep 9th, 2012
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <algorithm>
  3. #include <string.h>
  4. #include <stack>
  5. #include <vector>
  6. using namespace std;
  7.  
  8. const int MAXV = 210;
  9. const int MAXE = 40010;
  10.  
  11. int n;
  12. vector<int> adj[MAXV];
  13. int num[MAXV], low[MAXV];
  14. stack<int> s; bool inSt[MAXV];
  15. int sccCnt, vCnt;
  16. int scc[MAXV];
  17.  
  18. void dfsSCC(int u) {
  19.     num[u]=low[u]=vCnt++;
  20.     s.push(u); inSt[u]=true;
  21.  
  22.     for (int i=0; i<(int)adj[u].size(); i++) {
  23.         int v=adj[u][i];
  24.  
  25.         if (num[v]==-1) {
  26.             dfsSCC(v);
  27.             low[u]=min(low[v],low[u]);
  28.         }
  29.         else
  30.             if (inSt[v])
  31.                 low[u]=min(low[u],num[v]);
  32.     }
  33.  
  34.     if (low[u]==num[u]) {
  35.         int v=-1;
  36.  
  37.         do {
  38.             v=s.top();
  39.             s.pop();
  40.             inSt[v]=false;
  41.             scc[v]=sccCnt;
  42.         } while (u!=v);
  43.    
  44.         sccCnt++;
  45.     }
  46. }
  47.  
  48. void tarjanSCC() {
  49.     memset(num,-1,sizeof(num));
  50.     memset(low,-1,sizeof(low));
  51.     memset(inSt,0,sizeof(inSt));
  52.     memset(scc,-1,sizeof(scc));
  53.     sccCnt=0, vCnt=0;
  54.  
  55.     while (!s.empty())
  56.         s.pop();
  57.  
  58.     for (int i=0; i<n; i++)
  59.         if (num[i]==-1)
  60.             dfsSCC(i);
  61. }
  62.  
  63. int B,M;
  64.  
  65. inline int neg(int v) {
  66.     if (v<B) return v+B;
  67.     return v-B;
  68. }
  69.  
  70. bool twosat() {
  71.     tarjanSCC();
  72.  
  73.     for (int i=0; i<B; i++)
  74.         if (scc[i]==scc[neg(i)])
  75.             return false;
  76.     return true;
  77. }
  78.  
  79. void getVotes() {
  80.     int k, vot[4];
  81.     scanf("%d", &k);
  82.  
  83.     for (int i=0; i<k; i++) {
  84.         char tmp;
  85.         scanf("%d %c", &vot[i],&tmp);
  86.         vot[i]--;
  87.         if (tmp=='n') vot[i]=neg(vot[i]);
  88.     }
  89.  
  90.     switch(k) {
  91.         case 2:
  92.         case 1:
  93.             for (int i=0; i<k; i++)
  94.                 for (int j=0; j<k; j++)
  95.                     adj[neg(vot[i])].push_back(vot[j]);
  96.             break;
  97.         case 3:
  98.         case 4:
  99.             for (int i=0; i<k; i++)
  100.                 for (int j=0; j<k; j++)
  101.                     if (i!=j)
  102.                         adj[neg(vot[i])].push_back(vot[j]);
  103.             break;
  104.     }
  105. }
  106.  
  107. int _42=1;
  108.  
  109. int main() {
  110.     while (1) {
  111.         scanf("%d %d", &B,&M);
  112.         if (!B && !M) break;
  113.        
  114.         n=2*B;
  115.         for (int i=0; i<n; i++)
  116.             adj[i].clear();
  117.  
  118.         for (int i=0; i<M; i++)
  119.             getVotes();
  120.  
  121.         printf("Case %d: ", _42++);
  122.  
  123.         if (!twosat()) {
  124.             printf("impossible\n");
  125.             continue;
  126.         }
  127.  
  128.         for (int i=0; i<B; i++) {
  129.             bool t,f;
  130.             adj[neg(i)].push_back(i);
  131.             t=twosat();
  132.             adj[neg(i)].pop_back();
  133.             adj[i].push_back(neg(i));
  134.             f=twosat();
  135.             adj[i].pop_back();
  136.            
  137.             if (t&&f) printf("?");
  138.             else
  139.                 if (t) printf("y");
  140.                 else printf("n");
  141.         }
  142.  
  143.         printf("\n");
  144.     }
  145.     return 0;
  146. }
Add Comment
Please, Sign In to add comment