20分 MLE 求助!
查看原帖
20分 MLE 求助!
411816
自信即巅峰66楼主2023/3/26 19:08
#include<bits/stdc++.h>
#include<queue>
using namespace std;
const int maxm=1000005;
int head[100005];
int bz1[100005]={0};
int bz[100005]={0};
queue<int>q;
struct code{
   int next; 
   int to;
}doc[maxm];
struct aode{
   int x;
   int y;
}a[maxm];
bool cmp(aode a,aode b)
{
   if(a.x==b.x)
     return a.y>b.y;
   else
     return a.x<b.x;
}
void check(int from , int to , int num)
{   
   doc[num].next=head[from];
   head[from]=num;	
   doc[num].to=to;
}
void dfs(int t)
{
   if(!bz[t])
   {
   	cout << t << " ";
   	bz[t]=1;
   }
   for(int i = head[t] ; i ; i = doc[i].next)	
   	 {
   	 	if(i != 0)
   	 	  dfs(doc[i].to);	
   	 	else 
   		  return;	 	
   	 }   
}
void bfs(int g)
{
   while(!q.empty())
   {	
   		for(int i = head[q.front()] ; i != 0 ; i = doc[i].next )
   		{
   			if(bz1[doc[i].to] != 1)
   			{
   				q.push(doc[i].to);
   				bz1[doc[i].to] = 1;
   			}
   		}
   		cout << q.front() << " " ;
   		q.pop();	
   }
}
int main(){ 
   int n,m,bz;
   memset(doc,0,sizeof(doc));
   memset(head,0,sizeof(head));
   cin >> n >> m;
   for(int i = 1 ; i <= m ; i++)
     cin >> a[i].x >> a[i].y ;
   sort(a+1,a+1+m,cmp);
   for(int i = 1 ; i <= m ; i++)
     check(a[i].x,a[i].y,i);	
   dfs(1);
   cout << endl;
   q.push(1);
   bz1[1]=1;
   bfs(1);
   return 0;	
}
2023/3/26 19:08
加载中...