CF724G. Xor-matic Number of the Graph

给定一张图求$u\rightarrow v$所有路径的异或的和(相同的异或当一个)。

一条路径由一条链和几个环组成。环可以走也可以不走,就相当于线性基中取几个数。那么对于一条路径就是取$u-v$的一条链和他的环。见 P4151[WC2011]最大XOR和路径

显然不可能所有路径都遍厉过去。考虑一块连通图,那么线性基是共享的。从位考虑贡献

  • 线性基不存在$i$位,需要链上这位有$1$选中,$num1\times2^{i}\times2^{size}$
  • 线性基$i$位为$1$,需要链上这位有$0$选中,或者线性基这位不选$num0\times2^{i}\times2^{size-1}+num1\times2^{i}\times2^{size-1}$

考虑随机从一个点出发$dis[u\rightarrow v]=dis[u]\bigoplus dis[v]$,$num1=sum1\times sum2,num2=C(sum1,2)+C(sum2,2)$

复杂度$O(n+64^2+64n)$

代码
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
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
const int M = 63;
int qpow(int a, int x, int mo)
{
int res = 1;
while (x)
{
if (x & 1)
res = 1ll * res * a % mod;
a = 1ll * a * a % mod;
x >>= 1;
}
return res;
}
class LinearBase
{
public:
ll p[M];
void clear()
{
memset(p, 0, sizeof(p));
}

void insert(ll x)
{

for (int i = M - 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 = M - 1; i >= 0; i--)
res = max(res, res ^ p[i]);
return res;
}

ll query_min()
{
for (int i = 0; i < M; i++)
if (p[i])
return p[i];
return 0;
}
void rebuild()
{
for (int i = M - 1; i >= 0; i--)
for (int j = i - 1; j >= 0; j--)
if ((p[i] >> j) & 1)
p[i] ^= p[j];
}
ll query_kth(ll k)
{
return 0;
}
} lb;
struct edge
{
int to;
ll w;
};
vector<edge> g[N];
vector<int> s;
int n, m;
int vis[N];
ll val[N];
void dfs(int x, ll w)
{
val[x] = w;
vis[x] = 1;
s.push_back(x);
for (edge now : g[x])
{
int to = now.to;
if (!vis[to])
dfs(to, w ^ now.w);
else
lb.insert(val[to] ^ w ^ now.w);
}
}
int pow2[N];
ll num[M][2];
ll calc()
{
for (int i = 0; i < M; i++)
num[i][0] = num[i][1] = 0;
for (int x : s)
{
//cout<<val[x]<<endl;
for (int i = 0; i < M; i++)
{
if (val[x] & (1ll << i))
num[i][1]++;
else
{
num[i][0]++;
}
}
}
int siz = 0;
for (int i = 0; i < M; i++)
if (lb.p[i])
siz++;
int res = 0;
//cout<<num[0][1]<<endl;
for (int i = 0; i < M; i++)
{
bool flag = 0;
for (int j = 0; j < M;
j++)
{ // 判断线性基元素当前这一位中是否有 1
if ((lb.p[j] >> i) & 1)
{
flag = true;
break;
}
}
int p = (1ll * num[i][0] * (num[i][0] - 1) / 2 + 1ll * num[i][1] * (num[i][1] - 1) / 2) % mod;
if (flag)
{

res += 1ll * p * (siz == 0 ? 1 : pow2[siz - 1]) % mod * pow2[i] % mod;
res %= mod;
}
p = (1ll * num[i][0] * num[i][1]) % mod;
if (flag)
{
res += 1ll * p * (siz == 0 ? 1 : pow2[siz - 1]) % mod * pow2[i] % mod;
res %= mod;
}
else
{
res += 1ll * p * pow2[siz] % mod * pow2[i] % mod;
res %= mod;
}
}
return res;
}
ll ans;
int main()
{

scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++)
{
int u, v;
ll w;
scanf("%d%d%lld", &u, &v, &w);
g[u].push_back((edge){v, w});
g[v].push_back((edge){u, w});
}
pow2[0] = 1;
for (int i = 1; i < N; i++)
pow2[i] = 1ll * pow2[i - 1] * 2 % mod;
for (int i = 1; i <= n; i++)
if (!vis[i])
{
s.clear();
lb.clear();

dfs(i, 0);
ans = (ans + calc()) % mod;
}
printf("%lld\n", ans);
}