Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <algorithm>
- #include <cstdio>
- #include <cmath>
- #include <cctype>
- #include <cstdlib>
- #include <cstring>
- #include <numeric>
- #include <sstream>
- #include <fstream>
- #include <climits>
- #include <cassert>
- #include <cerrno>
- #include <climits>
- #include <limits>
- #include <string>
- #include <map>
- #include <set>
- #include <vector>
- #include <queue>
- #include <stack>
- #include <deque>
- #include <numeric>
- #include <utility>
- #include <bitset>
- using namespace std;
- int t, n, m, k, mor, temp[100], bulbs[1000], dis[66000], adj[1000];
- std::bitset <66000> toggle;
- std::queue <int> q;
- char str[1000];
- int toBinary(){
- int l = 0, z = 0;
- for (int k = n - 1; k >= 0; k--){
- int y = temp[k];
- int x = y * (1 << l);
- z += x;
- l++;
- }
- return z;
- }
- int FU(){
- int l = 0, z = 0;
- for (int k = 0; k < n; k++){
- int y = str[k] - 48;
- int x = y * (1 << l);
- z += x;
- l++;
- }
- return z;
- }
- int adjacentTo(int i){
- int l = 0;
- for (int j = 0; j < m; j++){
- int x = bulbs[j];
- int y = i ^ x;
- //printf("%d %d\n", i, x);
- if (!toggle[y]){
- adj[l++] = y;
- }
- }
- return l;
- }
- int main(){
- //freopen("input.txt", "r", stdin);
- scanf("%d", &t);
- for (int line = 1; line <= t; line++){
- scanf("%d %d", &n, &m);
- int max = 1 << n;
- for (int i = 0; i <= max; i++){
- dis[i] = INT_MAX;
- toggle[i] = false;
- }
- for (int p = 0; p < m; p++){
- scanf("%d", &k);
- for (int i = 0; i < n; i++){
- temp[i] = 0;
- }
- for (int i = 0; i < k; i++){
- int x;
- scanf("%d", &x);
- temp[x] = 1;
- }
- bulbs[p] = toBinary();
- }
- while (!q.empty()){
- q.pop();
- }
- int source = 0;
- q.push(source);
- toggle[source] = true, dis[source] = 0;
- while (!q.empty()){
- int i = q.front();
- q.pop();
- int d = adjacentTo(i);
- for (int k = 0; k < d; k++){
- int j = adj[k];
- dis[j] = dis[i] + 1;
- toggle[j] = true;
- q.push(j);
- }
- }
- scanf("%d", &mor);
- getchar();
- printf("Case %d:\n", line);
- for (int r = 0; r < mor; r++){
- gets(str);
- int source = 0;
- int destination = FU();
- if (toggle[destination]) printf("%d\n", dis[destination]);
- else puts("-1");
- }
- putchar(10);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement