20pts求助,只过样例
查看原帖
20pts求助,只过样例
933414
User_10_noob楼主2024/9/27 22:47

玄关%%%

#include<bits/stdc++.h>
#define rep(a,b,c) for (int i=a;i<=b;i+=c)
#define out(n) cout<<(n)<<' '
#define ll long long
using namespace std;
const int N=1e5+5,N1=1e6+5;
inline void read(int &n){
	n=0;
	char c;
	c=getchar();
	while (c>'9'||c<'0') c=getchar();
	do{
		n=(n<<1)+(n<<3)+(c^48);
		c=getchar();
	}while (c>='0'&&c<='9');
}
vector<int>G[N];
bool vis[N];
struct relation{
	int u,v;
};
bool cmp(relation a,relation b){
	if (a.u==b.u) return a.v<b.v;
	return a.u<b.u;
}
void dfs(int i){
	cout<<i<<' ';
	for (int j=0;j<G[i].size();j++){
		if(!vis[G[i][j]]){
			vis[G[i][j]]=1;
			dfs(G[i][j]);
		}
	}
}
queue<int>q;
void bfs(){
	q.push(1);
	while (!q.empty()){
		int i=q.front();
		q.pop();
		for (int j=0;j<G[i].size();j++){
			if (!vis[G[i][j]]){
				cout<<G[i][j]<<' ';
				vis[G[i][j]]=1;
				q.push(G[i][j]);
			}
		}
	}
}
int main(){
	int n,m;
	read(n);read(m);
	relation *b=new relation[m+5];
	for (int i=1;i<=m;i++){
		read(b[i].u);read(b[i].v);
	}
	sort(b+1,b+n+1,cmp);
	for (int i=1;i<=m;i++)
		G[b[i].u].push_back(b[i].v);
	vis[1]=1;
	dfs(1);
	memset(vis,0,sizeof(vis));
	vis[1]=1;
	printf("\n%d ",1);
	bfs();
	return 0;
} 
2024/9/27 22:47
加载中...