求条
查看原帖
求条
1228049
zhukaiting楼主2025/1/4 09:41

只有 1 个 AC,4 个 WA。

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+1;
bool b[N];
int m,n,p,q;
struct f{
	int a,id;
}w[N];
vector<int>v[N];
bool cmp(f x,f y){
	if(x.a==y.a)return x.id<y.id;
	return x.a<y.a;
}
void dfs(int x){
	if(b[x])return;
	printf("%d ",x);
	b[x]=1;
	for(int i=0;i<v[x].size();i++)dfs(v[x][i]);
	return;
}
int main(){
	scanf("%d%d",&n,&m);
	w[1].a=1;
	for(int i=1;i<=n;i++)w[i].id=i;
	while(m--){
		scanf("%d%d",&p,&q);
		if(w[q].a==0)w[q].a=w[p].a+1;
		v[p].push_back(q);
	}
	for(int i=1;i<=n;i++)sort(v[i].begin(),v[i].end());
	dfs(1);
	printf("\n");
	sort(w+1,w+n+1,cmp);
	for(int i=1;i<=n;i++)printf("%d ",w[i].id);
	return 0;
}
2025/1/4 09:41
加载中...