Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- typedef vector<int> vi;
- typedef vector<vi> vvi;
- int main()
- {
- 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];
- bitset<(1<<15)+3>bt;
- queue<int>q1;
- vvi wq;
- for(g=0;g<=(1<<q);g++)
- k1[g]=k3[g]=1e9;
- scanf("%d",&t);
- while(t--){
- scanf("%d %d",&q,&w);
- wq.resize(1<<q);
- for(g=0;g<w;g++){
- scanf("%d",&sw[g][0]);
- for(h=1;h<=sw[g][0];h++)
- scanf("%d",&sw[g][h]);
- }
- q1.push(0);
- while(!q1.empty()){
- x=q1.front();
- q1.pop();
- if(k1[x]==t)continue;
- k1[x]=t;
- for(g=0;g<w;g++){
- z=x;
- for(h=1;h<=sw[g][0];h++)
- z^=(1<<sw[g][h]);
- wq[x].push_back(z);
- q1.push(z);
- }
- }
- k2[0]=0;
- k3[0]=t;
- q1.push(0);
- while(!q1.empty()){
- x=q1.front();
- q1.pop();
- for(g=0;g<wq[x].size();g++){
- z=wq[x][g];
- if(k3[z]!=t){
- k2[z]=k2[x]+1;
- k3[z]=t;
- q1.push(z);
- }
- }
- }
- printf("Case %d:\n",s++);
- scanf("%d",&z);
- while(z--){
- cin>>bt;
- x=bt.to_ulong();
- if(k3[x]!=t)
- printf("-1\n");
- else printf("%d\n",k2[x]);
- }
- printf("\n");
- wq.clear();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement