不是,为什么RE啊
查看原帖
不是,为什么RE啊
790632
FiammettaX楼主2024/3/27 10:50
#include<iostream>
#include<queue>
#include<algorithm>
#include<vector>
#include<string.h>
using namespace std;
const int N = 100010,M=N*10;
int idx;
bool vis[N];
int e[N], ne[N], h[N];
int ans[N],a;
int help[N];
void add(int a,int b)
{
	e[idx] = b; ne[idx] = h[a]; h[a] = idx++;		//单链表存储邻接表
}
void bfs()
{
	queue<int> q;
	q.push(1);
	vis[1] = 1;
	ans[a++] = 1;
	while (!q.empty())
	{
		int t = q.front();
		q.pop();
		int hh=0;
		for (int i = h[t]; i != -1; i = ne[i])		//将t的所有子结点进行排序
		{
			help[hh++] = e[i];						//子结点入队
		}
		sort(help,help+hh);
		for (int i = 0; i < hh; i++)
		{
			if (!vis[help[i]])
			{
				ans[a++] = help[i];
				q.push(help[i]);
				vis[help[i]] = 1;
			}
		}
	}
}
void dfs(int x)
{
	ans[a++] = x;
	vis[x] = 1;
	if (h[x] == -1) return;
	vector<int> help;
	for (int i = h[x]; i != -1; i = ne[i]) help.push_back(e[i]);		//子节点排序载入help
	sort(help.begin(), help.end());
	for (int i = 0; i < help.size(); i++)
	{
		if (!vis[help[i]])
		{
			dfs(help[i]);
		}
	}
	help.clear();
}
int main()
{
	int n, m;
	cin >> n >> m;
	memset(h, -1, sizeof h);
	for (int i = 0; i < m; i++)
	{
		int a, b;
		cin >> a >> b;
		add(a, b);
	}
	dfs(1);
	for (int i = 0; i < a; i++) cout << ans[i] << " ";
	cout << endl;
	a = 0;
	memset(vis, 0, sizeof vis);
	bfs();
	for (int i = 0; i < a; i++) cout << ans[i] << " ";
}
只有20分,就第一个AC了,其他都RE了,样例和自己弄的几个测试都是对的,完全搞不懂哪里导致的RE啊?
Runtime Error.
Received signal 11: Segmentation fault with invalid memory reference.
2024/3/27 10:50
加载中...