题干:

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

题目大意:

有多个测试组T,每个测试组第一行n,接下来n行表示甲与乙认识,每给出一个关系,求出一共有多少个朋友。朋友的朋友也是朋友。

解题报告 :

先用map映射一下每个字符串对应一个序号(因为不然的话不好放到f数组num数组 的下标上)然后查询祖先节点那个集合的元素个数。

AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<string>using namespace std;
int n,m;
char s1[50],s2[50];
int f[200000 + 5];
int num[200000 + 5];
map<string , int> mp;int getf(int v) {return v==f[v]?v:f[v]=getf(f[v]);
}
void merge(int u,int v) {int t1=getf(u);int t2=getf(v);if(t1!=t2) {f[t2]=t1;num[t1]+=num[t2];//别写成t2了。。。。 }
}
void init() {for(int i = 1; i<=200000 + 5; i++) {f[i]=i;num[i]=1;}
}
int main()
{int top;while(~scanf("%d",&n) ) {while(n--) {mp.clear();top = 0;init();scanf("%d",&m);while(m--) {scanf("%s",s1);scanf("%s",s2);if(mp.find(s1) == mp.end() ) mp[s1] = ++top;if(mp.find(s2) == mp.end() ) mp[s2] = ++top;merge(mp[s1],mp[s2]);printf("%d\n",num[ getf( mp[s1] ) ] );}//一共top个人 }}return 0 ;
}

总结:

无。

【HDU - 3172】Virtual Friends(带权并查集--权为集合元素个数)相关推荐

  1. 【HDU - 3038】How Many Answers Are Wrong (带权并查集--权为区间和)

    题干:() TT and FF are ... friends. Uh... very very good friends -________-b FF is a bad boy, he is alw ...

  2. 树形结构 —— 并查集 —— 带权并查集

    [概述] 定义:带权并查集即是结点存有权值信息的并查集. 适用:当两个元素之间的关系可以量化,并且关系可以合并时,可以使用带权并查集来维护元素之间的关系. 权值:带权并查集每个元素的权通常描述其与并查 ...

  3. [HNOI2005]狡猾的商人 带权并查集

    给定m个区间和,问是否有矛盾 复习一下,带权并查集保存着这个元素与祖先的关系 在合并的过程中就像向量合并一样,可以画出x->fa[x],y->fa[y],x->y这样的图方便理解 这 ...

  4. How Many Answers Are Wrong HDU - 3038(带权并查集经典题,满满的都是注释)

    How Many Answers Are Wrong HDU - 3038  点击打开链接 题意:现在有n个数(你并不知道这n个数是什么),m次查询,每次查询给出u,v,w.表示从第u个数到第v个数的 ...

  5. Valentine's Day Round hdu 5176 The Experience of Love [好题 带权并查集 unsigned long long]

    传送门 The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  6. HDU 3047 Zjnu Stadium (带权并查集)

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=3047 题目: Problem Description In 12th Zhejiang College ...

  7. How Many Answers Are Wrong HDU - 3038(带权并查集)

    TT and FF are - friends. Uh- very very good friends -________-b FF is a bad boy, he is always wooing ...

  8. HDU 5176 The Experience of Love 带权并查集

    The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/O ...

  9. 带权并查集 HDU - 3047

    题意: 一圈座位有n个,给出m组序号之间的关系,比如,1 2 150 代表2号坐在1号位置序号+150,看m组数据有多少组冲突的. 思路: 带权并查集模板. #include<stdio.h&g ...

最新文章

  1. hdu1042 java_hdu 1431 素数回文
  2. scrapy带参数的命令
  3. 【数据竞赛】可能是全网特征工程实操最通透的...
  4. java学习(71):GUL边界布局管理器
  5. 从“void*”到指向非“void”的指针的转换要求显式类型转换错误
  6. New module changes in Go 1.16
  7. (17)System Verilog枚举类型enum详解
  8. 详细叙述ajax的详情,ajax的配置详情、ajax的调用解释、ajax的中文乱码和ajax的表单提交(内有实例)...
  9. iphone主屏幕动态壁纸_苹果11怎么设置动态壁纸?这个简单!只需这样操作
  10. js判断字符在另一个字符串中出现次数
  11. Mysql数据库乱码
  12. java验证码(采用struts2实现)
  13. value too long for type character varying(32)
  14. 上海证券交易所-债券品种介绍
  15. springboot启动报错Bean with name ‘xxxxService‘ has been injected into other beans
  16. 用SQL语句创建数据库和表
  17. 【HDOJ】4043 FXTZ II
  18. 最贴心的营销 微信营销,微酷为你的营销保驾护航
  19. 用DOS命令关闭端口
  20. 三面(技术+HR面试)网易,分享我的面试经验!(已拿offer)

热门文章

  1. html网页距离顶部50像素,HTML5 教程之CSS Padding(填充)
  2. Codeforces Round #736 (Div. 2)(B-C)
  3. linux rsync 常见错误,Linux rsync常见错误
  4. shell 获取家目录_一篇教会你写90%的shell脚本
  5. ssm框架里面前端拿HTML写,ssm框架引入Vue,声明式渲染,标签的href拼接字符串
  6. python支持gui编程_Python GUI编程完整示例
  7. linux内核编译后 make: 警告:检测到时钟错误.,系统时钟 make: 警告:检测到时钟错误。您的创建可能是不完整的。...
  8. hbase java admin_java连接hbase(一):Admin功能接口表管理
  9. a5d27 emmc启动 修改2
  10. DAHDI与Zaptel