SPFA求调,80分, WA#7、#10
查看原帖
SPFA求调,80分, WA#7、#10
486799
BlackPanda楼主2022/5/15 09:01
#include <bits/stdc++.h>
using namespace std;

inline int read(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
	return x*f;
}

int n,m;
int head[500010],ne[500010],ver[500010],tot;
int d[100010];
int v[100010];

void add(int x,int y){
	ver[++tot]=y;
	ne[tot]=head[x];
	head[x]=tot;
}

void spfa(int s){
	for(int i=1;i<=n;i++)	d[i]=0x3f;
	d[s]=0;
	queue<int> q;
	q.push(s);
	v[s]=1;
	while(!q.empty()){
		int x=q.front();
		q.pop();
		v[x]=0;
		for(int i=head[x];i;i=ne[i]){
			int y=ver[i];
			if(d[y] > d[x]+1){
				d[y]=d[x]+1;
				if(!v[y]){
					q.push(y);
					v[y]=1;
				}
			}
		}
	}
}

int main(){
	n=read();
	m=read();
	for(int i=1;i<=m;i++){
		int x,y;
		x=read();
		y=read();
		add(x,y);
		add(y,x);
	}
	spfa(1);
	int maxv=0;
	for(int i=1;i<=n;i++){
		maxv=max(maxv,d[i]);
	}
	int flag=0,ans=0;
	for(int i=1;i<=n;i++){
		if(maxv==d[i]){
			ans++;
			if(!flag){
				printf("%d ",i);
				flag=1;	
			}
		}
	}
	printf("%d %d",maxv,ans);
	return 0;
}


2022/5/15 09:01
加载中...