uva 11987 Almost Union-Find (并查集)
来源:互联网 发布:贪吃蛇的c语言代码 编辑:程序博客网 时间:2024/06/05 06:16
题目大意:
三个操作。
1. 合并两个集合
2.把第一个元素放到第二个集合里
3.输出集合的数量和和。。
思路分析:
要用p记录这个元素所在集合编号,然后用编号建立并查集。
#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>using namespace std;typedef long long LL;int set[111111];int cnt[111111];LL sum[111111];int p[111111];int find(int x){ if(x!=set[x]) set[x]=find(set[x]); return set[x];}int main(){ int n,m; while(scanf("%d%d",&n,&m)!=EOF) { for(int i=1;i<=n;i++)set[i]=i,cnt[i]=1,sum[i]=i,p[i]=i; while(m--) { int op; scanf("%d",&op); int l,r; if(op==1) { scanf("%d%d",&l,&r); int xx=find(p[l]); int yy=find(p[r]); if(xx==yy)continue; else { set[xx]=yy; cnt[yy]+=cnt[xx]; sum[yy]+=sum[xx]; } } else if(op==2) { scanf("%d%d",&l,&r); int xx=find(p[l]); int yy=find(p[r]); if(xx==yy)continue; else { sum[xx]-=l; cnt[xx]--; sum[yy]+=l; cnt[yy]++; p[l]=yy; } } else { scanf("%d",&l); printf("%d %lld\n",cnt[find(p[l])],sum[find(p[l])]); } } } return 0;}
0 0
- uva 11987 Almost Union-Find(加权并查集)
- UVA 11987 Almost Union-Find(并查集)
- uva 11987 Almost Union-Find(并查集)
- UVA 11987 Almost Union-Find (并查集)
- UVA 11987 Almost Union-Find(并查集)
- UVa:11987 Almost Union-Find(并查集)
- uva 11987 Almost Union-Find (并查集)
- UVA 11987 Almost Union-Find(并查集)
- UVA 11987 Almost Union-Find(hash + 并查集)
- UVA 11987 Almost Union-Find (并查集+删边)
- uva 11987 - Almost Union-Find(并查集)
- UVa 11987,Almost Union-Find,并查集
- UVA 11987 Almost Union-Find 并查集节点删除
- UVA 11987 - Almost Union-Find(并查集)
- UVA - 11987 Almost Union-Find 并查集
- uva 11987 Almost Union-Find (并查集)
- UVA 11987 Almost Union-Find (并查集)
- Almost Union-Find(Uva 11987)并查集
- bzoj有趣的题目
- java总结
- redis 或者memcache session 存储 同步
- jpanel 切换
- RR日期型格式
- uva 11987 Almost Union-Find (并查集)
- 解决在win7系统使用Protel 99SE添加库文件时出现“file is not recognized“
- java访问控制关键字用法
- uva 10534 Wavio Sequence
- Oracle忘记密码怎么办
- Mdev应用简记
- 学习OpenCV 第二版<1-3>:What Is Computer Vision?
- DOS命令大全
- Hero In Maze