Friend Network
InterviewTime limit3sMemory limit256 MB
Process friendships one by one and after each union print the size of the merged friend network containing the two users.
- Level
Medium6 of 10
- Topics
- Union-find, Hash map, Implementation
- Solved
- No attempts yet
Problem
Minhyuk loves making friends on social networking sites. Just as some people collect stamps, Minhyuk's hobby is collecting friends on social networks.
You are given the friendships of a site in the order they are formed. Each time a new friendship is formed, determine how many people belong to the friend network that contains the two people in that friendship.
A friend network is the set of people who can reach one another by moving only along friendships. In other words, two people belong to the same friend network if one can be reached from the other through a chain of friendships (a friend, a friend of a friend, and so on), even if they are not direct friends.
Input
The first line contains the number of test cases. The first line of each test case contains the number of friendships , which is at most 100,000. Each of the following lines contains one friendship, in the order the friendships are formed. A friendship consists of the IDs of two users; each ID is a string of at most 20 characters made up only of uppercase and lowercase English letters.
Output
Each time a friendship is formed, print on its own line the number of people in the friend network that contains the two people in that friendship.