Advertisement
palmerstone

Untitled

Jan 18th, 2012
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <algorithm>
  4. #include <cstdio>
  5. #include <cmath>
  6. #include <cctype>
  7. #include <cstdlib>
  8. #include <cstring>
  9. #include <numeric>
  10. #include <sstream>
  11. #include <fstream>
  12. #include <climits>
  13. #include <cassert>
  14. #include <cerrno>
  15. #include <climits>
  16. #include <limits>
  17. #include <string>
  18. #include <map>
  19. #include <set>
  20. #include <vector>
  21. #include <queue>
  22. #include <stack>
  23. #include <deque>
  24. #include <numeric>
  25. #include <utility>
  26. #include <bitset>
  27.  
  28. using namespace std;
  29.  
  30. int t, n, m, k, mor, temp[100], bulbs[1000], dis[66000], adj[1000];
  31. std::bitset <66000> toggle;
  32. std::queue <int> q;
  33. char str[1000];
  34.  
  35. int toBinary(){
  36. int l = 0, z = 0;
  37. for (int k = n - 1; k >= 0; k--){
  38. int y = temp[k];
  39. int x = y * (1 << l);
  40. z += x;
  41. l++;
  42. }
  43. return z;
  44. }
  45.  
  46. int FU(){
  47. int l = 0, z = 0;
  48. for (int k = 0; k < n; k++){
  49. int y = str[k] - 48;
  50. int x = y * (1 << l);
  51. z += x;
  52. l++;
  53. }
  54. return z;
  55. }
  56.  
  57. int adjacentTo(int i){
  58. int l = 0;
  59. for (int j = 0; j < m; j++){
  60. int x = bulbs[j];
  61. int y = i ^ x;
  62. //printf("%d %d\n", i, x);
  63. if (!toggle[y]){
  64. adj[l++] = y;
  65. }
  66. }
  67. return l;
  68. }
  69.  
  70. int main(){
  71. //freopen("input.txt", "r", stdin);
  72.  
  73. scanf("%d", &t);
  74. for (int line = 1; line <= t; line++){
  75. scanf("%d %d", &n, &m);
  76.  
  77. int max = 1 << n;
  78. for (int i = 0; i <= max; i++){
  79. dis[i] = INT_MAX;
  80. toggle[i] = false;
  81. }
  82.  
  83. for (int p = 0; p < m; p++){
  84. scanf("%d", &k);
  85. for (int i = 0; i < n; i++){
  86. temp[i] = 0;
  87. }
  88. for (int i = 0; i < k; i++){
  89. int x;
  90. scanf("%d", &x);
  91. temp[x] = 1;
  92. }
  93. bulbs[p] = toBinary();
  94. }
  95.  
  96. while (!q.empty()){
  97. q.pop();
  98. }
  99.  
  100. int source = 0;
  101. q.push(source);
  102. toggle[source] = true, dis[source] = 0;
  103.  
  104. while (!q.empty()){
  105. int i = q.front();
  106. q.pop();
  107. int d = adjacentTo(i);
  108. for (int k = 0; k < d; k++){
  109. int j = adj[k];
  110. dis[j] = dis[i] + 1;
  111. toggle[j] = true;
  112. q.push(j);
  113. }
  114. }
  115.  
  116. scanf("%d", &mor);
  117. getchar();
  118. printf("Case %d:\n", line);
  119. for (int r = 0; r < mor; r++){
  120. gets(str);
  121.  
  122. int source = 0;
  123. int destination = FU();
  124. if (toggle[destination]) printf("%d\n", dis[destination]);
  125. else puts("-1");
  126. }
  127. putchar(10);
  128. }
  129. return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement