记忆化DFS过了?求正确性证明 or HACK
查看原帖
记忆化DFS过了?求正确性证明 or HACK
732906
4BboIkm7h楼主2025/8/31 18:49

如题,打了个暴搜(dfs)只能过一个点,其他全 MLE,然后挂上记忆化之后直接就过了?是数据太水了吗,而且题解区貌似没找到同款解法……如果假了希望能提供一个 HACK。

提交记录

#include<bits/stdc++.h>
using namespace std;
const int N = 105, INF = 0x3f3f3f3f;
inline int read(){
	int x = 0, neg = 1;
	char c = getchar();
	while(!isdigit(c)) {if(c == '-') neg = -1; c = getchar();}
	while(isdigit(c)) {x = (x << 1) + (x << 3) + (c ^ 48); c = getchar();}
	return x * neg;
}
int n, m, a[N][15], dp[1<<11], ans = INF;
void dfs(int step, int sta){
	if(step >= dp[sta]) return;
	dp[sta] = step;
	if(sta == 0) {
		ans = min(ans, step - 1);
		return;
	}
	if(step >= ans) return;
	for(int i = 1; i <= m; i++){
		int new_sta = sta;
		for(int j = 1; j <= n; j++) {
			if(a[i][j] == 1 && (sta & (1 << j))) new_sta ^= (1 << j);
			if(a[i][j] == -1 && !(sta & (1 << j))) new_sta ^= (1 << j);
		}
		if(new_sta != sta) dfs(step + 1, new_sta);
	}
}
signed main(){
	n = read(), m = read();
	for(int i = 1; i <= m; i++)
		for(int j = 1; j <= n; j++) a[i][j] = read();
	memset(dp, 0x3f, sizeof(dp));
	dfs(1, (1 << (n + 1)) - 2);
	printf("%d", ans == INF ? -1 : ans);
	return 0;
}
2025/8/31 18:49
加载中...