Stack is a basic data structure. Where 3 operation can be done-

  1. Push: You can push object to the stack

  2. Pop: You can pop the object to the stack

  3. Top: You can check the value of the top object.

For further details you can get idea here ( if you really don’t know ) :https://en.wikibooks.org/wiki/Data_Structures/Stacks_and_Queues

Now we have a problem here, there are N stack in front of you. they are numbered from 1 to N. Each of them are initially empty. Now you will have Q operations. Each operation can be one the below 4 types:

  1. push i x, Push a value of x to stack numbered i

  2. pop i, Pop value from the stack numbered i, if stack is empty discard the operation

  3. put i j, Put the j’th stack on top of the i’th stack. So there will be no element left on the j’th stack.

  4. top i, Print the value of the top element of ith stack. If stack is empty print “Empty!”

Check the Sample IO for further understanding…

Input:

Input starts with an integer T (≤5), denoting the number of test cases.

The first line of a case is a blank line. The next line contains two integers N (1 ≤ N ≤ 104), Q(1 ≤ Q ≤ 5*104).

The next Q lines will contain a operation like above mentioned.

(1≤ I, j ≤ N), (1≤ x ≤ 105)

Output

For each test case, print the case number in a single line. Then for each 4th type operation you should print the value or “Empty!” if the stack is empty.

Input

Output

1

3 18

push 1 1

push 2 2

push 3 3

push 3 4

top 1

top 2

top 3

put 1 3

pop 2

top 1

top 2

top 3

pop 1

top 1

pop 1

top 1

pop 1

top 1

Case 1:

1

2

4

4

Empty!

Empty!

3

1

Empty!

Judge data is huge so use faster IO like scanf/printf

题意:模拟栈的操作。

思路:本来是水题一道,但是主要很细节,所以还是放这里,方便提醒自己注意细节。

push元素的时候更新栈首begin和栈定Laxt。

pop的时候如果空了,注意更新Begin=0;

put x到y的时候如果x为空或者x==y的时候不不操作。

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int Laxt[maxn],Begin[maxn],fa[maxn],cnt,num[maxn];
char c[];
int main()
{
int T,N,Q,Case=,x,i,j;
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&Q);
printf("Case %d:\n",++Case);
memset(Laxt,,sizeof(Laxt));
memset(Begin,,sizeof(Begin));
memset(fa,,sizeof(fa)); cnt=;
while(Q--){
scanf("%s",c);
if(c[]=='s'){ //push
scanf("%d%d",&i,&x);
num[++cnt]=x;
if(!Begin[i]) Begin[i]=cnt;
fa[cnt]=Laxt[i];
Laxt[i]=cnt;
}
else if(c[]=='t'){ //put
scanf("%d%d",&i,&j);
if(i!=j&&Laxt[j]){
fa[Begin[j]]=Laxt[i];
Laxt[i]=Laxt[j];
if(!Begin[i]) Begin[i]=Begin[j];
Laxt[j]=Begin[j]=;
}
}
else if(c[]=='p'){ // pop
scanf("%d",&i);
if(Laxt[i]) Laxt[i]=fa[Laxt[i]];
if(!Laxt[i]) Begin[i]=;
}
else { //top
scanf("%d",&i);
if(!Laxt[i]) printf("Empty!\n");
else printf("%d\n",num[Laxt[i]]);
}
}
}
return ;
}

