Step5.1.7 virtual friends,3172

Step5.1.7virtual friends,3172

              VirtualFriends

Time Limit: 4000/2000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others) 

Total Submission(s): 230 AcceptedSubmission(s): 77 

 

 

Problem Description

These days, you can do all sorts of thingsonline. For example, you can use various websites to make virtual friends. Forsome people, growing their social network (their friends, their friends'friends, their friends' friends' friends, and so on), has become an addictivehobby. Just as some people collect stamps, other people collect virtualfriends.

 

Your task is to observe the interactions onsuch a website and keep track of the size of each person's network.

 

Assume that every friendship is mutual. IfFred is Barney's friend, then Barney is also Fred's friend.

 

 

Input

Input file contains multiple test cases.

The first line of each case indicates thenumber of test friendship nest.

each friendship nest begins with a linecontaining an integer F, the number of friendships formed in this frindshipnest, which is no more than 100 000. Each of the following F lines contains thenames of two people who have just become friends, separated by a space. A nameis a string of 1 to 20 letters (uppercase or lowercase).

 

 

Output

Whenever a friendship is formed, print aline containing one integer, the number of people in the social network of thetwo people who have just become friends.

 

 

Sample Input

1

3

Fred Barney

Barney Betty

Betty Wilma

 

Sample Output

2

3

4

 

 

Source

University of Waterloo LocalContest 2008.09

 

 

Recommend

chenrui

 

题解:

并查集水题,这道题是要求如果是朋友就并到一起,每次输入朋友,则对应输出朋友圈的个数。简单的并查集,当两个人不在同一个朋友圈的时候,将两个朋友合并,就是让小的朋友圈的根改为大的根,其中根记录着整个朋友圈的人数,将根相加就可以了。

 

源代码:

#include <iostream>

#include <string>

#include <map>

#include <stdio.h>

 

using namespace std;

 

struct node

{

       intparents;

       intnum;

}fri[200005];

 

map<string,int> name;

 

void init(int n)

{

       n= n * 2 + 1;

       for(inti = 0;i < n;i++)

       {

              fri[i].parents= i;

              fri[i].num= 1;      

       }

}

 

int find_parent(int x)

{

       if(x== fri[x].parents)

              returnx;

 

       returnfri[x].parents = find_parent(fri[x].parents);

}

 

int Union(inta,int b)

{

       intx = find_parent(a);

       inty = find_parent(b);

 

       if(x== y)

       {

              intk = fri[x].parents;

              returnfri[k].num;

       }

      

 

       if(fri[x].num> fri[y].num)

       {

              fri[y].parents= x;

              fri[x].num+= fri[y].num;

              returnfri[x].num;

       }

       else

       {

              fri[x].parents= y;

              fri[y].num+= fri[x].num;

              returnfri[y].num;

       }

}

int main()

{

       intn,m;

       while(cin>> n)

       {

 

       for(intx = 0; x < n;x++)

       {    

              scanf("%d",&m);

              init(m);

              charname1[25],name2[25];

              intit = 0,i1,i2;

              for(inti = 0;i < m;i++)

              {

                     scanf("%s%s",name1,name2);

                     if(name.find(name1)== name.end())

                     {

                            name[name1]= it;

                            i1= it++;

                     }

                     else

                            i1= name[name1];

 

                     if(name.find(name2)== name.end())

                     {

                            name[name2]= it;

                            i2= it++;

                     }

                     else

                            i2= name[name2];

                    

                     printf("%d\n",Union(i1,i2));

              }

 

              name.clear();

             

       }

       }

 

       return0;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章