题目大意:有几个stack,初始里面有一个cube。支持两种操作:1.move x y: 将x所在的stack移动到y所在stack的顶部。2.count x:数在x所在stack中,在x之下的cube的个数。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#define maxn 300001 using namespace std;
int n,x,y;char c;
int cnt[maxn],sum[maxn],fa[maxn]; int find(int x)
{
if(fa[x]==x) return fa[x];
int f=fa[x];
fa[x]=find(fa[x]);
cnt[x]+=cnt[f];//cnt[i]表示i上面有几个。
return fa[x];
} void merge(int b,int a)
{
fa[a]=b;//将a接到b上
cnt[a]=sum[b];//a的位置就是b里面的个数。
sum[b]+=sum[a];//更新b的总数
sum[a]=;//更新a的总数。
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
fa[i]=i;
sum[i]=;
}
for(int i=;i<=n;i++)
{
cin>>c;
if(c=='M')
{
scanf("%d%d",&x,&y);
int l1=find(x);
int l2=find(y);
merge(l1,l2);
}
else
{
scanf("%d",&x);
int k=find(x);
cout<<sum[k]-cnt[x]-<<endl;//sum表示总数
}
}
return ;
}

心若向阳,无谓悲伤

最新文章

  1. Java Web ——http协议响应报文
  2. spring3.0使用annotation完全代替XML(三)
  3. JavaScript toLowerCase() 方法 把字符串转换为小写
  4. Runloop之个人理解
  5. JavaScript AJAX stream 流式显示
  6. mysql loop if
  7. c printf
  8. CSS负边距自适应布局三例
  9. 内容提供者 DocumentProvider Uri工具类
  10. IsNullOrEmpty和s == null || s.Length == 0哪个快
  11. 编写一个程序实现strcpy函数的功能
  12. JS判断doctype文档模式-document.compatMode
  13. java中File类中list()和listFiles()方法区别
  14. window批处理修改计算机名
  15. 移动端click事件出现300ms延迟
  16. (转载)Unity学习笔记:关于Dropdown的学习
  17. js实现滑动的弹性导航
  18. ultragrid
  19. 有用的Javascript,长期更新...
  20. QT帮助文档 英文

热门文章

  1. 作为一个程序员,分享一下我自己常用的几款非常方便的chrome插件
  2. 1.Ubuntu查看Python版本
  3. Trees on the level (二叉链表树)
  4. 3.6.5 空串与Null串
  5. Codeforces 300E(数学)
  6. 非常适合新手的jq/zepto源码分析06 -- 事件模型
  7. NETCore项目报错 An error occurred while starting the application
  8. MySQL: 改变Homebrew安装MySQL/MariaDB的数据库文件目录
  9. PHP array_pad()
  10. The Breakpoint will not currently be hit. No executable code associated with this line