Virtual Friends (and check +map)

Source: Internet
Author: User

Virtual Friends

Time limit:4000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 6636 Accepted Submission (s): 1888


Problem Descriptionthese 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.

Inputinput file contains multiple test cases.
The first line of all case indicates the number of test friendship nest.
Each friendship Nest begins with a line containing an integer F, the number of friendships formed in this frindship nest, Which is no more than 100 000. Each of the following F lines contains the names of the people who has just become friends, separated by a space. A name is a string of 1 to letters (uppercase or lowercase).

Outputwhenever A friendship is formed, print a line containing one integer and the number of people in the social network of The people who has just become friends.

Sample Input1 3 Fred Barney Barney Betty Betty Wilma

Sample Output2 3 4 format Pit Daddy, the water is dead; stub node .... Code:
1#include <iostream>2#include <stdio.h>3#include <map>4#include <string>5 #defineMAX (x, y) x>y?x:y6 Const intmaxn=100005;7 using namespacestd;8 intFRI[MAXN],NUM,NOW[MAXN];9 voidInitial () {Ten      for(intI=1; i<=maxn;++i) fri[i]=i,now[i]=1; One } A intFindintx) { -     /*if (x==fri[x]) return x; - return Fri[x]=find (Fri[x]);*/ the     intR=x; -       while(R!=fri[r]) r=Fri[r]; -      intI=x,j; -       while(i!=r) j=fri[i],fri[i]=r,i=J; +      returnR; - } + voidMergeintXinty) { A     intF1,f2; atF1=find (x); f2=find (y); -     if(f1!=F2) { -FRI[F2]=F1;NOW[F1]+=NOW[F2];//NOW[F2]=NOW[F1]; -         //printf ("now[%d]=%d\n", F1,now[f1]); -     } - } in intMain () { -     intT,n; to     stringb; +      while(~SCANF ("%d",&T)) { -      while(t--) {map<string,int>m;initial (); num=0; thescanf"%d",&N); *      while(n--){ $Cin>>a>>B;Panax Notoginseng         if(M.find (A) ==m.end ()) num++,m[a]=num; -         if(M.find (B) ==m.end ()) num++,m[b]=num; the     //printf ("%d%d\n", m[a],m[b]); + merge (M[a],m[b]); Aprintf"%d\n", Now[find (M[a])); the     } +     } -         } $     return 0; $ } - /*1 - 7 the a B - a AWuyi a C the C G - G H Wu N N - h N About */

Virtual Friends (and check +map)

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.