P5169 xtq的异或和

求图上有多少存在多少$(u,v)=x$

考虑树状结构直接$(u,v)$直接走,不会出现多出来的路径造成影响。直接$FWT$所有结点的深度异或。

由于加了个图,只要考虑环的影响,环当作线性基可以取或不取,再做一遍$FWT$即可。

代码
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mk make_pair
const int N = 1 << 18;
const int mod = 1e9 + 7;
int read()
{
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + c - '0', c = getchar();
return x * f;
}
const int MAXL = 20;
struct LinearBase
{
ll p[MAXL];

void clear()
{
memset(p, 0, sizeof(p));
}
void insert(ll x)
{
for (int i = MAXL - 1; i >= 0; i--)
{
if (x & (1ll << i))
{
if (!p[i])
{
p[i] = x;
break;
}
x ^= p[i];
}
}
}
ll query_max(ll x = 0)
{
ll res = x;
for (int i = MAXL - 1; i >= 0; i--)
res = max(res, res ^ p[i]);
return res;
}

ll query_min()
{
for (int i = 0; i < MAXL; i++)
if (p[i])
return p[i];
return 0;
}
void rebuild()
{
for (int i = MAXL - 1; i >= 0; i--)
for (int j = i - 1; j >= 0; j--)
if ((p[i] >> j) & 1)
p[i] ^= p[j];
}
void mergeFrom(const LinearBase &other)
{
for (int i = 0; i <= MAXL; i++)
insert(other.p[i]);
}
ll query_kth(ll k, int n)
{
rebuild();
vector<ll>
pp;
for (int i = 0; i < MAXL; ++i)
if (p[i])
pp.push_back(p[i]);
if (pp.size() != n)
k--;
if (k > (1LL << pp.size()) - 1)
return -1;
ll ans = 0;
for (int i = 0; i < pp.size(); ++i)
if (k & (1LL << i))
{
ans ^= pp[i];
}
return ans;
}
} lb;
int qpow(int a, int x, int mo)
{
int res = 1;
while (x)
{
if (x & 1)
res = 1ll * res * a % mo;
x >>= 1;
a = 1ll * a * a % mo;
}
return res;
}
int inc(int x, int y, int mo)
{
if (y < 0)
y += mo;
if (x + y >= mo)
x -= mo;
return x + y;
}
int Inv2;
void FWT(int *A, int n, int op, int t) //t=1 or t=2 and t=3 xor
{
for (int i = 2; i <= n; i <<= 1)
{
for (int j = 0, mid = i >> 1; j < n; j += i)
for (int k = 0; k < mid; k++)
{

if (t == 1)
A[j + mid + k] = inc(A[j + mid + k], A[j + k] * op, mod);
else if (t == 2)
A[j + k] = inc(A[j + k], A[j + mid + k] * op, mod);
else if (t == 3)
{
int x = A[j + k], y = A[j + mid + k];
if (op == 1)
A[j + k] = (x + y) % mod, A[j + mid + k] = (x - y + mod) % mod;
else
A[j + k] = 1ll * Inv2 * (x + y) % mod, A[j + mid + k] = 1ll * Inv2 * (x - y + mod) % mod;
}
}
}
}
void FWTX(int *A, int *B, int n, int t)
{
Inv2 = qpow(2, mod - 2, mod);
FWT(A, n, 1, t), FWT(B, n, 1, t);

for (int i = 0; i < n; ++i)
A[i] = 1ll * A[i] * B[i] % mod;
FWT(A, n, -1, t);
}
vector<pii> g[N];
int dep[N], vis[N];
int a[N], b[N], c[N];
void work(int x, int d)
{
if (x == MAXL)
{
c[d] = 1;
return;
}
if (lb.p[x])
work(x + 1, d ^ lb.p[x]);
work(x + 1, d);
}
void dfs(int x, int d)
{
dep[x] = d;
vis[x] = 1;
for (pii now : g[x])
{
int to = now.first;
if (vis[to])
{
lb.insert(dep[to] ^ dep[x] ^ now.second);
}
else
{
dfs(to, d ^ now.second);
}
}
}

int main()
{
int n = read(), m = read(), q = read();
for (int i = 1; i <= m; i++)
{
int u = read(), v = read(), w = read();
g[u].push_back(mk(v, w));
g[v].push_back(mk(u, w));
}
dfs(1, 0);
for (int i = 1; i <= n; i++)
{
a[dep[i]]++;
b[dep[i]]++;
}
work(0, 0);
FWTX(a, b, 1 << 18, 3);
FWTX(a, c, 1 << 18, 3);
while (q--)
{
int x = read();
printf("%d\n", a[x]);
}
}