hdu-2818-Building Block-带权并查集
2016-07-17 21:44
239 查看
http://acm.hdu.edu.cn/showproblem.php?pid=2818
有n条管pipe,能把砖块串起来,初始每条管穿了一个砖
题意: 有2种操作:
M x y把x的管放在y管的上面
C x求x的下面有多少个砖块
带权并查集,维护一个num[i]表示i到根节点之间有多少块砖,再维护一个tol[x]数组,表示管x上一共串有多少个砖块
合并就很简单了:if (fx!=fy)
{
fa[fx]=fy;
num[fx]=tol[fy];<span style="white-space:pre"> </span>
tol[fy]+=tol[fx];
}
有n条管pipe,能把砖块串起来,初始每条管穿了一个砖
题意: 有2种操作:
M x y把x的管放在y管的上面
C x求x的下面有多少个砖块
带权并查集,维护一个num[i]表示i到根节点之间有多少块砖,再维护一个tol[x]数组,表示管x上一共串有多少个砖块
合并就很简单了:if (fx!=fy)
{
fa[fx]=fy;
num[fx]=tol[fy];<span style="white-space:pre"> </span>
tol[fy]+=tol[fx];
}
#include <cstdio> #include <cmath> #include <cstring> #include <string> #include <algorithm> #include <queue> #include <map> #include <set> #include <vector> #include <iostream> using namespace std; const double pi=acos(-1.0); double eps=0.000001; int fa[31234]; int num[31234]; int tol[31234]; int find(int x) { if (fa[x]==x) return x; int tmp=find(fa[x]); num[x]=num[x]+num[fa[x]]; return fa[x]=tmp; } int main() { int n,m,k; while(cin>>n) { int x,y,z; char s[3]; for (int i=0; i<=31000; i++)fa[i]=i,num[i]=0,tol[i]=1; for (int i=1; i<=n; i++) { scanf("%s",s); if (s[0]=='M') { scanf("%d%d",&x,&y); int fx=find(x); int fy=find(y); if (fx!=fy) { fa[fx]=fy; num[fx]=tol[fy]; tol[fy]+=tol[fx]; } } else { scanf("%d",&x); find(x); printf("%d\n",num[x ]); } } } return 0; }
相关文章推荐
- php 序列化储存和转化 json_encode() json_decode($q,true)
- Android UI设计(引导界面):ViewPager之三PagerTabStrip与PagerTitleStrip添加标题栏的区别
- codeforces 234E Champions' League
- sap gui 740 mac 下载
- uboot启动zImage(go)和uImage(bootm)分析
- storyboard 控件设置autolayout没起作用
- 【转】编译quickfast解析库(沪深level2行情转码库)
- 三、Android Call requires API level 11 (current min is 8)
- 【C#】StringBuilder和string对比
- Junit测试【2】(详解suit函数)
- org.hibernate.loader.custom.NonUniqueDiscoveredSqlAliasException: Encountered a duplicated sql alias
- getpwuid()
- 如何生成uimage
- Result Maps collection already contains value for
- 'setting build path' has encountered
- iOS开发从入门到精通--UIViewController使用
- DRUID连接池的实用 配置详解
- 超级精简版/超精简/懂你版IDM 6.25 build 23推荐
- CodeForces 175D Plane of Tanks: Duel (概率)
- UILabel,文字添加下划线,中划线