#K85442. Suspect Connection Chain
Suspect Connection Chain
Suspect Connection Chain
You are given the information of suspect associations in the form of a list of relationships. Each relationship consists of a suspect, the number of his/her associates, and the list of associates. The associations are bidirectional. Your task is to determine whether there is a chain of associations linking an initial suspect to a target suspect.
The connection exists if you can traverse through the associations from the initial suspect to the target suspect. Otherwise, there is no connection. Formally, if we denote the suspects by nodes and their associations as undirected edges, then you are to determine if there is a path between two given nodes.
In mathematical notation, using \( G=(V,E) \) where \( V \) is the set of suspects and \( E \) is the set of edges, determine if there exists a path \( v_0, v_1, \dots, v_k \) such that \( v_0 = \text{initial} \) and \( v_k = \text{target} \).
inputFormat
The input is read from standard input and has the following format:
initial target r suspect_1 m associate_1 associate_2 ... associate_m suspect_2 m associate_1 associate_2 ... associate_m ... suspect_r m associate_1 associate_2 ... associate_m
Here, initial
and target
are the names of the initial and target suspects respectively, r
is the number of relationships provided, and for each relationship, suspect_i
is the name of the suspect, m
is the number of their associates, followed by the list of associate names.
outputFormat
Output to the standard output a single line containing either YES
if there is a chain connecting the initial suspect to the target suspect, or NO
if there is not.
Alice Bob 3
Alice 2 Carol David
Carol 2 Eve Frank
David 1 Bob
YES