样例过了只有20分,求调感谢大佬%%%
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e6+10;
typedef struct{
int v,u;
} Edge;
vector<Edge> side;
vector<int> edges[MAX];
bool bmp(Edge x,Edge y){
if(x.v == y.v) return x.u < y.u;
else return x.v < y.v;
}
int book[MAX];
int book2[MAX];
void dfs(int time){
for(int i = 0; i < edges[time].size(); i++){
if(!book[edges[time][i]]){
cout << edges[time][i] << ' ';
book[edges[time][i]] = 1;
dfs(edges[time][i]);
//book[edges[time][i]] = 0;
}
}
}
queue<int> q;
void bfs(){
while(!q.empty()){
int t = q.front();
for(int i = 0; i < edges[t].size(); i++){
if(!book2[edges[t][i]]){
cout << edges[t][i] << ' ';
q.push(edges[t][i]);
book2[edges[t][i]] = 1;
}
}
q.pop();
}
}
int main(){
int n,m;
cin >> n >> m;
for(int i = 1; i <= m; i++){
Edge t;
cin >> t.v >> t.u;
side.push_back(t);
}
sort(side.begin(),side.end(),bmp);
for(int i = 0; i < m; i++){
edges[side[i].v].push_back(side[i].u);
}
cout << 1 << ' ';
book[1] = 1;
dfs(1);
q.push(1);
cout << endl << 1 << ' ';
bfs();
return 0;
}