题目
#include <queue>
#include <vector>
#include <cstdio>
#include <memory.h>
#define maxn 1000005
using namespace std;
int n, m;
bool b[maxn];
queue <int> q;
vector <int> p[maxn];
void input() {
scanf("%d %d", &n, &m);
int u, v;
for(int i = 1; i <= m; i++) {
scanf("%d %d", &u, &v);
p[u].push_back(v);
}
return ;
}
void dfs(int x) {
printf("%d ", x);
for(int i = 0, sz = p[x].size(); i < sz; i++)
if(!b[p[x][i]]) {
b[p[x][i]] = true;
dfs(p[x][i]);
}
return ;
}
void bfs() {
while(!q.empty()) {
int x = q.front();
q.pop();
printf("%d ", x);
for(int i = 0, sz = p[x].size(); i < sz; i++)
if(!b[p[x][i]]) {
b[p[x][i]] = true;
q.push(p[x][i]);
}
continue;
}
return ;
}
int main() {
input();
b[1] = true;
dfs(1);
puts("");
memset(b, false, sizeof(b));
b[1] = true;
q.push(1);
bfs();
return 0;
}