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点走到距 ...
随机推荐
- net core WebApi 使用Swagger
Asp.net core WebApi 使用Swagger生成帮助页 最近我们团队一直进行.net core的转型,web开发向着前后端分离的技术架构演进,我们后台主要是采用了asp.net core ...
- Kestrel服务器启动并处理Http请求
从Hosting开始 知识点: 1.Kestrel服务器启动并处理Http请求的过程. 2.Startup的作用. 源码飘香: 总结: asp.net core将web开发拆分为多个独立的组件,大 ...
- 关于FutureTask的探索
之前关于Java线程的时候,都是通过实现Runnable接口或者是实现Callable接口,前者交给Thread去run,后者submit到一个ExecutorService去执行. 然后知道了还有个 ...
- CQRS之旅——前言(翻译)
探索CQRS和Event Sourcing 本项目聚焦在使用命令和查询分离模式和事件溯源(CQRS+Event Sourcing)构建一个具有高扩展,高可用和高维护性的应用程序. 本项目定位为一个学习 ...
- 09SpringAopAdvice
Spring原生的经典模式 实现 AOP 通知: 前置通知:在目标方法执行之前执行,不能改变方法的执行流程和结果! 实现 MethodBeforeAdvice接口! 后置通知:在目标方法执行之后执行, ...
- CSS filter 属性
filter 将模糊或者颜色偏移等图像效果用于元素,通常用于调整图像,背景和边框的渲染 css 标准中已内置一些预定义效果的函数,也可通过url使用SVG滤镜 语法 /* URL to SVG fil ...
- 从零开始的Lua宅[1]:编译Lua解释器
Lua是一门神奇的脚本语言,游戏宅必备,懒人必备.Lua差多不是学起来用起来最简单的语言了,以至于简单到自身就是文档,自身就是配置文件.但是Lua的运行效率却是众多脚本中非常高的,据说仅次于V8爹下的 ...
- 参考消息 Android 读报
<参考消息>是新华通讯社主办,参考消息报社编辑出版的日报,创刊于1931年,历史长达80年.<参考消息>每天及时选载世界各国(地区)通讯社.报刊及因特网上的最新消息.评论的精华 ...
- pod install Pull is not possible because you have unmerged files.
http://stackoverflow.com/questions/21474536/podfile-gives-an-error-on-install A bug was found in lib ...
- Android(java)学习笔记125:保存数据到SD卡 (附加:保存数据到内存)
1. 如果我们要想读写数据到SD卡中,首先必须知道SD的路径: File file = new File(Environment.getExternalStorageDirectory()," ...