求助,仅20分,对了第一个点
#include<bits/stdc++.h>
using namespace std;
int n,m,a,b,num,r,c1;
int head[100005];
bool f1[100005],f2[100005];
struct pth{
int s,t,next;
};
pth inn[1000005],p[1000005];
queue<int> q;
void add(int x,int y)
{
num++;
p[num].next=head[x];
p[num].s=x;
p[num].t=y;
head[x]=num;
}
int d[1000005];
bool cmp(pth a,pth b)
{
if(b.t>a.t) return 0;
if(b.t<=a.t) return 1;
}
void dfs(int x)
{
f1[x]=1;
cout<<x<<' ';
int now;
now=head[x];
while(now)
{
if(f1[p[now].t]==0)
{
dfs(p[now].t);
}
now=p[now].next;
}
return;
}
void bfs(int x)
{
int now,bb;
q.push(x);
while(!q.empty())
{
bb=q.front();
f2[bb]=1;
cout<<bb<<' ';
now=head[bb];
while(now)
{
if(f2[p[now].t]==0)
{
q.push(p[now].t);
f2[p[now].t]=1;
}
now=p[now].next;
}
q.pop();
}
return;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=m;i++)
{
cin>>inn[i].s>>inn[i].t;
}
sort(inn+1,inn+1+n,cmp);
for(int i=1;i<=m;i++)
add(inn[i].s,inn[i].t);
dfs(1);
cout<<endl;
bfs(1);
return 0;
}