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) ...
随机推荐
- Matlab练习——多项式和一元方程求解
1. 一元函数 f(x) = x^3 + (x - 0.98)^2 / (x + 1.25)^3 - 5 * (x + 1 / x),求f(x) = 0 的根 %在函数文件中定义函数 function ...
- 使用kendynet编写网关服务
网游服务器大多提供了网关服务,用于作为用户和内部服务器组之间通信代理.网关服务一方面将用户消息从客户端分发到正确的内部服务器. 另一方面将来自内部服务器的数据包转发给客户端.一般对于网关应用来说,压力 ...
- mvc 默认访问 Area 下控制器方法
在MVC项目中经常会使用到Area来分开不同的模块让项目结构更加的清晰.如果想网站打开默打开Area下的控制器时会出现以下的错误 “/”应用程序中的服务器错误. 未找到视图“Index”或其母版视图, ...
- Elasticsearch 学习之 分片未分配原因
分片未分配的原因主要有: 1)INDEX_CREATED:由于创建索引的API导致未分配.2)CLUSTER_RECOVERED :由于完全集群恢复导致未分配.3)INDEX_REOPENED :由于 ...
- MySQL的ALTER变更、正则查询、分组查询、排序查询以及事务查询的概
MySQL的表和字段信息的变更 ALTER TABLE table-name DROP column-name; #删除某个字段 ALTER TABLE table-name ADD column-n ...
- java 程序运行的基础知识【Java bytecode】
聊聊文字,写一篇关于 java 基础知识的博文. JVM 线程栈 到 函数运行 每一个JVM线程来说启动的时候都会创建一个私有的线程栈.一个jvm线程栈用来存储栈帧,jvm线程栈和C语言中的栈很类似, ...
- CENTOS安装ElasticSearch
原文链接:https://my.oschina.net/topeagle/blog/591451?fromerr=mzOr2qzZ CENTOS安装ElasticSearch ElasticSearc ...
- mysql概要(六)连接(内连接,左,右外连接
内连接 [join on / from 表1,表二 ]效果一样 区别是:可以理解为首先取得笛卡儿积后,再匹配/还是根据条件获得笛卡尔积 内连接:取俩表的交叉匹配数据:(mysql 内连接 左连接 右连 ...
- 涨知识,涨知识 :ThinkPHP框架下Where条件查询Mysql数据库某字段是否为空
代码虐我千百遍,我对代码如初恋~ 问题: 查询某字段app_date数据是否为NULL,正常我们实现的办法是: $map['app_data'] = array('eq','null'); $data ...
- javascript 字符串去空格
1.正则去空格 a.去掉字符串中所有空格 " hello world ".replace(/\s+/g,"");//helloworld b.去掉字符串左边空格 ...