Advertisement
Nusrat_Ullah

UVa 12135

Mar 15th, 2020
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef vector<int> vi;
  4. typedef vector<vi> vvi;
  5. int main()
  6. {
  7.     int g,h,t,q,w,z,x,s=1,sw[43][20],k1[(1<<15)+3],k2[(1<<15)+3],k3[(1<<15)+3];
  8.     bitset<(1<<15)+3>bt;
  9.     queue<int>q1;
  10.     vvi wq;
  11.     for(g=0;g<=(1<<q);g++)
  12.         k1[g]=k3[g]=1e9;
  13.     scanf("%d",&t);
  14.     while(t--){
  15.         scanf("%d %d",&q,&w);
  16.         wq.resize(1<<q);
  17.         for(g=0;g<w;g++){
  18.             scanf("%d",&sw[g][0]);
  19.             for(h=1;h<=sw[g][0];h++)
  20.                 scanf("%d",&sw[g][h]);
  21.         }
  22.         q1.push(0);
  23.         while(!q1.empty()){
  24.             x=q1.front();
  25.             q1.pop();
  26.             if(k1[x]==t)continue;
  27.             k1[x]=t;
  28.             for(g=0;g<w;g++){
  29.                 z=x;
  30.                 for(h=1;h<=sw[g][0];h++)
  31.                     z^=(1<<sw[g][h]);
  32.                 wq[x].push_back(z);
  33.                 q1.push(z);
  34.             }
  35.         }
  36.         k2[0]=0;
  37.         k3[0]=t;
  38.         q1.push(0);
  39.         while(!q1.empty()){
  40.             x=q1.front();
  41.             q1.pop();
  42.             for(g=0;g<wq[x].size();g++){
  43.                 z=wq[x][g];
  44.                 if(k3[z]!=t){
  45.                     k2[z]=k2[x]+1;
  46.                     k3[z]=t;
  47.                     q1.push(z);
  48.                 }
  49.             }
  50.         }
  51.         printf("Case %d:\n",s++);
  52.         scanf("%d",&z);
  53.         while(z--){
  54.             cin>>bt;
  55.             x=bt.to_ulong();
  56.             if(k3[x]!=t)
  57.                 printf("-1\n");
  58.             else printf("%d\n",k2[x]);
  59.         }
  60.         printf("\n");
  61.         wq.clear();
  62.     }
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement