#C291. Reachable Users in a Social Network
Reachable Users in a Social Network
Reachable Users in a Social Network
In this problem, you are given a representation of a social network and a starting user who posts an update. The social network is described as follows: each user is associated with a list of users that directly follow them. When a user posts an update, all of their direct followers will receive the update; then each follower will pass the update to their own followers, and so on. However, the original posting user should not be included in the final result.
Your task is to compute the list of all users who will receive the update directly or indirectly starting from the given user, and then output this list in alphabetical order (lexicographical order). The network may contain cycles. Use depth-first search (DFS) or any other graph traversal algorithm to solve the problem.
Note: In the description below, the social network can be thought of as a directed graph where an edge from user U to user V means that V follows U.
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains an integer n, the number of users in the network.
- The next n lines each describe a user and are formatted as follows: username k follower1 follower2 ... followerk, where username is the user's name, k is the number of followers, followed by k names of the followers.
- The last line contains a single string, the name of the user who posts the update.
It is guaranteed that every user listed in a follower list appears as a username in one of the n lines, though not necessarily in any specific order.
outputFormat
Print to standard output (stdout) the list of users who will receive the update, in alphabetical order, separated by a single space. If no user receives the update, output an empty line.
## sample7
alice 2 bob charlie
bob 1 david
charlie 2 david edward
david 1 frank
edward 0
frank 0
george 0
alice
bob charlie david edward frank