zoj-2833 friendship
2014-02-25 15:27
204 查看
A - Friendship
Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
SubmitStatus
Description
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.
时间超限啊,这咋办
换思路,下面是通过的代码,就是计数,把每个根的数都统计出来160MS过
:
Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
SubmitStatus
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 |
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<stdio.h> int bin[100005]; int find(int x) { int r; r=x; while(bin[r]!=r) r=bin[r]; return r; } void bing(int x,int y) { int fx,fy; fx=find(x); fy=find(y); if(fx!=fy) { bin[fx]=fy; //num[fx]=find(fy); } } int main() { int n,m,x,y,t,d,count,e=1; char c; while (scanf("%d%d",&n,&m)!=EOF) { getchar(); printf("Case %d:\n",e++); for(int i=1;i<=n;i++) {bin[i]=i;} for(int i=0;i<m;i++) { count=0; scanf("%c",&c); if(c=='M') { scanf("%d%d",&x,&y);getchar(); bing(x,y); } else if (c=='Q') { scanf("%d",&t);getchar(); d=find(t); for(int i=1;i<=n;i++) if(find(i)==d) count++; printf("%d\n",count ); } } }return 0; }
换思路,下面是通过的代码,就是计数,把每个根的数都统计出来160MS过
:
#include<stdio.h> int bin[100005],num[100005]; int find(int x) { int r; r=x; while(bin[r]!=r) r=bin[r]; return r; } void bing(int x,int y) { int fx,fy; fx=find(x); fy=find(y); if(fx==fy) return ; else if(fx<=fy) { bin[fx]=fy; num[fy]+=num[fx]; } else if(fx>=fy) { bin[fy]=fx; num[fx]+=num[fy]; } } int main() { int n,m,x,y,t,d,count,e=1,q=0; char c; while (scanf("%d%d",&n,&m)!=EOF) { getchar(); ++q; if(q!=1) printf("\n"); printf("Case %d:\n",e++); for(int i=1;i<=n;i++) {bin[i]=i;num[i]=1;} for(int i=0;i<m;i++) { count=0; scanf("%c",&c); if(c=='M') { scanf("%d%d",&x,&y);getchar(); bing(x,y); } else if (c=='Q') { scanf("%d",&t); printf("%d\n",num[find(t)]) ;getchar(); } } } return 0; }
相关文章推荐
- linux ------ 中断处理及延时操作
- 关于Android SDK无法更新的解决办法
- Regex.Escape
- 7.12.1: spel---使用expression接口进行表达式求值
- 单例模式(C++实现)
- 关于oracle数据库里的date项插入(不积跬步,无以至千里)
- Android 微信分享遇到问题 及解决方案
- 如何view定义
- HNUST 问题 F: 大爱线性表
- 求和为某数的全部组合
- 7.11.2: 基于XML Schema的简化配置方式---使用util Schema
- 双tomcat的部署
- Android中线程通讯类Handler
- 字符编码ASCII、UNICODE与UTF8,UTF16的含义等
- 华为和H3C三层交换机端口切换命令
- 多协议多功能数据传输蓝牙解决方案
- POJ 1703 Find them, Catch them
- Java数组及其常用类
- 7.11.1: 基于XML Schema的简化配置方式---使用p名称空间配置属性
- 应用netlink的内核模块和应用程序Makefile编写