Friendship

原创 2015年07月07日 15:18:48
A - Friendship
Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

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.


/*
Author:2486
Memory: 952 KB		Time: 170 MS
Language: C++ (g++ 4.7.2)		Result: Accepted
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=100000+5;
int par[maxn],sum[maxn];
int n,m,x,y;
char op[5];
void init(int x) {
    for(int i=0; i<=x; i++) {
        par[i]=i;
        sum[i]=1;
    }
}
int find(int x) {
    return par[x]==x?x:par[x]=find(par[x]);
}
bool same(int x,int y) {
    return find(x)==find(y);
}
void unite(int x,int y) {
    x=find(x);
    y=find(y);
    if(x==y)return;
    par[x]=y;
    sum[y]+=sum[x];
}
int main() {
    int cases=0;
    //freopen("D://imput.txt","r",stdin);
    while(~scanf("%d%d",&n,&m)) {
        init(n);
        cases++;
        if(cases!=1)printf("\n");
        printf("Case %d:\n",cases);
        for(int i=0; i<m; i++) {
            scanf("%s",op);
            if(op[0]=='Q') {
                scanf("%d",&x);
                printf("%d\n",sum[find(x)]);
            } else {
                scanf("%d%d",&x,&y);
                unite(x,y);
            }
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Peace and Friendship

  • 2009-04-11 18:49
  • 462KB
  • 下载

POJ--1815[Friendship] 最小割模型(拆点+枚举)

思路:(1):如果ss与tt直接相连,直接输出"NO ANSWER!"(2):然后在原图上求一次最小割min_cut,如果最小割为0,则直接输出0(3):接着枚举1-N个点,当枚举到当前点时要判断这个...

c语言编写的friendship

  • 2012-04-16 15:20
  • 858B
  • 下载

Friendship

  • 2012-06-11 19:58
  • 1.98MB
  • 下载

Python 对Facebook用户Friendship的聚类分析

Python 对Facebook用户Friendship的聚类分析

friendship.rar

  • 2010-06-24 21:36
  • 8.00MB
  • 下载

codeforces 791B Bear and Friendship Condition

B. Bear and Friendship Condition time limit per test 1 second memory limit per test 256 megabyte...

POJ 1815 Friendship(网络流,点拆分)

题意:点之间有双向的联系,问去掉多少个点可以使s,t之间没有联系,点尽量少,序号尽量小。 思路:  网络流建图真难啊  , 看别人的思路过的 ,, 把一个点拆分成两个,出点和入点,对于有联系的点对...

CF - 791B. Bear and Friendship Condition - 并查集+思维

1.题目描述: B. Bear and Friendship Condition time limit per test 1 second memory limit per te...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)