PAT L3-008. 喊山
原题链接
简单
作者:
青丝蛊
,
2021-04-22 19:33:15
,
所有人可见
,
阅读 340
#include <bits/stdc++.h>
using namespace std;
const int N = 10010, INF = 99999;
vector<int> v[N];
int dis[N];
bool vis[N];
int n, m, k;
void bfs(int s)
{
memset(dis, INF, sizeof dis);
memset(vis, false, sizeof vis);
dis[s] = 0, vis[s] = true;
queue<int> que;
que.push(s);
while (que.size())
{
int t = que.front();
que.pop();
for (auto c : v[t])
{
if (!vis[c])
{
dis[c] = dis[t] + 1;
vis[c] = true;
que.push(c);
}
}
}
}
int main()
{
cin >> n >> m >> k;
while (m--)
{
int a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
while (k--)
{
int x;
cin >> x;
bfs(x);
int MAX = 0, t;
for (int i = 1; i <= n; i++)
{
if (dis[i] != INF && dis[i] > MAX)
{
MAX = dis[i];
t = i;
}
}
if (!MAX) puts("0");
else cout << t << endl;
}
return 0;
}