#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])
{
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]);
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.