【HDU】2818-Building Block(并查集)
2015-06-04 16:22
417 查看
坑死了,这道题QAQ。。。说好的没有0,但是后台数据有0,WA了一下午= =
利用num[i]代表i下面有几个格子,Size[i]代表i做底的时候这一堆有几个箱子
利用num[i]代表i下面有几个格子,Size[i]代表i做底的时候这一堆有几个箱子
#include<cstdio> #include<algorithm> using namespace std; const int maxn = 30005; int n,fa[maxn]; int num[maxn],Size[maxn]; char op[5]; int find_father(int u){ if(fa[u] == u) return fa[u]; else{ int temp = fa[u]; fa[u] = find_father(fa[u]); num[u] += num[temp]; return fa[u]; } } int main(){ while(scanf("%d",&n) != EOF){ for(int i = 0; i <= 30000; i++){ fa[i] = i; Size[i] = 1; num[i] = 0; } int x,y; for(int i = 1; i <= n; i++){ scanf("%s",op); if(op[0] == 'M'){ scanf("%d%d",&x,&y); int fx = find_father(x),fy = find_father(y); if(fx!= fy){ fa[fx] = fy; num[fx] = Size[fy]; Size[fy]+= Size[fx]; Size[fx] = 0; } } else{ scanf("%d",&x); find_father(x); printf("%d\n",num[x]); } } } return 0; }
相关文章推荐
- Win7共享问题 映射网盘时出现的错误 the specified server cannot perform the requested operation
- UITextField格式化银行卡号
- 解析duilib 消息处理函数-笔记1
- easyUI 1.3.2 dataGrid动态绑定列名和数据
- 使用Qt Quick设计器
- request,路径问题,转发和重定向
- request和response的知识
- UEditor 1.4.3版本中去掉本地保存功能
- db2 sequences值
- request的用法
- UEditor前端配置项说明
- 百度UEditor修改右下角统计字数包含html样式
- 简单叙说 serialVersionUID 的作用 (用于笔记)
- iOS的UIButton和UILable
- Android酷炫实用的开源框架(UI框架)
- Java中的SerialVersionUID
- UIActionSheet的使用
- UIButton的title位置问题
- UIButton的title位置问题
- 新gre issue写作破题方法揭秘