码迷,mamicode.com
首页 > 其他好文 > 详细

HDU - 3172 Virtual Friends

时间:2017-08-08 19:42:43      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:rtu   obb   ebs   har   name   als   add   span   output   

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), has become an addictive hobby. Just as some people collect stamps, other people collect virtual friends.

Your task is to observe the interactions on such a website and keep track of 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 each 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 two people who have just become friends, separated by a space. A name is a string of 1 to 20 letters (uppercase or lowercase).OutputWhenever a friendship is formed, print a line containing one integer, the number of people in the social network of the two people who have just become friends.Sample Input

1
3
Fred Barney
Barney Betty
Betty Wilma

Sample Output

2
3
4

标准并查集
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<map>
 5 
 6 using namespace std;
 7 
 8 int a[100005],b[100005];
 9 
10 int find(int x)
11 {
12     //cout<<a[x]<<endl;
13     if(a[x]!=x)
14         a[x]=find(a[x]);
15     return a[x];
16 }
17 
18 int suan(int x,int y)
19 {
20     int xx=find(x);
21     int yy=find(y);
22     //cout<<xx<<" "<<yy<<endl; 
23     if(xx==yy)
24         printf("%d\n",b[yy]);
25         else
26         {
27             a[xx]=yy;
28             //cout<<a[xx]<<endl;
29             b[yy]=b[yy]+b[xx];
30             printf("%d\n",b[yy]);
31         }
32 }
33 
34 int main()
35 {
36     int T;
37     while(~scanf("%d",&T))
38     {
39         while(T--)
40         {
41             int n;
42             map<string,int> m;
43             char x[25],y[25];
44             scanf("%d",&n);
45             for(int i=1;i<=100000;i++)
46             {
47                 a[i]=i;
48                 b[i]=1;
49             }
50             int s=1;
51             for(int i=0;i<n;i++)
52             {
53                 scanf("%s %s",x,y);
54                 if(m[x]==0)
55                     m[x]=s++;
56                 if(m[y]==0)
57                     m[y]=s++;
58             //    cout<<m[x]<<" "<<m[y]<<endl; 
59                 suan(m[x],m[y]);
60             }
61         }
62     }
63     
64     return 0;
65 }

 

HDU - 3172 Virtual Friends

标签:rtu   obb   ebs   har   name   als   add   span   output   

原文地址:http://www.cnblogs.com/xibeiw/p/7308293.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!