Virtual Friends

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

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 3047 2818 2473 
 
 //875MS     1828K    1063B     G++
/* 题意:
给出n个朋友关系,朋友的朋友也是朋友,求每组关系的圈子的人数 并查集+STL:
这种做法有点小暴力,不过没想到更好的方法。
要使用并查集的路径压缩,每次更新把全部点更新到一个点上,
然后更新该改点的人数就行了。然后直接输出该点的人数。 */
#include<iostream>
#include<string>
#include<map>
using namespace std;
int set[];
int num[];
int find(int x)
{
int r=x;
while(set[r]!=r)
r=set[r];
int i=x;
while(i!=r){ //路径压缩
int j=set[i];
set[i]=r;
i=j;
}
return r;
}
void merge(int a,int b)
{
int x=find(a);
int y=find(b);
if(x!=y){
set[y]=x;
num[x]+=num[y];
printf("%d\n",num[x]);
}else{
printf("%d\n",num[x]);
}
}
int main(void)
{
int t,n;
char a[],b[];
while(scanf("%d",&t)!=EOF)
while(t--)
{
map<string,int>M;
M.clear();
for(int i=;i<=;i++){
set[i]=i;num[i]=;
}
scanf("%d",&n);
int m=;
for(int i=;i<n;i++){
scanf("%s%s",a,b);
if(M[a]==) M[a]=m++;
if(M[b]==) M[b]=m++;
merge(M[a],M[b]);
}
}
return ;
} /* 6
3
1 2
2 3
3 4
5
1 2
2 3
3 4
4 4
5 1 */

hdu 3172 Virtual Friends (并查集)的更多相关文章

  1. HDU 3172 Virtual Friends(并用正确的设置检查)

    职务地址:pid=3172">HDU 3172 带权并查集水题.每次合并的时候维护一下权值.注意坑爹的输入. . 代码例如以下: #include <iostream> # ...

  2. HDU 1811 拓扑排序 并查集

    有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...

  3. hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点)

    hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点) 题意: 给一张无向连通图,有两种操作 1 u v 加一条边(u,v) 2 u v 计算u到v路径上桥的个数 ...

  4. HDU 3172 Virtual Friends (map+并查集)

    These days, you can do all sorts of things online. For example, you can use various websites to make ...

  5. hdu 3172 Virtual Friends(并查集)University of Waterloo Local Contest 2008.09

    题目比较简单,但作为长久不写题之后的热身题还是不错的. 统计每组朋友的朋友圈的大小. 如果a和b是朋友,这个朋友圈的大小为2,如果b和c也是朋友,那么a和c也是朋友,此时这个朋友圈的大小为3. 输入t ...

  6. hdu 3172 Virtual Friends(并查集,字典树)

    题意:人与人交友构成关系网,两个人交友,相当于两个朋友圈的合并,问每个出两人,他们目前所在的关系网中的人数. 分析:用并查集,其实就是求每个集合当前的人数.对于人名的处理用到了字典树. 注意:1.题目 ...

  7. hdu 3172 Virtual Friends

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3172 并查集的运用... #include<algorithm> #include< ...

  8. <hdu - 1232> 畅通工程 并查集问题 (注意中的细节)

    本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232  结题思路:因为题目是汉语的,那我就不解释题意了,要求的是最少建设的道路,我们可以用并查集来做这 ...

  9. HDU 5441 Travel(并查集+统计节点个数)

    http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给出一个图,每条边有一个距离,现在有多个询问,每个询问有一个距离值d,对于每一个询问,计算出有多少点 ...

随机推荐

  1. Linux centos7 安装python3 及 GCC

    1.用wget下载python源码 PYTHON下载 找适合自己的版本,我下载的是3.7.2 2.用tar命令解压下载的文件 tar -zxvf Python-3.7.2.tgz 3.进入目录解压后的 ...

  2. Linux分享笔记:系统状态检测命令小结

    作为一名合格的运维人员,要能很好地了解Linux服务器,要能熟练查看Linux系统的运行状态.以下是常用到的Linux系统状态检测命令. 1. ifconfig:用于获取网卡配置与网络状态等信息.通常 ...

  3. 安装docker和更改docker镜像下载目录

    centos6.x系列: yum install http://mirrors.yun-idc.com/epel/6/i386/epel-release-6-8.noarch.rpm yum inst ...

  4. Linux Shell 与Linux常用命令

    Linux的人际交互分为图形界面方式和命令行方式. Linux本身只是一个操作系统内核,而由X Window图形用户接口为Linux提供图形用户界面功能.可以把X Window理解为一个运行在Linu ...

  5. Qt——菜单栏、工具栏、状态栏

    1.菜单栏 菜单栏的意义是将可点击触发最终事件的集中在一起,所以菜单栏中是QAction 添加菜单栏是QMainWindow的行为 QMenubar *menubar = this->addMe ...

  6. stm32+lwip(四):网页服务器测试

    我是卓波,很高兴你来看我的博客. 系列文章: stm32+lwip(一):使用STM32CubeMX生成项目 stm32+lwip(二):UDP测试 stm32+lwip(三):TCP测试 stm32 ...

  7. 42-EF Core Migration

    1-常用命令 1-由于2.1版本有点不一样,不会自动创建ApplicationUser类,发现合并没效果.暂时略 增加一个字段 E:\coding\netcore\IdentitySample> ...

  8. java使用urlConnection抓取部分数据乱码

    使用urlconnection做抓取的同学应该一开始都是使用这个吧.OK回到正题来..... 在内容己有中文.英文己正常显示,仍然会有部分中文或英文出现乱码,这是为什么呢?这个问题一直在心里盘旋... ...

  9. JDBC 的使用

    使用 MariaDB,JDBC 所有操作全部使用预处理 SQL 的基本类型与 Java 类型的对应关系 CHAR(N) - String VARCHAR(N) - String BOOLEN - bo ...

  10. [Windows]_[C/C++]_[如何调试子进程]

    场景 1.VC++ 的程序A在启动程序C时, 如果需要调试程序C的话一般有两种, 一种是通过菜单 调试->附加到进程的方式来调试程序, 缺点就是这个进程必须先启动, 但是一启动的话有可能就执行了 ...