社区讨论

神秘错误求答

P2444[POI 2000] 病毒参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@mk590y4n
此快照首次捕获于
2026/01/08 17:32
2 个月前
此快照最后确认于
2026/01/10 23:20
2 个月前
查看原帖
dfs 判环做法,如果外面套个 ans 记答案就 WA,但是把 dfs 改成返回 bool 就 AC。
WA :
codeCPP
#include <bits/stdc++.h>
using namespace std;

bool st;

const int N = 3e4 + 5;
int n;
int tot, ch[N][2], fail[N];
bool vis[N];

void Insert (string s) {
	int len = s.length (), now = 0;
	for (int i = 0; i < len; ++i) {
		int num = s[i] - '0';
		if (!ch[now][num]) ch[now][num] = ++tot;
		now = ch[now][num];
	}
	vis[now] = true;
}

void Fail () {
	queue <int> q;
	while (!q.empty ()) q.pop ();
	for (int i = 0; i <= 1; ++i) if (ch[0][i]) {
		fail[ch[0][i]] = 0;
		q.push (ch[0][i]);
	}
	
	while (!q.empty ()) {
		int u = q.front ();
		q.pop ();
		
		for (int i = 0; i <= 1; ++i) {
			if (ch[u][i]) {
				fail[ch[u][i]] = ch[fail[u]][i];
				vis[ch[u][i]] |= vis[fail[u]];
				q.push (ch[u][i]);
			} else ch[u][i] = ch[fail[u]][i];
		}
	}
}

bool ans;
bool viss[N], viis[N];
void dfs (int u) {
	if (viis[u] || vis[u]) return;
	if (viss[u]) {
		ans = true;
		return ;
	}
	viss[u] = viis[u] = true;
	
	for (int i = 0; i <= 1; ++i) dfs (ch[u][i]); 
	
	viis[u] = false;
}

bool ed;

int main () {

	ios::sync_with_stdio (false);
	cin.tie (0); cout.tie (0);
	
	cerr << "[Memory] " << (&st - &ed) / 1024 / 1024 << " MB\n";
	
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		string s;
		cin >> s;
		Insert (s);
	}
	
	Fail ();
	dfs (0);
	puts (ans ? "TAK" : "NIE");

	return 0;
}
AC:
code1CPP
#include <bits/stdc++.h>
using namespace std;

bool st;

const int N = 3e4 + 5;
int n;
int tot, ch[N][2], fail[N];
bool vis[N];

void Insert (string s) {
	int len = s.length (), now = 0;
	for (int i = 0; i < len; ++i) {
		int num = s[i] - '0';
		if (!ch[now][num]) ch[now][num] = ++tot;
		now = ch[now][num];
	}
	vis[now] = true;
}

void Fail () {
	queue <int> q;
	while (!q.empty ()) q.pop ();
	for (int i = 0; i <= 1; ++i) if (ch[0][i]) {
		fail[ch[0][i]] = 0;
		q.push (ch[0][i]);
	}
	
	while (!q.empty ()) {
		int u = q.front ();
		q.pop ();
		
		for (int i = 0; i <= 1; ++i) {
			if (ch[u][i]) {
				fail[ch[u][i]] = ch[fail[u]][i];
				vis[ch[u][i]] |= vis[ch[fail[u]][i]];
				q.push (ch[u][i]);
			} else ch[u][i] = ch[fail[u]][i];
		}
	}
}

bool ans;
bool viss[N], viis[N];
bool dfs (int u) {
	if (viss[u]) return true;
	if (viis[u] || vis[u]) return false;
	viss[u] = viis[u] = true;
	
	bool ret = false;
	for (int i = 0; i <= 1; ++i) ret |= dfs (ch[u][i]); 
	
	viss[u] = false;
	return ret;
}

bool ed;

int main () {

	ios::sync_with_stdio (false);
	cin.tie (0); cout.tie (0);
	
	cerr << "[Memory] " << (&st - &ed) / 1024 / 1024 << " MB\n";
	
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		string s;
		cin >> s;
		Insert (s);
	}
	
	Fail ();
	puts (dfs (0) ? "TAK" : "NIE");

	return 0;
}

回复

0 条回复,欢迎继续交流。

正在加载回复...