This page is still under construction.

Parts of this page are still being built. What you see may change.

Horror List

Interview

Time limit1sMemory limit128 MB

Summary
Each movie gets a level: 0 if on the horror list, else one plus the best level among similar movies; output the movie with the highest finite level, breaking ties by smallest ID.
Level

Medium5 of 10

Topics
Graph, BFS, Shortest path, Implementation
Solved
No attempts yet

Problem

A cinema hosts a surprise screening: a small group gathers in a room and streams one random movie from a large collection. The trouble is that some people end up watching terrible movies and are deeply disappointed.

To prevent this, when a group enters the room they type in a horror list — the bad movies that no one in the group ever wants to see. This list differs from group to group.

You also have a database telling you which movies are directly similar to which. Assume that a movie similar to a bad movie is almost as bad. Formally, the Horror Index (HI) of a movie is defined as follows:

  • HI=0HI = 0 if the movie is on the horror list. (This rule overrides the others.)
  • HI=Q+1HI = Q + 1 if the worst (i.e. lowest-HI) directly similar movie has HI=QHI = Q.
  • HI=+∞HI = +\infty if the movie is not connected to any bad movie at all (directly or indirectly).

Input

The first line contains three integers NN, HH, LL (1≤H<N≤10001 \le H < N \le 1000, 0≤L≤100000 \le L \le 10000), where NN is the number of movies (each identified by an ID from 00 to N−1N-1), HH is the number of movies on the horror list, and LL is the number of similarity relations in the database.

The second line contains HH distinct space-separated integers xix_i (0≤xi<N0 \le x_i < N), the IDs of the movies on the horror list.

Each of the following LL lines contains two space-separated integers aia_i, bib_i (0≤ai<bi<N0 \le a_i < b_i < N), meaning the movie with ID aia_i is similar to the movie with ID bib_i (and vice versa).

Output

Output the ID of the best movie, i.e. the one with the highest Horror Index. If several movies tie, output the one with the smallest ID.

Examples2

  1. Example 1

    Input
    6 3 5
    0 5 2
    0 1
    1 2
    4 5
    3 5
    0 2
    
    Expected output
    1
    
  2. Example 2

    Input
    6 2 3
    5 2
    0 5
    0 1
    3 4
    
    Expected output
    3