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

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

University of Waterloo Local Contest 2008.09

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 }
时间: 2024-10-11 08:44:46

杭电3172--Virtual Friends(并查集+map)的相关文章

杭电1232(图—并查集)

畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 30798    Accepted Submission(s): 16193 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相

hdu 3172 Virtual Friends (并查集 + 字典树)

题目: 链接:点击打开链接 题意: 输入n,给出n行数据,每行有两个字符串,输出关系网络中朋友的个数,n行. 思路: 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; const int N = 22; const int M = 200020; struct node { int c; node *chil

杭电3635-Dragon Balls(并查集)

Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4582    Accepted Submission(s): 1747 Problem Description Five hundred years later, the number of dragon balls will increase unexpect

杭电ACM1269——迷宫城堡~~并查集

这题,可以用并查集来解决.需要用到两个数组来判断从i到j以及j到i的是否属于同一个并查集. 下面的是AC的代码: #include <iostream> #include <cstdio> using namespace std; int par[2][10005]; int n, m; int finds(int x, int i) { if(x == par[i][x]) return x; else return par[i][x] = finds(par[i][x], i)

油菜花王国——杭电校赛(并查集)

油菜花王国 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1609    Accepted Submission(s): 411 Problem Description 程序设计竞赛即将到来,作为学校ACM集训队主力,小明训练一直很努力.今天天气不错,教练也心情大好,破例给各位队员放假一天,小明就骑着自己的小电驴到郊外踏青去了. 出城不

More is better(杭电1856)(并查集)

More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others) Total Submission(s): 15434    Accepted Submission(s): 5667 Problem Description Mr Wang wants some boys to help him with a project. Because the projec

杭电 5326 Work (并查集求子结点为k的结点数)

Description It’s an interesting experience to move from ICPC to work, end my college life and start a brand new journey in company. As is known to all, every stuff in a company has a title, everyone except the boss has a direct leader, and all the re

杭电 4707 pet(并查集求元素大于k的集合)

Description One day, Lin Ji wake up in the morning and found that his pethamster escaped. He searched in the room but didn’t find the hamster. He tried to use some cheese to trap the hamster. He put the cheese trap in his room and waited for three da

杭电 oj3047~带权并查集

Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2947    Accepted Submission(s): 1132 Problem Description In 12th Zhejiang College Students Games 2007, there was a new stadium built