直接上代码
#include<bits/stdc++.h>
using namespace std;
set<int> s[100004];
int n,m;
bool chk[100004];
void dfs(int x)
{
if(chk[k])
{
return ;
}
chk[k]=1;
cout<<x<<" ";
for(auto i:s[x])dfs(i);
}
void bfs()
{
queue<int> q;
q.push(1);
while(!q.empty())
{
int x=q.front();
q.pop();
if(chk[x])continue;
chk[x]=1;
cout<<x<<" ";
for(auto i:s[x])q.push(i);
}
}
int main()
{
cin>>n>>m;
for(int i=1;i<=m;i++)
{
int t,x;
cin>>t>>x;
s[t].insert(x);
}
dfs(1);
cout<<endl;
bfs(1);
}