ZOJ:2833 Friendship(并查集+哈希)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2833
A friend is like a flower,
a rose to be exact,
Or maybe like a brand new gate
that never comes unlatched.
A friend is like an owl,
both beautiful and wise.
Or perhaps a friend is like a ghost,
whose spirit never dies.
A friend is like a heart that goes
strong until the end.
Where would we be in this world
if we didn't have a friend?
- By Emma Guest
Now you've grown up, it's time to make friends. The friends you make
in university are the friends you make for life. You will be proud if
you have many friends.
Input
There are multiple test cases for this problem.
Each test case starts with a line containing two integers N, M (1 <= N
<= 100'000, 1 <= M <= 200'000), representing that there are
totally N persons (indexed from 1 to N) and M operations, then M lines
with the form "M a b" (without quotation) or "Q a" (without quotation)
follow. The operation "M a b" means that person a and b make friends
with each other, though they may be already friends, while "Q a" means a
query operation.
Friendship is transitivity, which means if a and b, b and c are friends
then a and c are also friends. In the initial, you have no friends
except yourself, when you are freshman, you know nobody, right? So in
such case you have only one friend.
Output
For each test case, output "Case #:" first where "#" is the number of
the case which starts from 1, then for each query operation "Q a",
output a single line with the number of person a's friends.
Separate two consecutive test cases with a blank line, but Do NOT output an extra blank line after the last one.
Sample Input
3 5
M 1 2
Q 1
Q 3
M 2 3
Q 2
5 10
M 3 2
Q 4
M 1 2
Q 4
M 3 2
Q 1
M 3 1
Q 5
M 4 2
Q 4
Sample Output
Case 1:
2
1
3
Case 2:
1
1
3
1
4
Notes
This problem has huge input and output data, please use 'scanf()' and
'printf()' instead of 'cin' and 'cout' to avoid time limit exceed.
题解: 格式错误了N次,以前的题都没卡这么死,这次最后一次输入没空行卡的很死,第一次学会这么写的方式,首先定义一个标记变量,还有手残了N次,另外由于M的值很大,如果不处理,果断超时,所以用到了哈希。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
int bin[],sum[];
int n,m;
int findx(int x)
{
int r=x;
while(r!=bin[r])
r=bin[r];
int k,j=x;
while(j!=r)
{
k=bin[j];
bin[j]=r;
j=k;
}
return r;
}
void merge(int x,int y)
{
int fx=findx(x);
int fy=findx(y);
if(fx!=fy)
{
bin[fy]=fx;
sum[fx]+=sum[fy];//根节点加上新加入集合的数目
}
}
int main()
{
char a[];
int flag=,K=,x,y;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(flag==)
{
printf("\n");
}
flag=;
printf("Case %d:\n",++K);
for(int i=; i<=n; i++)
{
bin[i]=i;
sum[i]=;
}
while(m--)
{
scanf("%s",a);
if(a[]=='M')
{
scanf("%d%d",&x,&y);
if(x!=y)
{
merge(x,y);
}
}
else if(a[]=='Q')
{
scanf("%d",&x);
printf("%d\n",sum[findx(x)]);//这里我竟写错了,该剁手啊
}
}
}
return ;
}
ZOJ:2833 Friendship(并查集+哈希)的更多相关文章
- 栈&队列&并查集&哈希表(julyedu网课整理)
date: 2018-11-25 08:31:30 updated: 2018-11-25 08:31:30 栈&队列&并查集&哈希表(julyedu网课整理) 栈和队列 1. ...
- zoj 2833 friendship
zoj 2833这次真的很顺利了..居然是因为数组的大小没有符合要求,瞎折腾了很久..没有注意到要求范围,真是该死! 想法很简单,就是定义一个父结点数组,下标 i 表示这个元素,初始化为 -1表示 这 ...
- ZOJ - 3261 逆向并查集
思路:很巧妙的解法.如果按照常规一边读入,一边合并并查集,删边实在没办法做. 首先读入所有的操作,把所有不会被删除的边加入并查集,然后从最后一个操作开始逆向操作,当遇到删边操作,就直接把这条边加入并查 ...
- zoj 3761(并查集+搜索)
题意:在一个平面上,有若干个球,给出球的坐标,每次可以将一个球朝另一个球打过去(只有上下左右),碰到下一个球之后原先的球停下来,然后被撞的球朝这个方向移动,直到有一个球再也撞不到下一个球后,这个球飞出 ...
- zoj 3261 逆向并查集+离线处理
题意:给出一些点,每个点有权值,然后有一些边,相连.无向的.然后有一些操作 链接:点我 query a.表示从a出发的能到达的所有点权值最大的点的编号(相同取编号最小,而且权值要比自己大) desto ...
- Colored Sticks (字典树哈希+并查集+欧拉路)
Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27704 Accepted: 7336 Description You ...
- hdu 4424 & zoj 3659 Conquer a New Region (并查集 + 贪心)
Conquer a New Region Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 算法初级面试题05——哈希函数/表、生成多个哈希函数、哈希扩容、利用哈希分流找出大文件的重复内容、设计RandomPool结构、布隆过滤器、一致性哈希、并查集、岛问题
今天主要讨论:哈希函数.哈希表.布隆过滤器.一致性哈希.并查集的介绍和应用. 题目一 认识哈希函数和哈希表 1.输入无限大 2.输出有限的S集合 3.输入什么就输出什么 4.会发生哈希碰撞 5.会均匀 ...
- 左神算法第五节课:认识哈希函数和哈希表,设计RandomPool结构,布隆过滤器,一致性哈希,岛问题,并查集结构
认识哈希函数和哈希表 MD5Hash值的返回范围:0~9+a~f,是16位,故范围是0~16^16(2^64)-1, [Hash函数],又叫散列函数: Hash的性质: 1) 输入域无穷大: 2) ...
随机推荐
- echarts - 特殊需求实现代码汇总之【柱图】篇
其实包括饼图.线图在内,和柱图都一样的感觉,他们的配置项基本也是对应的那几个,所以想实现某些相似的效果,只要找到对应的属性就可以了. 1.柱图渐变色设置 还记得上篇线图中的实现是在areaStyle的 ...
- String 类实现 以及>> <<流插入/流提取运算符重载
简单版的String类,旨在说明>> <<重载 #include <iostream> //#include <cstring>//包含char*的字符 ...
- webpack4 优化记录
webpack4.0优化那些事儿 一 缩小文件搜索范围 1 include & exclude 1) action 限制编译范围 2) useage module: { rules: [ { ...
- 外网电脑配置8G运行内存,运行Android Studio,速度很轻松
Win 7系统 之前RAM是 4 G,运行Android studio ,再运行浏览器或办公软件时卡的一比.再插入一个 4G内存条,总共8G时,速度嗖的一下就上来了.
- Sencha Touch 实战开发培训 视频教程 第二期 第七节
2014.4.21 晚上8:20左右开课. 本节课视频耗时比较短,不过期间意外情况比较多,录制时间偏长了点. 本期培训一共八节,前两节免费,后面的课程需要付费才可以观看. 本节内容: 视频的录制播放 ...
- 你可能不知道的shell、bash二三事(Centos 7)
个人.bashrc: ~/.bashrc: # .bashrc # User specific aliases and functions alias rm='rm -i' alias cp='cp ...
- PostgreSQL9.3安装tds_fdw扩展
PostgreSQL从9.x开始支持所谓的外表的功能,就是在PostgreSQL中通过安装一些扩展再进行一些配置可以在本地建立一个外表映射到其他不同类型的数据库(可以大致理解为跨越PostgreSQL ...
- H3C系列之三层交换机开启telnet管理的配置
环境介绍>>>>>>>>>>>>>>>>>>>>交换机名牌:H3C交换机类型:三 ...
- 【BZOJ2138】stone Hall定理+线段树
[BZOJ2138]stone Description 话说Nan在海边等人,预计还要等上M分钟.为了打发时间,他玩起了石子.Nan搬来了N堆石子,编号为1到N,每堆包含Ai颗石子.每1分钟,Nan会 ...
- spring-boot 学习笔记一
参考博客:https://www.cnblogs.com/ityouknow/p/5662753.html 1.构建项目: 访问http://start.spring.io/,下载demo: 下载解压 ...