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. 1
  2. 3
  3. Fred Barney
  4. Barney Betty
  5. Betty Wilma

Sample Output

  1. 2
  2. 3
  3. 4
  4.  
  5. 题意:找朋友,输出相应的朋友数量
    思路:就是并查集加个秩,在用map存一下,方便输出
  1. #include<cstdio>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<sstream>
  6. #include<cmath>
  7. #include<cstdlib>
  8. #include<queue>
  9. #include<map>
  10. #include<set>
  11. #include<vector>
  12. using namespace std;
  13. #define INF 0x3f3f3f3f
  14. #define eps 1e-10
  15. const int maxn=;
  16. map<string,int>p;
  17. int pre[maxn],ran[maxn],maxx;
  18. void init()
  19. {
  20. int i;
  21. for(i=;i<maxn;++i)
  22. {
  23. pre[i] = i;
  24. ran[i]=;
  25. }
  26. }
  27.  
  28. int find(int x)
  29. {
  30. if(x!=pre[x])
  31. {
  32. pre[x] = find(pre[x]);
  33. }
  34. return pre[x];
  35. }
  36.  
  37. void combine(int x,int y)
  38. {
  39. int fx = find(x);
  40. int fy = find(y);
  41. if(fx!=fy)
  42. {
  43. pre[fx] = fy;
  44. ran[fy] += ran[fx];
  45. }
  46. }
  47.  
  48. int main()
  49. {
  50.  
  51. int n,m;
  52. char name[],na[];
  53. while(scanf("%d",&n)!=EOF)
  54. {
  55. while(n--)
  56. {
  57. p.clear();
  58. init();
  59. int k=;
  60. scanf("%d",&m);
  61. for(int i=;i<m;i++)
  62. {
  63. scanf("%s %s",name,na);
  64. if(p.find(name)==p.end())
  65. p[name]=k++;
  66. if(p.find(na)==p.end())
  67. p[na]=k++;
  68. combine(p[name],p[na]);
  69. printf("%d\n",ran[find(p[na])]);
  70. }
  71. }
  72. }
  73. return ;
  74. }

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

  1. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  2. HDU 3926 并查集 图同构简单判断 STL

    给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...

  3. HDU 4496 并查集 逆向思维

    给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...

  4. HDU 1232 并查集/dfs

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...

  5. HDU 2860 并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...

  6. hdu 1198 (并查集 or dfs) Farm Irrigation

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...

  7. hdu 1598 (并查集加贪心) 速度与激情

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...

  8. hdu 4496(并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496. 思路:简单并查集应用,从后往前算就可以了. #include<iostream> ...

  9. 2015多校第6场 HDU 5361 并查集,最短路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5361 题意:有n个点1-n, 每个点到相邻点的距离是1,然后每个点可以通过花费c[i]的钱从i点走到距 ...

随机推荐

  1. Net Core2.0下使用Dapper

    Net Core2.0下使用Dapper 今天成功把.Net Framework下使用Dapper进行封装的ORM成功迁移到.Net Core 2.0上,在迁移的过程中也遇到一些很有意思的问题,值得和 ...

  2. 课程增加功能(java web)

    1.设计思想 先写类DBUtil用来连接数据库.在UserDaoImpl2类中写在数据库中添加课程表信息的方法.然后定义类Calss2来写保存超级课表数据:课程名称,任课教师,上课地点的属性及其get ...

  3. github 新建一个分支

    我能说今天在github上新建分支的时候懵逼了半天吗..为了下次不再懵逼,还是在这里记录一下吧.. 进入你的项目---code---Branch----点击那个倒三角-----你会发现一个输入框(这是 ...

  4. 《java学习三》jvm性能优化------jconsul

    利用jconsul检测线程死锁,    死锁的线程,会有   已锁定    三个字 visualVm                       也在jdk里 VisualVM 是一款免费的,集成了多 ...

  5. 网络编程之异步IO,rabbitMQ笔记

    对于网络并发编程而言,多线程与多进程算是最常见的需求场景了.毕竟网站开放就是想要更多的流量访问的. 回顾 回顾下之前学过的关于线程,进程和协程的知识点 IO密集型任务--用多线程更好计算密集型任务-- ...

  6. vue2.0:(六)、移动端像素border的实现和整合引入less文件

    知识点一.如何在手机上看我们制作的移动端页面. 正常我们在电脑上都是按如下图来制作手机页面的: 如果要在手机上面看就不能用localhost了.所以,进入命令行,输入ipconfig查看本地ip地址: ...

  7. 牛客NOIP提高组(二)题解

    心路历程 预计得分:100 + 40 + 30 = 170 实际得分:100 + 30 + 0 = 130 T2有一个部分分的数组没开够RE了. T3好像是思路有点小问题.. 思路没问题,实现的时候一 ...

  8. [20190618]日常学习记录(二)-flex属性及vue实战

    早上在看flex属性,总结一下它的优缺点 为什么使用flex, 她和浮动相比,代码更少.浮动要考虑左浮动右浮动,有时还要去清除浮动.flex一行代码就搞定了. 她更灵活,实现平均分配,根据内容大小分配 ...

  9. CRC检错技术原理

    一.题外话 说来惭愧,一开始是考虑写关于CRC检错技术更深层次数学原理的,然而在翻看<Basic Algebra>后,我果断放弃了这种不切实际的想法.个人觉得不是因为本人数学水平差或者能力 ...

  10. Linux下如何修改用户默认目录

      Linux下默认的用户目录一般为/home/xxx(root用户除外),有些时候我们可能需要修改这个目录,下面我就给大家分享2中修改的方法 工具/原料 Linux操作系统 方法/步骤 1 1.切换 ...