hdu3635(并查集)
来源:互联网 发布:java培训那家口碑最高 编辑:程序博客网 时间:2024/05/16 07:38
这道题的难点在于如何记录转移的次数,我们可以想到find函数里面会遍历到根节点的translate次数,所以,我们可以在find的时候修改访问的值。
其他没有什么难点,简单的并查集处理就好
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <algorithm>#include <cmath>#include <vector>#define N 10005using namespace std;int m,n,G[N],Rank[N],tran[N];int Find(int x){ if(G[x]==x) return x; else { int m=G[x]; G[x]=Find(G[x]); tran[x]+=tran[m]; return G[x]; }}void Set(){ int i; for(i=1;i<=n;i++) { G[i]=i; Rank[i]=1; tran[i]=0; }}void translate(int a,int b){ int p=Find(a),q=Find(b); if(p==q) return; G[p]=q; Rank[q]+=Rank[p]; Rank[p]=0; tran[p]++;}int main(){ int T,i,p,q,num=0; char ch[10]; scanf("%d",&T); while(T--) { scanf("%d %d",&n,&m); printf("Case %d:\n",++num); Set(); for(i=0;i<m;i++) { scanf("%s",ch); if(ch[0]=='T') { scanf("%d %d",&p,&q); translate(p,q); } else if(ch[0]=='Q') { scanf("%d",&p); q=Find(p); printf("%d %d %d\n",q,Rank[q],tran[p]); } } } return 0;}
0 0
- hdu3635(并查集)
- hdu3635(并查集)
- hdu3635并查集
- HDU3635(并查集)
- hdu3635并查集
- HDU3635:Dragon Balls(并查集)
- hdu3635之并查集
- HDU3635 Dragon Balls 并查集
- HDU3635 带权并查集
- HDU3635 Dragon Balls(并查集)
- hdu3635 Dragon Balls--并查集
- HDU3635 Dragon Balls(并查集)
- Dragon Balls(hdu3635带权并查集)
- 【hdu3635】Dragon Balls —— 并查集
- HDU3635 Dragon Balls 解题报告【带权并查集】
- 带权并查集--hdu3635 Dragon Balls
- 09-07 HDU_Steps5.1 并查集 HDU1829 HDU1325 HDU1598 HDU3461 HDU3635 HDU2473 HDU3172 HDU3038
- 并查集(集并查)
- 关于DB2归档新老参数
- 关于jquery $.fn
- ADVM 和 ACFS
- linux编程实践4(实现ls -l命令)
- 基于visual Studio2013解决算法导论之007优先队列(堆实现)
- hdu3635(并查集)
- 我对linux内核链表实现中的部分函数的理解——第二节
- Python中的property
- LSPCI命令详解分析
- FLEX和java web通过blazeds交互信息的实例
- MS-SQL 如何处理嵌套事务
- 工欲善其事,必先利其器系列--Netbeans之远程开发
- UVa 11624 Fire! / BFS
- DBMS_STATS.GATHER_SCHEMA_STATS介绍使用