#C9935. Referral Counting

    ID: 54083 Type: Default 1000ms 256MiB

Referral Counting

Referral Counting

You are given a referral network represented as a set of referral pairs. Each referral pair is a relationship in the form of two space‐separated strings indicating that the first user referred the second user. A referral can be either direct or indirect. For a given target user, your task is to count the total number of users who have been referred directly or indirectly by that user.

For example, consider the referrals:

A B A C B D C E A F

If the target user is A, then the users B, C, D, E, and F are all either directly or indirectly referred by A, and the output should be 5.

The relationships may form a tree structure and it is guaranteed that there are no cycles in the referral chain.

The solution should read from standard input (stdin) and output the result to standard output (stdout).

All formulas (if any) must be represented in LaTeX format. Here, the answer is simply the count of nodes in the tree (excluding the root) reached via a breadth-first search (BFS) starting from the given user.

inputFormat

The input is given via standard input and consists of multiple lines:

  1. The first line contains an integer nn, which indicates the number of referral records.
  2. The next nn lines each contain two non-empty strings separated by a space. The first string is the referrer and the second string is the referred user.
  3. The last line contains a single string representing the target user whose total number of referrals (direct and indirect) is to be counted.

outputFormat

Output a single integer to standard output representing the total number of users referred (directly or indirectly) by the given target user.## sample

1
A B
A
1

</p>