#include #include #include #include #include using namespace std; struct NodeT { vector adj; int cost{0}; }; using GraphT = map; void MultiDFS(GraphT & G, vector sources); int main() { vector horrorList; GraphT graph; int movies; cin >> movies; for(int i = 0; i < movies; ++i){ graph[i].cost = movies + 1; } int horrorListSize; cin >> horrorListSize; int edges; cin >> edges; for(int i = 0; i < horrorListSize; ++i) { int tmp; cin >> tmp; horrorList.push_back(tmp); } for(int i = 0; i < edges; ++i) { int src, dest; cin >> src >> dest; graph[src].adj.push_back(dest); graph[dest].adj.push_back(src); } MultiDFS(graph, horrorList); int maxCost{graph[0].cost}; int maxID{0}; for(auto & x: graph) { if (maxCost < x.second.cost) { maxCost = x.second.cost; maxID = x.first; } } cout << maxID << endl; return 0; } void MultiDFS(GraphT & G, vector sources) { // the max cost has already been set up. queue Q; for(auto x: sources) { G[x].cost = 0; Q.push(x); } while (Q.size() > 0) { int u = Q.front(); Q.pop(); for(auto v: G[u].adj) { if (G[v].cost > G[u].cost + 1) { G[v].cost = G[u].cost + 1; Q.push(v); } } } }