跳到正文

题解

P13336 [GCJ 2012 Finals] Shifting Paths 题解

lyhr31415926约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年6月5日
收录
2026年6月30日
更新
2026年11月18日
篇幅
约 7 分钟
题目
P13336

版本

  1. v1当前

一种直接的做法是设 fS,if_{S,i} 表示当前到达前 n−1n-1 个点的次数为 奇数/偶数,现在在 ii 号点,还需要多少步才能到终点,直接暴力转移就是 O⁡(2nn)\operatorname{O}(2^nn)。

注意到数据范围为 n≤40n\le 40,就容易想到用折半的思想解决。把点分为两个集合 A,BA,B,分别处理两个集合中的点,但发现这样仍然是 O⁡(2nn)\operatorname{O}(2^nn) 的。

注意到一个关键性质:想要从一个状态出发再到达这个状态,且这个状态的所在点可以到 nn,那这个过程中一定会经过 nn。证明考虑设每个点到 nn 的距离为 did_i,从一个状态 (S,x)(S,x) 出发,必然会到达一个与 nn 距离 dx−1d_x-1 的点,以此类推,会推出必然会到达一个与 nn 距离 00 的点。也就是说,从一个可以到 nn 的状态出发,最多只会跳 O⁡(2n)\operatorname{O}(2^n) 条边就可以到达 nn。

考虑建出原图的反图,从 nn 开始 bfs,无法到达的点就扔掉(这些点一定到不了 nn),然后拿出与 nn 距离较近的若干个点放到集合 BB,其他点放到集合 AA,在集合 BB 中,我们每一次可以暴力跳边,复杂度是 O⁡(2∣B∣)\operatorname{O}(2^{|B|}),对于 AA 集合,我们依然可以预处理每个状态到达集合 BB 时的状态,这个复杂度是 O⁡(∣A∣2∣A∣)\operatorname{O}(|A|2^{|A|}),平衡一下发现 ∣B∣=22|B|=22 时复杂度会比较优。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read() {
	int x = 0, f = 1;
	char ch = getchar();
	while(ch < '0' || ch > '9') {
		if(ch == '-')	f = -1;
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9') {
		x = (x << 3) + (x << 1) + ch - '0';
		ch = getchar();
	}
	return x * f;
}
int n, m, ch[105][2], f[300005][20], g[300005][20], h[300005][20], head[1005], tail[1005], nxt[1005], tot, dis[105], pos[105];
vector<int> vcA, vcB;
bool flag[105], vis[300005][20];
void add_edge(int u, int v) {
	tail[++tot] = v;
	nxt[tot] = head[u];
	head[u] = tot;
	return;
}
bool cmp(int x, int y) {
	return dis[x] < dis[y];
}
void dfs(int S, int x) {
	if(vis[S][pos[x]])	return;
	vis[S][pos[x]] = true;
	int ed = ch[x][(S >> pos[x]) & 1], edS = (S ^ (1 << pos[x]));
	if(flag[ed]) {
		f[S][pos[x]] = 1;
		g[S][pos[x]] = ed;
		h[S][pos[x]] = edS;
		return;
	}
	if(dis[ed] > n) {
		f[S][pos[x]] = 1;
		g[S][pos[x]] = ed;
		h[S][pos[x]] = edS;
		return;
	}
	dfs(edS, ed);
	f[S][pos[x]] = f[edS][pos[ed]] + 1;
	g[S][pos[x]] = g[edS][pos[ed]];
	h[S][pos[x]] = h[edS][pos[ed]];
	return;
}
inline int solve(int x, int S1, int S2) {
	if(x == n)	return 0;
	if(dis[x] > n)	return 1e15;
	if(flag[x]) {
		int ed = ch[x][(S2 >> pos[x]) & 1], edS = (S2 ^ (1 << pos[x]));
		return solve(ed, S1, edS) + 1;
	}
	else {
		return solve(g[S1][pos[x]], h[S1][pos[x]], S2) + f[S1][pos[x]];
	}
}
void work() {
	n = read();
	for(int i = 1; i <= n; ++i)	head[i] = 0;
	tot = 0;
	for(int i = 1; i < n; ++i) {
		ch[i][0] = read(); ch[i][1] = read();
		add_edge(ch[i][0], i);
		add_edge(ch[i][1], i);
	}
	queue<int> qu;
	qu.push(n);
	memset(dis, 0x3f, sizeof dis);
	memset(flag, 0, sizeof flag);
	dis[n] = 0;
	while(!qu.empty()) {
		int p = qu.front();
		qu.pop();
		for(int i = head[p]; i; i = nxt[i]) {
			int ed = tail[i];
			if(dis[ed] > n) {
				dis[ed] = dis[p] + 1;
				qu.push(ed);
			}
		}
	}
	for(int i = 1; i <= n; ++i)	pos[i] = i;
	sort(pos + 1, pos + n + 1, cmp);
	vcA.clear(); vcB.clear(); 
	for(int i = 1; i <= min(22ll, n); ++i) {
		if(dis[pos[i]] > n)	break;
		flag[pos[i]] = true;
		vcB.push_back(pos[i]);
	}
	for(int i = min(22ll, n) + 1; i <= n; ++i) {
		if(dis[pos[i]] > n)	break;
		flag[pos[i]] = false;
		vcA.push_back(pos[i]); 
	}
	for(int i = 0; i < vcA.size(); ++i)	pos[vcA[i]] = i;
	for(int i = 0; i < vcB.size(); ++i)	pos[vcB[i]] = i;
	if(dis[1] > n) {
		printf("Infinity\n");
		return;
	}
	m = vcA.size();
	memset(vis, 0, sizeof vis);
	for(int S = 0; S < (1 << m); ++S) {
		for(int i = 0; i < m; ++i) {
			if(!vis[S][i]) {
				dfs(S, vcA[i]);
			}
		}
	}
	int ans = solve(1, 0, 0);
	if(ans > 1e15)	printf("Infinity\n");
	else	printf("%lld\n", ans);
	return;
}
signed main() {
	int T = read();
	for(int Cas = 1; Cas <= T; ++Cas) {
		printf("Case #%d: ", Cas);
		work();
	}
	return 0;
}
原文