Virtual Friends HDU - 3172 (并查集+秩+map)
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 题意:找朋友,输出相应的朋友数量
思路:就是并查集加个秩,在用map存一下,方便输出
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<map>
#include<set>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
const int maxn=;
map<string,int>p;
int pre[maxn],ran[maxn],maxx;
void init()
{
int i;
for(i=;i<maxn;++i)
{
pre[i] = i;
ran[i]=;
}
} int find(int x)
{
if(x!=pre[x])
{
pre[x] = find(pre[x]);
}
return pre[x];
} void combine(int x,int y)
{
int fx = find(x);
int fy = find(y);
if(fx!=fy)
{
pre[fx] = fy;
ran[fy] += ran[fx];
}
} int main()
{ int n,m;
char name[],na[];
while(scanf("%d",&n)!=EOF)
{
while(n--)
{
p.clear();
init();
int k=;
scanf("%d",&m);
for(int i=;i<m;i++)
{
scanf("%s %s",name,na);
if(p.find(name)==p.end())
p[name]=k++;
if(p.find(na)==p.end())
p[na]=k++;
combine(p[name],p[na]);
printf("%d\n",ran[find(p[na])]);
}
}
}
return ;
}
Virtual Friends HDU - 3172 (并查集+秩+map)的更多相关文章
- hdu 4514 并查集+树形dp
湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- HDU 3926 并查集 图同构简单判断 STL
给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...
- HDU 4496 并查集 逆向思维
给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...
- HDU 1232 并查集/dfs
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...
- HDU 2860 并查集
http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...
- hdu 1198 (并查集 or dfs) Farm Irrigation
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...
- hdu 1598 (并查集加贪心) 速度与激情
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...
- hdu 4496(并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496. 思路:简单并查集应用,从后往前算就可以了. #include<iostream> ...
- 2015多校第6场 HDU 5361 并查集,最短路
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5361 题意:有n个点1-n, 每个点到相邻点的距离是1,然后每个点可以通过花费c[i]的钱从i点走到距 ...
随机推荐
- awk单引号处理
awk中使用单引号,常规字符串,'\''即可,但如果像下面在$4变量用单引号,则还需要加上双引号才行. cat 2.txt | awk '{ print $1, $2, $3, "'\''& ...
- java jmap
jmap : 命令用于生成堆转储快照.它还可以查询finalize执行队列.Java堆和永久代的详细信息,如空间使用率.当前用的是哪种收集器等. 命令格式: jmap [option] vmid op ...
- nodejs 实践:express 最佳实践(三) express 解析
nodejs 实践:express 最佳实践(三) express 解析 nodejs 发展很快,从 npm 上面的包托管数量就可以看出来.不过从另一方面来看,也是反映了 nodejs 的基础不稳固, ...
- 关于office转换成pdf组件服务中的DCOM配置问题
在开始->运行 中录入“dcomcnfg” 单击“确定”后弹出“组件服务”窗口 依次选择“组件服务”->“计算机”->“我的电脑”->“DCOM配置” 在“DCOM配置”下找到 ...
- Tomcat 下载及配置
1.下载 下载地址:http://tomcat.apache.org/ 进去后下拉到底部 2.解压 Tomcat不需要安装,直接解压即可.解压后会得到这么一个文件夹 3.在MyEclipse中配置To ...
- MySQL获取某个时间范围内的数据 TO_DAYS(date)函数
1.利用to_days函数查询今天的数据: select * from 表名 where to_days(时间字段名) = to_days(now()); to_days函数:返回从0000年(公元1 ...
- VMware虚拟机中red hat linux ping不通宿主物理主机原因
在VMware Workstation中安装了red hat enterprise linux系统,网络使用“桥接”形式,最后出现在Windows下能够Ping通虚拟主机,而虚拟主机Ping不通Win ...
- 掌握 Azure 的注册、帐户和订阅管理 Azure 上云须知
计划使用由世纪互联运营的 Microsoft Azure 的用户,可通过下列流程注册开通并购买所需 Azure 服务:信息获取 > 试用 > 购买 > 账户/订阅管理 > 支付 ...
- Python中的绝对路径和相对路径
大牛们应该对路径都很了解了,这篇文章主要给像我这样的入门小白普及常识用的,啊哈 下面的路径介绍针对windows,其他平台的暂时不是很了解. 在编写的py文件中打开文件的时候经常见到下面其中路径的表达 ...
- 关于SQL Server索引密度的知识
文章主要描述的是SQL Server索引密度(Index Densities),当一个查询的SARG 的值直到查询运行时才得以知晓,或是SARG是一个关于索引的多列时,SQL Server才使用为索引 ...