ZOJ 2833 Friendship(并查集)

题目链接:https://zoj.pintia.cn/problem-sets/91827364500/problems/91827366332

Friendship

Time Limit: 3000 msMemory Limit: 32768 KB

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.

解题思路:
并查集模板题,加路径压缩即可.

代码如下:

#include <bits/stdc++.h>
using namespace std;
#define N 100005
int n,m,bin[N],sum[N];
inline int find(int a){return bin[a]==a?a:bin[a]=find(bin[a]);
}
inline void merge(int a,int b){int fa=find(a);int fb=find(b);if(fa!=fb){bin[fa]=fb;sum[fb]+=sum[fa];}
}
inline void init(){for(int i=1;i<=n;i++){bin[i]=i;sum[i]=1;}
}
int main(){int ans=1;while(scanf("%d %d",&n,&m)!=EOF){init();if(ans!=1)printf("\n");printf("Case %d:\n",ans++);for(int i=0;i<m;i++){getchar();char c;scanf("%c",&c);if(c=='M'){int a,b;scanf("%d %d",&a,&b);merge(a,b);}else{int a;scanf("%d",&a);printf("%d\n",sum[find(a)]);}}}return 0;
}

ZOJ 2833 Friendship相关推荐

  1. ZOJ 2833 Friendship

    点击打开链接 Friendship Time Limit: 3 Seconds      Memory Limit: 32768 KB A friend is like a flower, a ros ...

  2. 【转载】图论 500题——主要为hdu/poj/zoj

    转自--http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  3. ZOJ 2723 Semi-Prime ||ZOJ 2060 Fibonacci Again 水水水!

    两题水题: 1.如果一个数能被分解为两个素数的乘积,则称为Semi-Prime,给你一个数,让你判断是不是Semi-Prime数. 2.定义F(0) = 7, F(1) = 11, F(n) = F( ...

  4. zoj 1204 Additive equations

    ACCEPT acm作业 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=204 因为老师是在集合那里要我们做这道题.所以我很是天 ...

  5. 【HDU/POJ/ZOJ】Calling Extraterrestrial Intelligence Again (素数打表模板)

    http://poj.org/problem?id=1411  POJ http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=168 ...

  6. 模拟 ZOJ 3878 Convert QWERTY to Dvorak

    题目传送门 1 /* 2 模拟:手敲map一一映射,累! 3 除了忘记读入字符串不能用gets用getline外还是很顺利的AC了:) 4 */ 5 #include <cstdio> 6 ...

  7. 矩阵连乘积 ZOJ 1276 Optimal Array Multiplication Sequence

    题目传送门 1 /* 2 题意:加上适当的括号,改变计算顺序使得总的计算次数最少 3 矩阵连乘积问题,DP解决:状态转移方程: 4 dp[i][j] = min (dp[i][k] + dp[k+1] ...

  8. ZOJ 3597 Hit the Target! (线段树扫描线 -- 矩形所能覆盖的最多的点数)

    ZOJ 3597 题意是说有n把枪,有m个靶子,每把枪只有一发子弹(也就是说一把枪最多只能打一个靶子), 告诉你第 i 把枪可以打到第j个靶, 现在等概率的出现一个连续的P把枪,在知道这P把枪之后,你 ...

  9. 8. American Friendship 美国式的友谊

    8. American Friendship 美国式的友谊 (1) Americans usually consider themselves a friendly people.Their frie ...

最新文章

  1. 小程序json字符串转 json对象 { name :你好} 转成 { name :你好}
  2. Volley 源码解析之图片请求
  3. python【力扣LeetCode算法题库】面试题 10.01-合并排序的数组
  4. oracle 日期改字符格式_oracle怎么把数值型日期转换成日期字符型
  5. 设计模式-策略模式和工厂模式结合使用
  6. 斐波那契数列的鬼畜的性质
  7. 学习环境配置:Manjaro、MSYS2以及常见软件
  8. python面向对象中的类
  9. 自然语言处理系列篇——关键词智能提取
  10. 找不到图片素材,看这里
  11. android 沙箱 逆向,【原创】沙箱Sandboxie v3.40 逆向完整源码
  12. Evaluate the standards between the Top Five through ratings of transferred players on whoscored.com
  13. 解决树莓派开机黑屏不显示桌面问题
  14. 基于 NVIDIA GPU 和 RAPIDS 加速 Spark 3.0
  15. python编程遵循哪些规律_编程语言软件开发10个小技巧,Java、python、前端等都遵循此规律,108G资料放送中!...
  16. 苹果用计算机知道密码,怎么查看苹果系统电脑中safari浏览器内的用户登录密码...
  17. 计算机网络---网络体系结构分析
  18. mybatis(java持久层框架)
  19. 手机端移动端的前端原生js裁剪图片上传
  20. AntV X6制作画板工具(图形,线段,图片上传)

热门文章

  1. 好书好人生--读书的步骤
  2. 如何换算天干地支和现在的纪年,计月,计日,计时
  3. Java白皮书关键词总结
  4. Word标题编号后空格太大怎么调?
  5. 生物医药行业竞争环境
  6. 第"三"行代码-kotlin部分学习笔记
  7. 模型测试集上准确率(ACC)、精确率precision、召回率recall、AUC评估指标计算
  8. 企宣java开发项目答辩_java答辩ppt模板
  9. 获取哔哩哔哩网站的排行榜
  10. 省级面板数据(2000-2019)十三:建筑业(主要经济指标、劳动生产率)(stata版)