Neighborhoods in Graphs
InterviewTime limit1sMemory limit256 MB
Count the distinct vertices at distance 1 or 2 from a query vertex in an undirected graph, excluding the vertex itself.
Problem
Model co-conspirators as an undirected graph: each vertex is a super-villain, and each edge joins two villains who plotted together. For a given vertex vx, count how many other vertices lie within distance 1 or 2. Do not count vx itself.
Input
The first line contains T, the number of test cases. Each test case is one line with n, e, then 2e vertex names describing edges, and finally the query vertex vx. (n < 100, e < 1000)
Output
For each test case, print The number of supervillains in 2-hop neighborhood of vx is C, where C is the size of the 2-hop neighborhood.