Article directory
Question 225. 2022 Winter Holiday Ladder Competition Training-7-15 Shouting Mountain (30 points)
1. The topic
2. Problem solving
Just do bfs and you're done
#include <bits/stdc++.h>
using namespace std;
const int maxn=10010;
int n,m;
vector<int> G[maxn];
int visited[maxn];
int main()
{
int i,j;
int k,v1,v2;
int v0,w;
cin>>n>>m>>k;
for(i=0; i<m; i++)
{
scanf("%d%d",&v1,&v2);
G[v1].push_back(v2);
G[v2].push_back(v1);
}
for(i=0; i<k; i++)
{
fill(visited,visited+maxn,0);
scanf("%d",&v0);
queue<int> q;
q.push(v0);
visited[v0]=1;
int temp;
while(!q.empty())
{
temp=q.front();
q.pop();
sort(G[temp].begin(),G[temp].end(),greater<int>());//降序排序,保证在最后的是最小节点
for(int i=0;i<G[temp].size();i++)
{
if(!visited[G[temp][i]])
{
q.push(G[temp][i]);
visited[G[temp][i]]=1;
}
}
}
if(temp==v0)
{
temp=0;
}
printf("%d\n",temp);
}
}