先看一下这个,然后是代码:
#include<bits/stdc++.h>
using namespace std;
struct graph{
int from,to;
friend bool operator <(graph g1,graph g2)
{
return g1.from<g2.from||g1.from==g2.from&&g1.to<g2.to;
}
};
graph* g;
array<vector<int>,100002>arr;
bool* flag;
int n,m;
void dfs(int cur=1)
{
printf("%d ",cur);
flag[1]=true;
bool f=false;
for(register int j=1;j<=m;++j)
{
if(!flag[j]) f=true;
}
if(!f) return;
else
{
for(auto i=arr[cur].begin();i!=arr[cur].end();++i)
{
if(!flag[*i])
{
flag[*i]=true;
dfs(*i);
}
}
}
}
queue<int>q;
void bfs()
{
memset(flag,false,n+1);
q.push(1);
printf("\n");
while(q.size())
{
int tmp=q.front();
q.pop();
if(!flag[tmp]) printf("%d ",tmp);
flag[tmp]=true;
for(auto i=arr[tmp].begin();i!=arr[tmp].end();++i)
{
if(!flag[*i])
{
q.push(*i);
}
}
}
}
int main()
{
scanf("%d%d",&n,&m);
g=new graph[m+1];
flag=new bool[n+1];
memset(flag,false,n+1);
for(int i=0;i<m;i++)
{
scanf("%d%d",&g[i].from,&g[i].to);
}
sort(g,g+m);
for(int i=0;i<m;i++) arr[g[i].from].push_back(g[i].to);
delete[] g;
dfs();
bfs();
return 0;
}
所以请问一下发生了什么?