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;
}

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

Entity Framework 实践系列 —— 搞好关系 - 单相思(单向一对一,one-to-one)

原以为躲入代码世界,就可以不用搞关系,哪知“关系无处不在”。写代码多年之后,终于明白“面向对象的关键是搞好对象之间的关系”。而Entity Framework作为ORM中的明日之星,首当其冲的使命就是...
  • WdengniZ
  • WdengniZ
  • 2013年01月22日 10:44
  • 1522

C++中的friend的浅析

friend ,友元。  (1)在类里面声明friend 函数,而且只能在类外部可以定义该函数,并且可以访问该类的public成员。  Friend函数的声明可以放在:public,private...
  • nemo2011
  • nemo2011
  • 2013年01月13日 15:10
  • 1218

并查集——Friendship

题目: Description A friend is like a flower, a rose to be exact, Or maybe li...
  • u013246445
  • u013246445
  • 2014年02月25日 19:20
  • 324

并查集(A - Friendship)

Description A friend is like a flower, a rose to be exact, Or maybe like a brand new gate...
  • u013514659
  • u013514659
  • 2014年02月25日 16:49
  • 283

【POJ】1815 Friendship 枚举+最大流。。

传送门:【POJ】1815 Friendship 题目分析:枚举都能过。。。
  • u013368721
  • u013368721
  • 2014年08月02日 20:43
  • 499

【最小割+枚举】POJ-1815 Friendship

Friendship Time Limit: 2000MS   Memory Limit: 20000K       Description ...
  • u012325552
  • u012325552
  • 2014年09月27日 21:00
  • 415

【POJ1815】Friendship 网络流最小割

破题水。
  • Vmurder
  • Vmurder
  • 2015年01月07日 10:59
  • 926

Friendship (poj 1815 最小点割集+枚举)

题意:给出N个人之间的通讯关系,给出S和T,问切断S到T的联系最小要删去的点是多少,输出最小字典序解。 思路:拆点,从1~N枚举删掉i后看最大流是否减少,若减少则i属于最小点割集。...
  • u014422052
  • u014422052
  • 2015年09月15日 16:10
  • 383

POJ 1815 Friendship(最小割+拆点法)

题意:求拆除几个点可以使得s和t不连通。 典型的最小割问题,不过该题不是删除边而是删除点,又是结点容量问题,所以我们用拆点法来解决 。  对于最小割问题,也做了不少了,说说我的理解吧: 就是通过最大流...
  • weizhuwyzc000
  • weizhuwyzc000
  • 2015年09月01日 20:29
  • 735

poj 1815 Friendship(最小点割集)

Friendship Time Limit: 2000MS   Memory Limit: 20000K Total Submissions: 8465   Acc...
  • WEYuLi
  • WEYuLi
  • 2014年01月16日 17:50
  • 771
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Friendship
举报原因:
原因补充:

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