hdu3234 Exclusive-OR

分析

带权并查集

添加一个n点,使其值为0,这样任何值异或它都是本身。

然后注意查询的时候,先除去根节点是n的。 然后对于有相同根节点的节点,如果它的数量是奇数那么一定无法确定,偶数则可以确定。

代码

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
// ybmj
#include <bits/stdc++.h>
using namespace std;
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define lson_len (len - (len >> 1))
#define rson_len (len >> 1)
#define pb(x) push_back(x)
#define clr(a, x) memset(a, x, sizeof(a))
#define mp(x, y) make_pair(x, y)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const int maxn = 2e4 + 5;
int par[maxn], val[maxn];
int a[30];
bool flag;
int n;

inline int find(int x) {
if (x == par[x]) return x;
int fa = par[x];
par[x] = find(par[x]);
val[x] ^= val[fa];
return par[x];
}
inline void merge(int u, int v, int x, int i) {
int fu = find(u);
int fv = find(v);
if (fu == fv) {
if ((val[u] ^ val[v]) != x) {
// cout << val[u] << ' ' << val[v] << ' ' << (val[u] ^ val[v]) << endl;
// cout << u << ' ' << v << ' ' << x << endl;
flag = true;
cout << "The first " << i << " facts are conflicting." << endl;
}
} else {
if (fu == n) swap(fu, fv);
val[fu] = val[u] ^ val[v] ^ x;
par[fu] = fv;
}
}
void query(int m) {
vector<int> fas;
vector<int> sons[30];
int ret = 0;
for (int i = 0; i < m; i++) {
int fa = find(a[i]);
if (fa == n)
ret ^= val[a[i]];
else {
int k = 0;
for (k = 0; k < fas.size(); k++) {
if (fas[k] == fa) {
break;
}
}
if (k == fas.size()) fas.pb(fa);
sons[k].pb(a[i]);
}
}
for (int i = 0; i < fas.size(); i++) {
if (sons[i].size() & 1) {
cout << "I don't know." << endl;
return;
}
for (int k = 0; k < sons[i].size(); k++) {
ret ^= val[sons[i][k]];
}
}
cout << ret << endl;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
std::ios::sync_with_stdio(false);
int kase = 1;
int Q;
while (cin >> n >> Q) {
flag = false;
if (!n && !Q) break;
cout << "Case " << kase++ << ":" << endl;
for (int i = 0; i <= n; i++) par[i] = i, val[i] = 0;
int idx = 0;
string line;
char op;
for (int i = 0; i < Q; i++) {
cin >> line;
if (line == "I") {
idx++;
int k = 0;
while (cin >> a[k++]) {
if (cin.get() == '\n') {
break;
}
}
if(flag) continue;
if (k == 2)
merge(a[0], n, a[1], idx);
else
merge(a[0], a[1], a[2], idx);
}
else{
int k ;
cin >> k;
for(int i=0;i<k;i++) cin >> a[i];
if(flag) continue;
query(k);
}
}
cout << endl;
}
}