#K2251. Maximum Unique Guests Invitation

    ID: 24695 Type: Default 1000ms 256MiB

Maximum Unique Guests Invitation

Maximum Unique Guests Invitation

You are organizing a party with n guests numbered from 1 to n. Each guest may invite some other guests directly. The invitation process is acyclic and recursive: if guest i invites guest j, then guest j will in turn invite their own friends, and so on.

You are given an integer n and n lines describing the friend lists. The ith line contains space-separated integers, representing the guests that guest i will invite directly. Some lines may be empty, meaning that the guest does not invite anyone directly.

Your task is to determine the total number of unique guests that will eventually receive an invitation. Formally, let \(L_i\) be the list of friends guest \(i\) invites. Starting from each guest \(i\) and following the invitation chain, compute the union of all reached guests and output its size.

inputFormat

The input is read from standard input (stdin) and has the following format:

n
friends_list_line_1
friends_list_line_2
...
friends_list_line_n

Where:

  • n is an integer representing the number of guests.
  • Each of the next n lines contains zero or more space-separated integers denoting the guest numbers that the corresponding guest invites directly. An empty line indicates no direct invitations.

outputFormat

Output a single integer to standard output (stdout) — the total number of unique guests that are invited either directly or indirectly.

## sample
3
2 3
1
1
3