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

杭电3172--Virtual Friends(并查集+map)

时间:2015-08-06 10:48:48      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

Virtual Friends

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6844    Accepted Submission(s): 1952


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

 

Input
Input 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).
 

 

Output
Whenever 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
 

 

Source
 

 

Recommend
chenrui   |   We have carefully selected several similar problems for you:  3038 3234 1829 3047 2818 
RE: 同一集合中节点个数;
    (并查集)转化思想 ,利用<map>把字符串转化为数字,还有他的存储功能;
 1 #include <map>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <iostream>
 5 using namespace std;
 6 char a[25], b[25];  int father[200020], num[200020];
 7 map<string, int> mmap;
 8 void init()
 9 {
10     for(int i = 1; i <200020; i++)
11     {
12         father[i] = i;
13         num[i]  =1;
14     }
15 }
16 int find(int a)
17 {
18     while(a != father[a])
19         a = father[a];
20     return a;
21 }
22 void mercy(int a, int b)  //加了Yh,,过了, 
23 {
24     int q = find(a);
25     int p = find(b);
26     if(q != p)
27     {
28         if(num[q] > num[p])
29         {
30             father[p] = q;
31             num[q] +=num[p];
32             printf("%d\n", num[q]);
33         }
34         else
35         {
36             father[q] = p;
37             num[p] += num[q]; 
38             printf("%d\n", num[p]);
39         }
40     }
41     else 
42          printf("%d\n", num[p]);
43 }
44 int main()
45 {
46     int m, t, num;
47     while(cin >> t)
48     {
49         while(t--)
50         {
51             
52             init();  num = 1;
53             mmap.clear();
54             scanf("%d", &m);
55             //for(int i = 1; i <= m; i++)
56             while(m--)
57             {
58                 scanf("%s %s", a, b);
59                 if(!mmap[a]) mmap[a] = num++;
60                 if(!mmap[b]) mmap[b] = num++;
61                 mercy(mmap[a], mmap[b]);
62             }
63         }
64     }
65     return 0;
66 }

 

 
 

杭电3172--Virtual Friends(并查集+map)

标签:

原文地址:http://www.cnblogs.com/fengshun/p/4707134.html

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