为什么 WA
查看原帖
为什么 WA
890515
ikunTLE楼主2025/1/18 19:42

看一眼难度是绿,我哪些条件看少了

#include<bits/stdc++.h>
using namespace std;
int read(){int x=0;char f=1,ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();return x*f;}
const int N=1e5+10;
int a[N],f[N];
vector<int>vc[N];
bool vis[N];
struct node{
	int x,l;
};queue<node>q;
void bfs(int x){
	memset(vis,false,sizeof(vis));
	vis[x]=true;
	for(auto i:vc[x])
		if(f[i]>1){
			f[i]=1;
			q.push({i,1});
			vis[i]=true;
		}
	while(!q.empty()){
		node u=q.front();q.pop();
		for(auto i:vc[u.x])
			if(u.l+1<f[i]&&!vis[i]){
				f[i]=u.l+1;
				q.push({i,u.l+1});
				vis[i]=true;
			}
	}
	return;
}
int main(){
	int n=read(),m=read(),k=read();
	for(int i=1;i<=k;++i)
		a[i]=read();
	while(m--){
		int u=read(),v=read();
		vc[u].push_back(v);
		vc[v].push_back(u);
	}
	memset(f,0x3f,sizeof(f));
	for(int i=1;i<=k;++i)
		bfs(a[i]);
	for(int i=1;i<=n;++i)
		if(f[i]>N)
			printf("-1 ");
		else printf("%d ",f[i]);
	return 0;
}
2025/1/18 19:42
加载中...