蒟蒻0分求助
查看原帖
蒟蒻0分求助
1049413
QAQvQAQ楼主2024/1/18 21:49

样例过了

#include<bits/stdc++.h>
using namespace std;
int n,m;
map<int,vector<int> >mp; 
int book[100005];
void dfs(int hhh){
	if(book[hhh]==0){
		book[hhh]=1;
		cout<<hhh<<" ";
		for(int i=0;i<mp[hhh].size();i++){
			dfs(mp[hhh][i]);
		}
	}else{
		return;
	}
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int x,y;
		cin>>x>>y;
		mp[x].push_back(y);
	}
	dfs(1);
	cout<<endl;
	memset(book,0,sizeof(book));
	queue<int>q;
	q.push(1);
	book[1]=1;
	while(!q.empty()){
		int tmp=q.front();
		q.pop();
		cout<<tmp<<" ";
		for(int i=0;i<mp[tmp].size();i++){
			int ntmp=mp[tmp][i];
			if(book[ntmp]==0){
				q.push(ntmp);
				book[ntmp]=1;
			}
		}
	}
    return 0;
}

2024/1/18 21:49
加载中...