SPOJ:Stack Overflow(并查集)的更多相关文章

  1. SPOJ IAPCR2F 【并查集】

    思路: 利用并查集/DFS都可以处理连通问题. PS:注意Find()查找值和pre[]值的区别. #include<bits/stdc++.h> using namespace std; ...

  2. SPOJ GSS4 Can you answer these queries IV ——树状数组 并查集

    [题目分析] 区间开方+区间求和. 由于区间开方次数较少,直接并查集维护下一个不是1的数的位置,然后暴力修改,树状数组求和即可. 这不是BZOJ上上帝造题7分钟嘛 [代码] #include < ...

  3. SPOJ:Lexicographically Smallest(并查集&排序)

    Taplu and Abhishar loved playing scrabble. One day they thought of inventing a new game using alphab ...

  4. SPOJ:Lost and survived(multiset+并查集)

    On September 22, 2004, Oceanic Flight 815 crashed on a mysterious island somewhere in the pacific. T ...

  5. SPOJ LEXSTR 并查集

    题目描述: Taplu and Abhishar loved playing scrabble. One day they thought of inventing a new game using ...

  6. “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)

    题意:初始时有个首都1,有n个操作 +V表示有一个新的城市连接到了V号城市 -V表示V号城市断开了连接,同时V的子城市也会断开连接 每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号 ...

  7. hdu 5652 India and China Origins 并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题目大意:n*m的矩阵上,0为平原,1为山.q个询问,第i个询问给定坐标xi,yi,表示i年后这 ...

  8. HDU 2545 树上战争 (并查集+YY)

    题意:给一棵树,如果树上的某个节点被某个人占据,则它的所有儿子都被占据,lxh和pfz初始时分别站在两个节点上,lxh总是先移动 ,谁当前所在的点被另一个人占据,他就输了比赛,问谁能获胜 比较有意思的 ...

  9. ACM: FZU 2112 Tickets - 欧拉回路 - 并查集

     FZU 2112 Tickets Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u P ...

  10. UVA 572 油田连通块-并查集解决

    题意:8个方向如果能够连成一块就算是一个连通块,求一共有几个连通块. 分析:网上的题解一般都是dfs,但是今天发现并查集也可以解决,为了方便我自己理解大神的模板,便尝试解这道题目,没想到过了... # ...

随机推荐

  1. 王垠:完全用Linux工作 (2003)

    完全用Linux工作,抛弃windows 我已经半年没有使用 Windows 的方式工作了.Linux 高效的完成了我所有的工作. GNU/Linux 不是每个人都想用的.如果你只需要处理一般的事务, ...

  2. Android-事件体系全面总结+实践分析

    事件分发在Android中是很重要的基础知识,网上相关的文章也很多,但是花了很多精力看了很多别人的分析总结,最终的感觉还是似懂非懂,所以决定自己动手研究一下,去发现其中的规律.本文顺着我自己的思路去研 ...

  3. Mysql导出导入相关操作记录

    一.使用source source sql脚本文件路径 二.使用mysqldump 命令行下具体用法如下:  mysqldump -u用户名 -p密码 -d 数据库名 表名 脚本名; 1.导出数据库為 ...

  4. UltraEdit UE如何取消保存文件自动备份

    高级-配置-文件处理-备份,设置为不备份                          

  5. 使用RPi-Monitor监控、统计Guitar的运行状态

    前言 之前发在ickey社区上的一系列文章: 犹抱琵琶半遮面,无人知是荔枝来--unboxing & interview 一.二.三 葡萄美酒夜光杯,巧妇难为无米炊--资料与社区 一支穿云箭, ...

  6. 吃我一记咸鱼突刺——使用板载RTC定时开机

    前言 原创文章,转载引用务必注明链接.水平有限,欢迎指正. 2016年3月30日 Lemuntu(Base On Jessie) 3.10.37 原载于Lemaker论坛.汇总于此. 看ATC2603 ...

  7. iphone 消息推送 实现

    IPhone 消息推送实现 参考 资料 http://blog.csdn.net/victormokai/article/details/39501277 对生成pem 的补充 拿到mac 上生成导出 ...

  8. mysql 同样内容的字段合并为一条的方法

    从两个表中内联取出的数据,当中category_name字段有同样内容,想将具有同样内容的字段进行合并,将amount字段进行加法运算,变成下表中的内容 url=http%3A%2F%2Fdev.my ...

  9. 基于社交网络的情绪化分析IV

    基于社交网络的情绪化分析IV By 白熊花田(http://blog.csdn.net/whiterbear) 转载需注明出处,谢谢. 前面进行了微博数据的抓取,简单的处理,类似度分析.后面两篇进行学 ...

  10. 编资源bundle时图片文件变成tiff的解决方法

    一般,编写SDK的时候,如果SDK还带了一些资源文件,那么最理想的是将资源文件也打包成为bundle给应用方一起使用.而在编资源bundle时,有时会发现编译好后的图片文件从png转成了tiff,这样 ...