ABC451G
upd on 2026.4.1:修改了一些错误。
又来写题解了。
题意
有一个由
一条路径的权值定义为这条路径所有边的异或和。
解法
我会保持极高注意力。
首先考虑两条
那么相当于
那么我们就可以发现,
设环
接着我们又又注意到,我们异或上去的环可以随意选,前提是能够从
线性基!
没错,线性基是个很不错的选择。因为这个东西可以把我们这些能得到的权值全部存下来,并且能够求出上面说的最小值。我们令
然后我们可以证明
字典树!
是的,我们直接将
时间复杂度
不理解可以看代码。
::::success[Code]
#include <bits/stdc++.h>
#define loop(i,a,b) for(register int i=(a);~i;i=(b))
#define Mem(a,b) memset((a),(b),sizeof((a)))
#define eb emplace_back
#define pb push_back
using namespace std;
using i64 = long long;
using uint = unsigned int;
using ui64 = unsigned long long;
using i128 = __int128;
constexpr int N = 4e6 + 15, mod = 998244353, inf = 1e9;
namespace FAST_IO {
#define IOSIZE 300000
char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
template <typename T> inline void read (T &x) { x = 0; T f = 1;char ch = getchar ();while (!isdigit (ch)) {if (ch == '-') f = -1; ch = getchar ();}while (isdigit (ch)) {x = (x << 1) + (x << 3) + (ch ^ '0'); ch = getchar ();} x *= f;}
template <> inline void read (double &x) { x = 0; int f = 1;char ch = getchar ();while (!isdigit (ch)) { if (ch == '-') f = -1; ch = getchar ();} while (isdigit (ch)) x = x * 10 + (ch - '0'), ch = getchar ();if (ch == '.') {ch = getchar (); for (double t = 0.1; isdigit (ch); t *= 0.1) x += t * (ch - '0'), ch = getchar ();}x *= f;}
inline void read(char &x) {char ch;while ((ch = getchar()) != EOF && isspace(ch));if (ch != EOF) x = ch;}
inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
inline bool read(string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
template <typename T, typename ...Args> inline void read (T &x, Args &...args) {read(x); read(args...);}
template <typename T> inline void write (T x) { if (x < 0) putchar ('-'), x = -x; if (x > 9) write (x / 10); putchar (x % 10 + 48);}
inline void write(string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
inline void write(char *x) { while (*x) putchar(*x++); }
inline void write(const char *x) { while (*x) putchar(*x++); }
inline void write(char x) { putchar(x); }
struct fio { ~fio () {if (p3 != obuf) { fwrite(obuf, 1, p3 - obuf, stdout);p3 = obuf;}}} io;
template <typename T> fio& operator >> (fio &io, T &x) {return read (x), io;}
template <typename T> fio& operator << (fio &io, const T &x) {return write (x), io;}
#define cin io
#define cout io
#define endl '\n'
}
using namespace FAST_IO;
bool MS;
int n, m;
i64 k;
int head[N], to[N << 1], nxt[N << 1], idx;
i64 val[N << 1];
i64 bit[65];
int vis[N];
i64 dis[N];
int trie[N][2], tot;
i64 ed[N];
inline void add (int u, int v, i64 w) {
to[idx] = v;
val[idx] = w;
nxt[idx] = head[u];
head[u] = idx ++;
}
inline void insert (i64 x) {
for (int i = 60; i >= 0; -- i) {
if ((x >> i) & 1) {
if (!bit[i]) {
bit[i] = x;
break;
} else x ^= bit[i];
}
}
}//线性基
void dfs (int u, int fa) {
vis[u] = 1;
for (int i = head[u]; ~i; i = nxt[i]) {
int v = to[i];
i64 w = val[i];
if (v == fa) continue;
if (!vis[v]) {//没遇到环
dis[v] = dis[u] ^ w;//正常求权值
dfs (v, u);
} else {//遇到环了
insert (dis[u] ^ dis[v] ^ w);
}
}
}//将这些环的权值扔到线性基里
inline void inserttrie (i64 x) {
int p = 1;
for (int i = 60; i >= 0; -- i) {
int c = (x >> i) & 1;
if (!trie[p][c]) trie[p][c] = ++ tot;
p = trie[p][c];
ed[p] ++;
}
}//0/1trie
inline i64 query (i64 x) {
int p = 1;
i64 ans = 0;
for (int i = 60; i >= 0; -- i) {
if (!p) break;
int bx = (x >> i) & 1, bk = (k >> i) & 1;
if (bk) {
if (trie[p][bx]) {
ans += ed[trie[p][bx]];
}
p = trie[p][bx ^ 1];
} else {
p = trie[p][bx];
}
}
if (p) ans += ed[p];
return ans;
}//查询
inline void solve () {
for (int i = 0; i <= tot + 10; ++ i) ed[i] = 0, trie[i][0] = trie[i][1] = 0;
idx = 0, tot = 1;
cin >> n >> m >> k;
for (int i = 0; i <= n + 10; ++ i) head[i] = -1, vis[i] = 0, dis[i] = 0;
Mem (bit, 0);
for (int i = 1; i <= m; ++ i) {
int u, v;
i64 w;
cin >> u >> v >> w;
add (u, v, w);
add (v, u, w);
}
dfs (1, 0);//预处理线性基
for (int i = 1; i <= n; ++ i) {
for (int j = 60; j >= 0; -- j) {
if (bit[j] && (dis[i] >> j) & 1) dis[i] ^= bit[j];
}
}//求 mv[u]
i64 ans = 0;
for (int i = 1; i <= n; ++ i) {
ans += query (dis[i]);//求答案
inserttrie (dis[i]);
}
cout << ans << endl;
}
bool MT;
int main () {
int T_T;
cin >> T_T;
while (T_T --) solve ();
cerr << "Memory:" << (&MT - &MS) / 1048576.0 << "MB Time:" << clock() << "ms" << endl;
return 0;
}
::::