HDU 3172 Virtual Friends (map+并查集)

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. 
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3172

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

题解:带权并查集 可以记录每个集合中元素的个数

      普通并查集中增加sum数组在记录每个父节点的深度(即该集合中元素个数)

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <vector>
 6 #include <cstdlib>
 7 #include <iomanip>
 8 #include <cmath>
 9 #include <ctime>
10 #include <map>
11 #include <set>
12 #include <queue>
13 using namespace std;
14 #define lowbit(x) (x&(-x))
15 #define max(x,y) (x>y?x:y)
16 #define min(x,y) (x<y?x:y)
17 #define MAX 100000000000000000
18 #define MOD 1000000007
19 #define pi acos(-1.0)
20 #define ei exp(1)
21 #define PI 3.141592653589793238462
22 #define INF 0x3f3f3f3f3f
23 #define mem(a) (memset(a,0,sizeof(a)))
24 typedef long long ll;
25 ll gcd(ll a,ll b){
26     return b?gcd(b,a%b):a;
27 }
28 const int N=100100;
29 const int mod=1e9+7;
30 map<string,int> mp;
31 int sum[N],f[N];
32 void init()
33 {
34     for(int i=1;i<N;i++)
35         f[i]=i,sum[i]=1;
36 }
37 int Find(int x)
38 {
39     if(x!=f[x])
40         f[x]=Find(f[x]);
41     return f[x];
42 }
43 int Union(int a,int b)
44 {
45     int p=Find(a);
46     int q=Find(b);
47     if(p!=q){
48         f[p]=q;
49         sum[q]+=sum[p];
50     }
51     return sum[q];
52 }
53 int main()
54 {
55     int t,n;
56     while(scanf("%d",&t)!=EOF){
57         while(t--){
58             mp.clear();
59             scanf("%d",&n);
60             init();
61             int num=1;
62             char a[25],b[25];
63             while(n--){
64                 int u,v;
65                 scanf("%s%s",a,b);
66                 if(mp[a]==0) mp[a]=num++;
67                 if(mp[b]==0) mp[b]=num++;
68                 u=mp[a],v=mp[b];
69                 int k=Union(u,v);
70                 printf("%d\n",k);
71             }
72         }
73     }
74     return 0;
75 }
时间: 2024-10-10 14:23:54

HDU 3172 Virtual Friends (map+并查集)的相关文章

hdu 3172 Virtual Friends(并查集)

题目比较简单,但作为长久不写题之后的热身题还是不错的. 统计每组朋友的朋友圈的大小. 如果a和b是朋友,这个朋友圈的大小为2,如果b和c也是朋友,那么a和c也是朋友,此时这个朋友圈的大小为3. 输入t,表示接下来有t组数据. 每组数据有n组朋友关系. 接下来n行,每行一组朋友关系,然后输出这组朋友的朋友圈大小,即有多少朋友. 然后又是t组数据……(这点好坑)重复上述输入,直到数据结束. 因为最多有10^5个人,那么如果用线性字符串数组保存人名,肯定超时得不要不要的,所以要用map(每次操作时间复

hdu 3172 Virtual Friends (映射并查集)

Virtual Friends Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5491    Accepted Submission(s): 1519 Problem Description These days, you can do all sorts of things online. For example, you can u

HDU 3172 Virtual Friends 带权并查集 -秩

ll T; while(~scanf("%d",&T)){ while(T--) { = = ... 思路: 用秩合并,看了题解才发现 if(fx == fy)要输出当前集合的秩而不是0... #include <cstdio> #include <iostream> #include <algorithm> #include <string.h> #include <vector> #include <map&

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

HDU 3635 延缓更新的并查集

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

HDU 3461 Code Lock(并查集的应用+快速幂)

* 65536kb,只能开到1.76*10^7大小的数组.而题目的N取到了10^7,我开始做的时候没注意,用了按秩合并,uset+rank达到了2*10^7所以MLE,所以貌似不能用按秩合并. 其实路径压缩也可以不用.............  题目的大意: 一个密码锁上有编号为1到N的N个字母,每个字母可以取26个小写英文字母中的一个.再给你M个区间[L,M],表示该区间的字母可以一起同步"增加"(从'a'变为'b'为增1,'z'增1为'a').假如一组密码按照给定的区间进行有限

HDU 1558 Segment set (并查集+线段非规范相交)

题目链接 题意 : 如果两个线段相交就属于同一集合,查询某条线段所属集合有多少线段,输出. 思路 : 先判断与其他线段是否相交,然后合并. 1 //1558 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <cmath> 6 #define eps 1e-8 7 #define zero(x) (((x) > 0 ? (x) : (-x)) < e

HDU 3172 Virtual Friends(带权并查集)

题目地址:HDU 3172 带权并查集水题.每次合并的时候维护一下权值.注意坑爹的输入.. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #inclu

hdu 3172 Virtual Friends

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3172 并查集的运用... 1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstdio> 5 #include<map> 6 using std::map; 7 using std::string; 8 const int Max_N = 100010