1540 银河英雄传说
来源:互联网 发布:软件测试基础方法 编辑:程序博客网 时间:2024/06/09 13:42
分析:
类似于带权并查集,又由于更新的时候是从根开始叠加,所以pre[i] 为前面含有的战舰数,每次更新pre[i]
再用一个sum数组保存根节点后面的子节点数目,每次合并时将 pre[尾部舰队根节点的] 设为 sum[被接的舰队的根节点]
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cmath>using namespace std;#define maxn 30000+5int fa[maxn];int sum[maxn];int pre[maxn];int find(int x){ if(x == fa[x])return x; int f = fa[x]; fa[x] = find(fa[x]); pre[x] += pre[f]; return fa[x];}void init(){ for(int i = 0 ; i < maxn ; ++i){ fa[i] = i; sum[i] = 1; }}int main(){ init(); int T,a,b; char c[3]; cin >> T; while(T--) { scanf("%s %d %d",&c,&a,&b); int x = find(a); int y = find(b); if(c[0] == 'M'){ fa[x] = y; pre[x] = sum[y]; sum[y] += sum[x]; } else { if( x == y ) printf("%d\n",abs(pre[b] - pre[a])-1); else printf("-1\n"); } }}
0 0
- 1540 银河英雄传说
- wikioi 1540 银河英雄传说
- COdevs 1540 银河英雄传说
- 【codevs 1540】银河英雄传说
- 1540银河英雄传说{并查集}
- 银河英雄传说 (codevs 1540) 题解
- Codevs 1540+洛谷1196 银河英雄传说
- TYVJ 银河英雄传说
- 【noi2002】银河英雄传说
- Noi2002银河英雄传说
- 【NOI2002】银河英雄传说
- 【codevs1540】 银河英雄传说
- 银河英雄传说
- 【u010】银河英雄传说
- P1196 银河英雄传说
- [P1196]银河英雄传说
- 银河英雄传说
- NOI2002 银河英雄传说
- UISprite和UITexture
- 亡羊补牢,为时未晚
- pair 对组
- 疯狂Java笔记:第 1 章 Java简介与开发环境的搭建
- 轻量却超强——推荐几款好用的截图工具
- 1540 银河英雄传说
- vector使用swap释放内存
- 459. Repeated Substring Pattern
- 文章标题
- Oracle Study---Oracle 11g 不可见索引案例
- linux中rpm命令的一些使用总结
- 散列函数设计:除留余数法
- 三条技巧让你的译文“说得通”
- 合数分解 UVA