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(并查集+哈希)的更多相关文章

  1. 栈&队列&并查集&哈希表(julyedu网课整理)

    date: 2018-11-25 08:31:30 updated: 2018-11-25 08:31:30 栈&队列&并查集&哈希表(julyedu网课整理) 栈和队列 1. ...

  2. zoj 2833 friendship

    zoj 2833这次真的很顺利了..居然是因为数组的大小没有符合要求,瞎折腾了很久..没有注意到要求范围,真是该死! 想法很简单,就是定义一个父结点数组,下标 i 表示这个元素,初始化为 -1表示 这 ...

  3. ZOJ - 3261 逆向并查集

    思路:很巧妙的解法.如果按照常规一边读入,一边合并并查集,删边实在没办法做. 首先读入所有的操作,把所有不会被删除的边加入并查集,然后从最后一个操作开始逆向操作,当遇到删边操作,就直接把这条边加入并查 ...

  4. zoj 3761(并查集+搜索)

    题意:在一个平面上,有若干个球,给出球的坐标,每次可以将一个球朝另一个球打过去(只有上下左右),碰到下一个球之后原先的球停下来,然后被撞的球朝这个方向移动,直到有一个球再也撞不到下一个球后,这个球飞出 ...

  5. zoj 3261 逆向并查集+离线处理

    题意:给出一些点,每个点有权值,然后有一些边,相连.无向的.然后有一些操作 链接:点我 query a.表示从a出发的能到达的所有点权值最大的点的编号(相同取编号最小,而且权值要比自己大) desto ...

  6. Colored Sticks (字典树哈希+并查集+欧拉路)

    Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27704   Accepted: 7336 Description You ...

  7. 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 ...

  8. 算法初级面试题05——哈希函数/表、生成多个哈希函数、哈希扩容、利用哈希分流找出大文件的重复内容、设计RandomPool结构、布隆过滤器、一致性哈希、并查集、岛问题

    今天主要讨论:哈希函数.哈希表.布隆过滤器.一致性哈希.并查集的介绍和应用. 题目一 认识哈希函数和哈希表 1.输入无限大 2.输出有限的S集合 3.输入什么就输出什么 4.会发生哈希碰撞 5.会均匀 ...

  9. 左神算法第五节课:认识哈希函数和哈希表,设计RandomPool结构,布隆过滤器,一致性哈希,岛问题,并查集结构

    认识哈希函数和哈希表 MD5Hash值的返回范围:0~9+a~f,是16位,故范围是0~16^16(2^64)-1, [Hash函数],又叫散列函数: Hash的性质: 1)  输入域无穷大: 2)  ...

随机推荐

  1. linux制做RPM包

    制作rpm包 1.制作流程 1.1 前期工作 1)创建打包用的目录rpmbuild/{BUILD,SPECS,RPMS, SOURCES,SRPMS} 建议使用普通用户,在用户家目录中创建 2)确定好 ...

  2. PV&UV&IP之间的区别和联系

    PV PV是网站分析的一个术语,用于衡量网站用户访问的网页的数量.对于广告投入商来说,PV值可以预期它可以带来多少收入广告,一般来说,OV与来访者的数量成正比,但是PV并不直接决定页面的真实来访者数量 ...

  3. Python中四种运行其他程序的方式

    原文地址:http://blog.csdn.net/jerry_1126/article/details/46584179 在Python中,可以方便地使用os模块来运行其他脚本或者程序,这样就可以在 ...

  4. Android 自动化测试 robotium

    转:http://xiaomaozi.blog.51cto.com/925779/908886 Android 的开发可以说已经遍地都是,不说精致的app,只要看些书,看点教学视频,学习二至三个月,都 ...

  5. 1007: [HNOI2008]水平可见直线[维护下凸壳]

    1007: [HNOI2008]水平可见直线 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 7184  Solved: 2741[Submit][Sta ...

  6. async 与await

    一. async 与await (https://segmentfault.com/a/1190000007535316) 1.async 是“异步”的简写,而 await 可以认为是 async w ...

  7. 用ELK打造可视化集中式日志

    原文链接:https://yq.aliyun.com/articles/57420 摘要: Elk是Elastic search, Logstash和Kibana三者的简称. Elastic sear ...

  8. 在稳定性测试中,将测试结果持续填加进入html报告

    公司需要设计一个稳定性测试,就是一直持续的跑不同的用例,直到人为停止,用例基本完成,基本框架思路就是随机选择一个testcase,跑完后输出结果.但存在一个问题,现在的unittest或nose测试报 ...

  9. java常用数据格式转化,类似数据库group by cube rollup

    java常用数据格式转化,类似数据库group by cube rollup单循环一条sql返回格式如:List<Map<String, List<Record>>> ...

  10. windows下的C++与cuda编译器位置

    在windows下最常见的C++编译器为visual studio自带的编译器cl.exe 通常其所在目录为: C:\Program Files (x86)\Microsoft Visual Stud ...