Virtual Friends
These days, you can do all sorts of things online. For example, you can use various websites to make virtual friends. For some people, growing their social network (their friends, their friends ' friends, their friends ' friends ' friends, and So on), have become an addictive hobby. Just as some people collect stamps, other people collect virtual friends.
Your task is to observe the interactions in such a website and keep track of the the size of each person ' s network.
Assume that every friendship is mutual. If Fred is Barney's friend, then Barney is also Fred's friend.
Input
The first line of input contains one integer specifying the number of the test cases to follow. Each test case begins with a line containing an integerFF, the number of friendships formed, which is no more thanabout 100000. Each of the following FF lines contains the names of both people who has just become friends, separated by a space. A name is a string of 1 to letters (uppercase or lowercase).
Output
Whenever a friendship is formed, print a line containing one integer, the number of people in the social network of the TW O people who has just become friends.
Sample Input 1 |
Sample Output 1 |
13Fred Barneybarney Bettybetty Wilma |
234 |
Test instructions
Friends are friends, ask the current people have a few friends
Ideas
and check Set
Code
#include <bits/stdc++.h>using namespacestd;intset[200010];intnum[200010];intFindroot (intx) {if(x =Set[x])returnx; returnSET[X] =Findroot (set[x]);}voidUnion (intXinty) {x=Findroot (x); Y=Findroot (y); if(X! =y) {set[x]=y; Num[y]+=Num[x]; } cout<< Num[y] <<Endl;}intMain () {intT; CIN>>T; while(t--) { intN; CIN>>N; Map<string,int>Map; for(inti =0; I <=2N ++i) {Set[i]=i; Num[i]=1; } stringA, B; for(inti =0, j =1; I < n; ++i) {cin>> A >>B; if(! Map[a]) Map[a] = j + +; if(! MAP[B]) Map[b] = j + +; Union (Map[a], map[b]); } }}
Kattis-virtual Friends