Horror List
InterviewTime limit1sMemory limit128 MB
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:
- if the movie is on the horror list. (This rule overrides the others.)
- if the worst (i.e. lowest-HI) directly similar movie has .
- if the movie is not connected to any bad movie at all (directly or indirectly).
Input
The first line contains three integers , , (, ), where is the number of movies (each identified by an ID from to ), is the number of movies on the horror list, and is the number of similarity relations in the database.
The second line contains distinct space-separated integers (), the IDs of the movies on the horror list.
Each of the following lines contains two space-separated integers , (), meaning the movie with ID is similar to the movie with ID (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.