poj1988Cute Stacking
2024-08-31 00:52:23
题目大意:有几个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 ;
}
心若向阳,无谓悲伤
最新文章
- Java Web ——http协议响应报文
- spring3.0使用annotation完全代替XML(三)
- JavaScript toLowerCase() 方法 把字符串转换为小写
- Runloop之个人理解
- JavaScript AJAX stream 流式显示
- mysql loop if
- c printf
- CSS负边距自适应布局三例
- 内容提供者 DocumentProvider Uri工具类
- IsNullOrEmpty和s == null || s.Length == 0哪个快
- 编写一个程序实现strcpy函数的功能
- JS判断doctype文档模式-document.compatMode
- java中File类中list()和listFiles()方法区别
- window批处理修改计算机名
- 移动端click事件出现300ms延迟
- (转载)Unity学习笔记:关于Dropdown的学习
- js实现滑动的弹性导航
- ultragrid
- 有用的Javascript,长期更新...
- QT帮助文档 英文
热门文章
- 作为一个程序员,分享一下我自己常用的几款非常方便的chrome插件
- 1.Ubuntu查看Python版本
- Trees on the level (二叉链表树)
- 3.6.5 空串与Null串
- Codeforces 300E(数学)
- 非常适合新手的jq/zepto源码分析06 -- 事件模型
- NETCore项目报错 An error occurred while starting the application
- MySQL: 改变Homebrew安装MySQL/MariaDB的数据库文件目录
- PHP array_pad()
- The Breakpoint will not currently be hit. No executable code associated with this line