1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
| // // main.cpp // HDU3234 // // Created by zhangmin chen on 2019/7/10. // Copyright © 2019 zhangmin chen. All rights reserved. //
using namespace std; typedef long long llong; typedef set<int>::iterator ssii;
const int maxn = 20000 + 10; const int maxl = 40000 + 10;
int n, q; int pa[maxn], d[maxn]; int vis[maxn]; int line[maxl][3]; int data[maxn];
void initSet() { Set(line, 0); Set(data, 0); _for(i, 0, maxn) { pa[i] = i; d[i] = 0; } }
int findSet(int x) { if(x == pa[x]) return x; else { int root = findSet(pa[x]); d[x] ^= d[pa[x]]; return pa[x] = root; } }
bool combine(int p, int q, int v) { int rootp = findSet(p); int rootq = findSet(q); if(rootp == rootq) return (d[p] ^ d[q]) == v; //debug(nil); // union rootp and rootq, d[p] ^ d[q] = v // pa[rootp] = rootq if(rootp == n) swap(rootp, rootq); pa[rootp] = rootq; d[rootp] = (d[p] ^ d[q] ^ v); return true; }
int query(const int *data, int k) { int ans = 0; Set(vis, 0); _for(i, 0, k) { // d[data[i]] if(vis[i]) continue; int cnt = 0, root = findSet(data[i]); _for(j, i, k) { if(!vis[j] && root == findSet(data[j])) { vis[j] = 1; cnt++; ans ^= d[data[j]]; } } if(root != n && cnt % 2 == 1) return -1; } return ans; }
int main() { freopen("input.txt", "r", stdin); int cnt = 0; while (scanf("%d%d", &n, &q) && (n + q)) { initSet(); printf("Case %d:\n", ++cnt); //debug(n); int ic = 0; bool bug = 0; _for(i, 0, q) { string str; cin >> str; if(str[0] == 'I') { // ic++; int sz = 0; while(cin >> line[i][sz++]) if(getchar() == '\n') break; if(bug) continue; int p = 0, q = 0, v = 0; if(sz == 2) { // I p v, data[0] = p, data[1] = v p = line[i][0]; v = line[i][1]; q = n; } else if(sz == 3) { // I p q v p = line[i][0]; q = line[i][1]; v = line[i][2]; } if(!combine(p, q, v)) { printf("The first %d facts are conflicting.\n", ic); bug = 1; } } else if(str[0] == 'Q') { // k p1 p2 ... pk int k; scanf("%d", &k); // _for(i, 0, data.size()) cout << data[i] << " "; // cout << endl; _for(i, 0, k) scanf("%d", &data[i]); if(bug) continue; int res = query(data, k); if(res == -1) printf("I don't know.\n"); else printf("%d\n", res); } // _for(i, 0, data.size()) cout << data[i] << " "; // cout << endl; } puts(""); } }
|