Kattis-virtual Friends

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.