带权并查集:HDU3172-Virtual Friends
Virtual Friends
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9179 Accepted Submission(s): 2654
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
解题心得
- 这个是一个带权并查集,其实也没有什么新奇的,主要就是需要记录每一个string的根,这个可以用一个map来映射一下,将每一个string映射成一个数字,然后用映射的数字来进行合并就行了。
- 这个题需要输出的东西是当前建立关系的网络里面的人数,所以需要用数组来记录一下当前根的人数就可以了。
- 其实这个题主要处理的就是一个关于string的问题,当需要将一个string用一个数子来代替的时候可以很流畅的想到map 的映射,具体的映射操作看代码。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
int father[maxn*2],ans,num[maxn*2];
map <string,int> mp;//string映射成一个number
int Find(int x)
{
if(father[x] == x)
return x;
else
return father[x] = Find(father[x]);
}
void _merge(int a,int b)
{
int fa = Find(a);
int fb = Find(b);
if(fa != fb)
{
father[fa] = fb;
num[fb] += num[fa];//合并的时候要将不同根的网络的人数也合并起来
}
printf("%d\n",num[fb]);
}
int main()
{
int t;
while(scanf("%d",&t) != EOF)
{
while(t--)
{
int n;
mp.clear();
ans = 1;
scanf("%d",&n);
int cnt = 0;
for(int i=0; i<=n; i++)
{
father[i] = i;
num[i] = 1;
}
for(int i=0; i<n; i++)
{
string s1,s2;
cin>>s1>>s2;
//将string用一个数字来表示
if(mp.find(s1) == mp.end())
mp[s1] = cnt++;
if(mp.find(s2) == mp.end())
mp[s2] = cnt++;
_merge(mp[s1],mp[s2]);
}
}
}
}
带权并查集:HDU3172-Virtual Friends的更多相关文章
- HDU_3172_带权并查集
Virtual Friends Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- POJ 1703 Find them, Catch them(带权并查集)
传送门 Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42463 Accep ...
- [NOIP摸你赛]Hzwer的陨石(带权并查集)
题目描述: 经过不懈的努力,Hzwer召唤了很多陨石.已知Hzwer的地图上共有n个区域,且一开始的时候第i个陨石掉在了第i个区域.有电力喷射背包的ndsf很自豪,他认为搬陨石很容易,所以他将一些区域 ...
- poj1417 带权并查集 + 背包 + 记录路径
True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2713 Accepted: 868 Descrip ...
- poj1984 带权并查集(向量处理)
Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5939 Accepted: 2 ...
- 【BZOJ-4690】Never Wait For Weights 带权并查集
4690: Never Wait for Weights Time Limit: 15 Sec Memory Limit: 256 MBSubmit: 88 Solved: 41[Submit][ ...
- hdu3038(带权并查集)
题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 题意: n表示有一个长度为n的数组, 接下来有m行形如x, y, d的输入, 表示 ...
- 洛谷OJ P1196 银河英雄传说(带权并查集)
题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦 创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山 ...
- poj1984 带权并查集
题意:有多个点,在平面上位于坐标点上,给出一些关系,表示某个点在某个点的正东/西/南/北方向多少距离,然后给出一系列询问,表示在第几个关系给出后询问某两点的曼哈顿距离,或者未知则输出-1. 只要在元素 ...
随机推荐
- B - Median Pyramid Easy 构造题
B - Median Pyramid Easy Time limit : 2sec / Memory limit : 256MB Score : 400 points Problem Statemen ...
- opencv 形态学膨胀和腐蚀以及开运算和闭运算
- oo总结
架构设计 第一次作业 需求分析 这次作业是针对类中的一些元素,如属性,操作,继承,实现等查询,所以这次的架构我们的第一感觉,按照正常的结构在类中存属性操作,继承的父类和实现的接口等. 具体功能 为了实 ...
- Hive基础(1)
Hive基础(1) Hive的HQL(2) 1. Hive并不是分布式的,它独立于机器之外,类似于Hadoop的客户端. 2. 元数据和数据的区别,前者如表名.列名.字段名等. 3. Hive的三种安 ...
- Flash图表FusionCharts如何自定义图表导出菜单或界面
FusionCharts的导出组件界面有两种模式: Compact Mode: 用于保存单张图片,每一个单独的导出组件实例都代表单独的图表.在这种模式下,只有一个按钮和标题是可见的. Full Mod ...
- 真正的S2b其实是S2b2c
本文转自阿里参谋长曾鸣:真正的S2b其实是S2b2c! 在<在未来五年,S2b是最有可能领先的商业模式>这篇文章发表之后,曾鸣书院收到了非常多的反馈,看到很多实践和思考. 在这篇文章中,曾 ...
- Linux上通过MySQL命令访问MySQL数据库时常见问题汇总
Linux上通过mysql命令访问MySQL数据库时常见问题汇总 1)创建登录账号 #创建用户并授权 #允许本地访问 create user 'test'@'localhost' identified ...
- Python+selenium之selenium Grid2
利用selenium grid2 keyi可以在不同的主机上建立主节点(hub)和分支节点(node),可以使主节点上的测试用例在不同的分支节点上运行.对不同的节点来说,可以搭建不同的测试环境(操作系 ...
- 香港城市大学:全球首创3D打印微型机器人技术 有望作治疗癌症用途
香港城市大学(香港城大)的研究团队开发出了全球首创以磁力控制的3D打印微型机器人,该微型机器人技术能做到在生物体内精准运载细胞到指定的位置.新研发的微型机器人有望应用在治疗癌症的靶向治疗,并为细胞层面 ...
- python 函数内使用全局变量
x = def change_global(): global x x = x + change_global() print(x) result: 2