Social Networking Application
InterviewTime limit1sMemory limit128 MB
Given a friendship graph, answer queries asking whether two users lie in the same connected component.
- Level
Easy3 of 10
- Topics
- Union-find, Graph, DFS, Implementation
- Solved
- No attempts yet
Problem
Since childhood, Sanggeun has shown remarkable talent for computer programming, and he has always wanted to build a social networking website. After three years of carefully studying various services, he now wants to build his own new social networking application.
Users sign up for the application and then add people they know in real life as friends. From this friendship information, a friendship graph can be built.
The most important feature of the application is this: when one user visits another user's page, it shows a path connecting the two people in the friendship graph. If there is no such path, nothing is shown.
As the service grew popular and the number of users increased, this feature became slower and slower. The reason is that, when there is no path between two people, the program spends far too long searching the whole graph trying to find one. So Sanggeun wants to precompute whether a path between two people exists.
You are given the number of users and their friendships. For each given pair of people, determine whether they are connected in the friendship graph (that is, whether a path between them exists).
Input
The input consists of several test cases. The first line of the input contains the number of test cases ().
The first line of each test case contains the number of users (). The second line contains the number of friendships (). Each of the next lines contains two integers and (), meaning that users and are friends. The next line contains the number of pairs to check (). Each of the next lines contains two integers and describing a pair to check.
Output
For each test case, first print Scenario i:, where is the test case number starting from . Then, for each given pair, print if there is a path connecting the two people and otherwise, one per line.
Print one blank line between the outputs of consecutive test cases.