https://www.luogu.com.cn/problem/P5318
#include<bits/stdc++.h>
using namespace std;
int n,m;
vector<int> e[100005];
bool vis[100005];
void dfs(int u){
cout<<u<<" ";
for(int i=0;i<e[u].size();i++){
int v=e[u][i];
if(!vis[v]){
vis[v]=1;
dfs(v);
}
}
}
void bfs(){
memset(vis,0,sizeof(vis));
queue<int>q;
q.push(1);
vis[1]=1;
while(!q.empty()){
int u=q.front();
q.pop();
cout<<u<<" ";
for(int i=0;i<e[u].size();i++){
int v=e[u][i];
if(!vis[v]){
vis[v]=1;
q.push(v);
}
}
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
int u,v;
cin>>u>>v;
e[u].push_back(v);
}
for(int i=1;i<=n;i++){
sort(e[i].begin(),e[i].end());
}
vis[1]=1;
dfs(1);
cout<<endl;
bfs();
return 0;
}