跳到正文

个人记录

「gym104197G」Graph Problem With Small n

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

文章信息

发布
2023年3月18日
收录
2025年12月12日
更新
2026年11月18日
篇幅
约 7 分钟
获赞
2

版本

  1. v1当前

Problem\texttt {Problem}

题目描述

给定一个 nn 个点的无向图,对每对二元组 (i,j)(1≤i,j≤n)(i, j) (1 \leq i, j \leq n),判断是否存在一条 i→ji \to j 的哈密顿路径。

哈密顿路径:一条恰好经过每个点一次的路径。

数据范围

2≤n≤242 \leq n \leq 24

Solution\texttt {Solution}

不难想到状压dp,有个暴力的做法,fS,i,jf_{S, i, j} 表示从 ii 出发,经过了点集 SS,终点为 jj 是否可行,时间复杂度是 O(2nn3)\mathcal O (2^n n^3),空间复杂度 O(2nn2)\mathcal O (2^n n^2)。

记录每个点 ii 的相邻点的点集 ViV_i,上面的状态就可以直接枚举可能的终点,用 ViandS′V_i \texttt {and} S' 是否有值来 O(1)\mathcal O (1) 判断其可不可能成为终点,时间复杂度可以优化成 O(2nn2)\mathcal O (2^n n^2),空间复杂度不变。

因为每个 dp 值只有 0/10/1 两种情况,不妨把 jj 这一维放入 dp 值中,即 fS,if_{S, i} 表示从 ii 出发,经过了点集 SS,能到达的终点的集合(这个方法很常见,类似高维前缀和,相当于能用 int 表示的 bitset,因为位数少不需要 bitset),这样空间复杂度就是 O(2nn)\mathcal O (2^n n) 的。

一般的图论状压都是这样,那么这道题肯定有它的特殊之处,就是哈密顿路径,想想哈密顿路径的性质。

每个点恰好经过一次,这说明什么?最后的路径是一条链,一定有一个点是 00(记点的标号为 0∼(n−1)0 \sim (n - 1)),它会把这条路径分成两部分。由于是无向图,不妨考虑从 00 出发向两端走,如果这两条路径不经过同一个点(除了起点 00),并且和起来经过了 nn 个点,是不是就说明这两条路径的终点 uu 和 vv 之间存在一条哈密顿路径。

也就是说,如果我们能找到 0→u0 \to u 经过的点集 SS ,0→v0 \to v 经过的点集 TT,使得 S∩T={0}∧S∪T={0,1,2,⋯ ,n−1}=US \cap T = \{ 0 \} \wedge S \cup T = \{ 0, 1, 2, \cdots, n - 1 \} = U(设 UU 为全集),那么 (u,v)(u, v) 的答案就为 11 。

所以,我们只需要找到所有的 fS,0f_{S, 0},然后枚举 SS,此时 T=(UxorS)∪{0}T = (U \texttt {xor} S) \cup \{ 0 \},如果 u∈fS,0∧v∈fT,0u \in f_{S, 0} \wedge v \in f_{T, 0},则 (u,v)(u, v) 的答案是 11,具体实现可以用 ans[u]\texttt {ans[u]} 表示起点为 uu 答案为 11 的终点 vv 的集合(和上面 dp 去维的方法一样)。那么转移时,就是再枚举 uu,如果 uu 在集合 fS,0f_{S, 0} 里,就用 fT,0f_{T, 0} 更新 ans[u]\texttt {ans[u]},总时间复杂度 O(2nn)\mathcal O (2^n n)。

因为我们的 fS,if_{S, i} 实际只有一维(i=0i = 0),所以空间复杂度是 O(2n)\mathcal O (2^n) 的,转移时枚举 SS 和可能的终点 jj,时间复杂度 O(2nn)\mathcal O (2^n n)。

总时间复杂度 O(2nn)\mathcal O (2^n n),空间复杂度 O(2n)\mathcal O (2^n),可以通过本题。

Code\texttt {Code}

C++
#include <cstdio>

#define LL long long
#define uLL unsigned LL

namespace Read {
	static const int buf_size = 1 << 12;
	static const bool use_fread = false;
	 
	static unsigned char buf[buf_size];
	static int buf_len, buf_pos;
	
	bool isEOF() {
		if (buf_pos == buf_len) {
			buf_pos = 0; buf_len = fread(buf, 1, buf_size, stdin);
			if (buf_pos == buf_len) return true;
		}
		return false;
	}
	
	char readChar() {
		if (!use_fread) return getchar();
		return isEOF() ? EOF : buf[buf_pos++];
	}
	
	LL rint() {
		LL x = 0, Fx = 1; char c = readChar();
		while (c < '0' || c > '9') { Fx ^= (c == '-'); c = readChar(); }
		while ('0' <= c && c <= '9') { x = (x << 3) + (x << 1) + (c ^ 48); c = readChar(); }
		return Fx ? x : -x;
	}
	
	template <typename T>
	void read(T &x) {
		x = rint(); 
	}
	
	template <typename T, typename... Ts>
	void read(T &x, Ts &...rest) {
		read(x);
		read(rest...);
	}
} using namespace Read;

const int MAX_n = 24;

int n, up;
int v[MAX_n + 5];
int ans[MAX_n + 5];
int dp[(1 << MAX_n) + 5];

char str[MAX_n + 5][MAX_n + 5];

int main() {
	#ifdef LOCAL
		freopen("DATA.in", "r", stdin);
//		freopen("DATA.out", "w", stdout);
	#endif
	read(n); up = 1 << n;
	for (int i = 0; i < n; i++) {
		scanf("%s", str[i]);
		for (int j = 0; j < n; j++)
			if (str[i][j] == '1') v[i] |= (1 << j);
	}
	dp[1] = 1;
	for (int S = 3; S < up; S += 2) {
		for (int i = 0; i < n; i++) {
			if (S & (1 << i)) {
				int T = S ^ (1 << i);
				if (v[i] & dp[T]) dp[S] |= (1 << i);
			}
		}
	}
	for (int S = 0; S < up; S++)
		for (int i = 0; i < n; i++)
			if (dp[S] & (1 << i)) ans[i] |= dp[((up - 1) ^ S) | 1];
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++)
			printf("%d", (ans[i] >> j) & 1);
		printf("\n");
	}
	return 0;
}
原文