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) ...
随机推荐
- TNS-12532: TNS:invalid argument,Oracle的报错信息太让人无语
TNS-12532: TNS:invalid argument,Oracle的报错信息太让人无语 现象: Tnsping报错: [oracle@unicomGZ01 admin]$ ../../bin ...
- 原生js--键盘事件
键盘事件知识点: 1.如果用户按键事件足够长,在keyup事件触发之前,会触发多次keydown事件 2.通过keyCode(charCode firefox)指定按下的是哪个键,采用unicode编 ...
- css笔记——文本样式
聊聊text-decoration.text-indent.text-transform.letter-spacing.word-spacing.vertical-align.下面是一些常用设置文本样 ...
- Android 简单计算器实现源码
1.string.xml代码 <?xml version="1.0" encoding="utf-8"?> <resources> &l ...
- 关于C#泛型作用的简单说明
泛型:即通过参数化类型来实现在同一份代码上操作多种数据类型.泛型编程是一种编程范式,它利用“参数化类型”将类型抽象化,从而实现更为灵活的复用. C#泛型的作用概述 C#泛型赋予了代码更强的类型安全,更 ...
- 【CF886E】Maximum Element DP
[CF886E]Maximum Element 题意:小P有一个1-n的序列,他想找到整个序列中最大值的出现位置,但是他觉得O(n)扫一遍太慢了,所以它采用了如下方法: 1.逐个遍历每个元素,如果这个 ...
- nginx 二级域名跳转
server { listen ; server_name m.aaoo.cn; #charset koi8-r; #access_log logs/host.access.log main; rew ...
- Chrome V8引擎的一点认识
最近在玩弄JavaScript ,也一直在捉摸,脚本Engine怎么解析你写的Code,对Google兴趣浓,索性就看了谷歌的脚本engine的官方资料,都是E文的,但是却是最纯的不是,看下来总结V8 ...
- Mysql数据库操作复习,增删改查
Mysql数据库 Apache(服务员) php(大厨) mysql(冰柜) Mysql是瑞典的mysqlAB公司开发的一款中小型关系型数据库管理系统. MysqlAB公司在2008年被S ...
- System.Web.UI.Page.Cache 页面 缓存 清除
这个也是网上查询到方法,不错记录一下! /// <summary> /// 清空所有的Cache /// </summary> public static void Clear ...