zoj 2833 Friendship(并查集)
2015-08-13 16:14
344 查看
Friendship
Time Limit: 3 Seconds
Memory Limit: 32768 KB
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
分析:简单的并查集的应用,用一个pre[]数组,在初始状态,每个数组存储的根节点使其本身,当a,b连到一块时,pre【a】=b,代码如下
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 |
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
分析:简单的并查集的应用,用一个pre[]数组,在初始状态,每个数组存储的根节点使其本身,当a,b连到一块时,pre【a】=b,代码如下
#include"cstdio" #include"iostream" #include"cstring" using namespace std; const int maxn=100000+4; int pre[maxn],num[maxn]; int find(int x){ while(pre[x]!=x) x=pre[x]; return x; } int join(int a,int b){ int x=find(a); int y=find(b); pre[a]=x; pre[b]=y; if(x!=y){ pre[x]=y; num[y]+=num[x]; } } int main(){ int n ,m,cas=1; int f=1; while(scanf("%d %d",&n,&m)==2){ if(f!=1){ cout<<endl; } f=0; cout<<"Case "<<cas++<<":"<<endl; memset(pre,0,sizeof(pre)); memset(num,0,sizeof(num)); for(int i=1;i<=n;i++){ pre[i]=i; num[i]=1; } char c; for(int i=1;i<=m;i++){ getchar(); cin>>c; int a,b,d; if(c=='M'){ cin>>a>>b; join(a,b); } else if(c=='Q'){ int a; cin>>a; cout<<num[find(a)]<<endl; } } } }
相关文章推荐
- Receiver not registered.
- NYOJ--13--Fibonacci数
- 迷宫问题 POJ 3984 【BFS】
- HDU 5370 Tree Maker
- [老老实实学WCF] 第五篇 再探通信--ClientBase
- 【坑】【未解答】使用canvas中的arcTo绘制圆角矩形遇到的问题
- 第五章 编码/加密——《跟我学Shiro》
- NSOperation-NSBlockOperation
- DOS下adb命令使用
- How Does Caching Work in AFNetworking? : AFImageCache & NSUrlCache Explained
- InfiniTAM的编译探索
- nginx+tomcat的安装与配置)
- Cash Machine poj (多重背包)
- 玩转指针(Playing with Pointers)
- [老老实实学WCF] 第四篇 初探通信--ChannelFactory
- 第四章 INI配置——《跟我学Shiro》
- 【Ex2013】【跨站点DAG】防止数据库活动副本切换到备份服务器
- 第三章 授权——《跟我学Shiro》
- 全局钩子函数之 SetWindowsHookEx
- Excel Sheet Column Number