A health team tracks n people, numbered 1 through n, and m recorded contacts. Each contact links two people who met, and contact is mutual. A contact cluster is a maximal set of people all reachable from one another through chains of contacts.
Given a query person q, report how many people belong to q's contact cluster, counting q itself.
n and m.m lines: two integers u and v (1-indexed), a recorded contact between u and v.q (1-indexed), the query person.A single integer: the number of people in q's contact cluster.
Example 1
Input
6 4 1 2 2 3 4 5 5 6 2
Expected
3
Explanation
Person 2 is in the cluster {1,2,3}, which has 3 people.
Example 2
Input
5 2 1 2 3 4 5
Expected
1
Explanation
Person 5 has no recorded contacts, so their cluster is just themselves: 1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →