跳到正文

题解

ABC451G

XXh0919约 12 分钟
原文
文章信息与版本

文章信息

发布
2026年3月28日
收录
2026年3月29日
更新
2026年11月21日
篇幅
约 12 分钟
获赞
1

版本

  1. v2当前
  2. v1

upd on 2026.4.1:修改了一些错误。

又来写题解了。

题意

有一个由 nn 个点 mm 条边组成的无向图,每条边有边权。问你图中有多少个点对 (i,j)(i,j) 满足 1≤i<j≤n1\le i<j\le n 并且连接 ii,jj 的路径权值最小值不超过 kk,其中 kk 是全局给定的常数。

一条路径的权值定义为这条路径所有边的异或和。

解法

我会保持极高注意力。

首先考虑两条 uu 到 vv 的路径,设这两条路径的异或和分别为 x1,x2x_1,x_2,那么容易注意到 x1⊕x2x_1\oplus x_2 的值一定是某一个环的异或值。如下图:

那么相当于 u−a−c−vu-a-c-v 的权值和 u−a−b−c−vu-a-b-c-v 的权值异或起来,把 u−au-a 和 c−vc-v 的权值抵消了,剩下的就是环 a−b−ca-b-c 的权值。

那么我们就可以发现,uu 到 vv 任意一条路径的权值,都可以由这两点间选一条不是该路径的路径,再异或上若干个环得到。

设环 cc 的权值是 vcv_c,那么我们钦定 11 号点为起点,令 dud_u 表示 11 到 uu 的任意一条路径的权值,那么 uu 到 vv 的路径就可以表示为 du⊕dv⊕vcd_u\oplus d_v\oplus v_c,其中 cc 是图中所有环的异或和构成的集合。

接着我们又又注意到,我们异或上去的环可以随意选,前提是能够从 uu 走这个环走到 vv。那么我们通过这样任意的组合就可以得到很多个权值。而我们希望把这所有得到的权值异或上一个数,使这个数最小。好像有点熟悉了?

线性基!

没错,线性基是个很不错的选择。因为这个东西可以把我们这些能得到的权值全部存下来,并且能够求出上面说的最小值。我们令 mvxmv_x 表示 xx 异或上线性基里的数(就是那一堆环能得到的权值)的最小值是多少,那么我们希望 du⊕dv⊕vd_u\oplus d_v\oplus v 最小,也就是希望 mvdu⊕dvmv_{d_u\oplus d_v} 最小。

然后我们可以证明 mvdu⊕dv=mvdu⊕mvdvmv_{d_u\oplus d_v}=mv_{d_u}\oplus mv_{d_v},因为这就相当于你将两条个路径的权值搞到最小,然后异或起来,肯定是等于这两个异或起来求最小。那么问题就转化成了求又多少个 (u,v)(u,v) 满足 1≤u<v≤n1\le u<v\le n 且 mvdu⊕mvdv≤kmv_{d_u}\oplus mv_{d_v}\le k。这怎么快速求呢?

字典树!

是的,我们直接将 mvumv_u 塞到一个 0/1 trie 上,然后就能够快速求出答案啦。

时间复杂度 O(nlog⁡V)O(n\log V),其中 VV 是数值最大值。

不理解可以看代码。

Code
C++
#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;
}
原文