ZOJ 2833-Friendship(并查集+优化)

Friendship


Time Limit: 3 Seconds     
Memory 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

赤裸裸的并查集,一开始TLE了一次,当时是直接查某个元素所属集合里面的元素的个数,扫一遍需要O(n)最坏要O(m*n)。。。不TLE才怪,直接开结构体记录每个集合元素的个数,初始化为1,合并的时候将被合并的集合的元素的个数加到另一个集合里去,查找O(1)

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <list>
using namespace std;
typedef struct node
{
	int data,num;
};
node fa[100010];
void Make_set(int n)
{
	for(int i=1;i<=n;i++)
	{
		fa[i].data=i;
		fa[i].num=1;
	}
}
int Find(int x)
{
	if(x!=fa[x].data)
		fa[x].data=Find(fa[x].data);
	return fa[x].data;
}
void Union(int x,int y)
{
	int fx=Find(x);
	int fy=Find(y);
	if(fx==fy)
		return ;
	fa[fy].data=fx;
	fa[fx].num+=fa[fy].num;
}
int main()
{
	int n,m,x,y,T=1;char op;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		if(T!=1)
			printf("\n");
		getchar();
		printf("Case %d:\n",T++);
		Make_set(n);
		while(m--)
	   {
		 scanf("%c",&op);
		 if(op=='M')
		 {
			scanf("%d%d",&x,&y);
			getchar();
			Union(x,y);
		 }
		 else if(op=='Q')
		 {
			scanf("%d",&x);getchar();
				int ans=fa[Find(x)].num;
			printf("%d\n",ans);
		 }
		}
	}
	return 0;
}

ZOJ 2833-Friendship(并查集+优化),布布扣,bubuko.com

时间: 2024-12-26 04:55:27

ZOJ 2833-Friendship(并查集+优化)的相关文章

普林斯顿公开课 算法1-10:并查集-优化的快速合并方法

应用 渗透问题 游戏中会用到. 动态连接 最近共同祖先 等价有限状态机 物理学Hoshen-Kopelman算法:就是对网格中的像素进行分块 Hinley-Milner多态类型推断 Kruskai最小生成树 Fortran等价语句编译 形态学开闭属性 Matlab中关于图像处理的bwlabel函数 渗透问题 一个N×N的矩阵,判断顶部和底部是否连通就是渗透问题. 下图中左侧的矩阵能渗透,右侧矩阵不能渗透. 渗透问题在电学.流体力学.社会交际中都有应用. 在游戏中可能需要生成一张地图,但是作为地图

(贪心 + 并查集优化) poj 1456

Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9452   Accepted: 4067 Description A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx that is measured as an int

Supermarket poj 1456 贪心+并查集优化

Language: Default Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9512   Accepted: 4096 Description A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx that is

POJ 1456——Supermarket——————【贪心+并查集优化】

Supermarket Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1456 Description A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx

poj 1827 A Bunch Of Monsters 贪心(并查集优化)

Description Background Jim is a brave explorer. One day, he set out for his next destination, a mysterious hill. When he arrived at the foot of the hill, he was told that there were a bunch of monsters living in that hill, and was dissuaded from cont

Supermarket---poj456(贪心并查集优化)

题目链接:http://poj.org/problem?id=1456 题意是现有n个物品,每个物品有一个保质期和一个利润,现在每天只能卖一个商品,问最大的利润是多少,商品如果过期了就不能卖了: 暴力的方法: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <stack> #i

并查集优化连边

很多题目均可用并查集优化连边, 跳过一些已经访问过的点 比方说, 对[L,R]范围进行一定更新可以这样写 for (int i=Find(L); i<=R; i=Find(i)) { //do something fa[i] = i+1; } 这样操作过后[L,R]的fa均指向R+1, 下一次会直接跳到R+1, 相当于每个点值只更新一次 例题 1, CF 827A 求构造一个最小字典序的字符串, 其中字符串某些位置的字符固定 #include <iostream> #define REP

区间 (模拟并查集优化)

问题描述 给出一个序列 a1, ..., an. 定义一个区间 [l,r] 是好的,当且仅当这个区间中存在一个 i,使得 ai 恰好等于 al, al+1, ..., ar-1, ar 的最大公因数. 求最长的好的区间的长度. 输入 第一行 n,表示序列的长度; 第二行 n 个数 a1,a2,...,an. 输出 输出一行一个数,表示最长的好的区间的长度. 题解 题解写代码里可能容易懂? 代码 #include <cstdio> #define ll long long #define fil

Supermarket ——贪心(并查集优化)

题目链接 题意: 给你n个商品,每个商品都有两个参数 p d ,p为该商品卖出后的利润,d表明该商品只能在这个期限之前卖出,一天只能卖出一件商品. 问你这批商品最多能获得多少利润 题解: 贪心!!! 按照利润从大到小排序,如果利润相同就按照期限从大到小排序,这样才能保证在一定期限内卖更多的商品获得更大的利润 排序完成后,枚举每个商品的结束的时间,然后向前暴力(找到离这个期限最近的且可占用的时间),如果当前时间可以卖出即没被vis数组标记,就可以再在当前时间卖出 代码: #include<iost