Cube Stacking
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 23678   Accepted: 8299
Case Time Limit: 1000MS

Description

Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)identical cubes labeled 1 through N. They start with N stacks, each containing a single cube. Farmer John asks Betsy to perform P (1<= P <= 100,000) operation. There are two types of operations: 
moves and counts. 
* In a move operation, Farmer John asks Bessie to move the stack containing cube X on top of the stack containing cube Y. 
* In a count operation, Farmer John asks Bessie to count the number of cubes on the stack with cube X that are under the cube X and report that value.

Write a program that can verify the results of the game. 

Input

* Line 1: A single integer, P

* Lines 2..P+1: Each of these lines describes a legal operation. Line 2 describes the first operation, etc. Each line begins with a 'M' for a move operation or a 'C' for a count operation. For move operations, the line also contains two integers: X and Y.For
count operations, the line also contains a single integer: X.

Note that the value for N does not appear in the input file. No move operation will request a move a stack onto itself. 

Output

Print the output from each of the count operations in the same order as the input file. 

Sample Input

6
M 1 6
C 1
M 2 4
M 2 6
C 3
C 4

Sample Output

1
0
2

Source

题目链接:POJ 1988

以前看了很久的题解但是完全看不懂,也是最近会了食物链之后才做的。

既然食物链可以用数值代表关系,那这题只要把取模去掉就可以用数值node[x].up代表自己与祖节点之间的砖块数(不包括本身),然后还有一个问题就是还要知道自己所在砖块堆的总个数node[fx].total(fx是此砖块堆的祖节点即代表)。

那统计up用食物链的思想比较简单就可以做到,关键就是total怎么算,可以设想(画图更好理解),把砖a所在堆放到砖b所在堆,那node[fa].total肯定增加了node[fb].total个,然后node[fa].up显然不变(实际上祖节点就是堆的最上面那块砖的号码),node[fb].total就不要动了,因为只有祖节点才能具有记录total的功能,fb此时已沦为fa下面的一个子节点,node[fb].up还是可以动的,即增加了node[fa].total个。然后这题就差不多解决了,最后输出的时候用本堆砖块数total-本砖块上面的砖块数up-1,减1是因为题目要求只算压在自己这块砖下面的,自己当然不算

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=300010;
struct info
{
int pre;
int up;
int total;
};
info node[N];
void init(int n)
{
for (int i=0; i<=n; ++i)
{
node[i].pre=i;
node[i].up=0;
node[i].total=1;
}
}
int find(int n)
{
if(node[n].pre==n)
return n;
int tpre=node[n].pre;
node[n].pre=find(node[n].pre);
node[n].up=node[n].up+node[tpre].up;
return node[n].pre;
}
void joint(int a,int b)
{
int fa=find(a),fb=find(b);
if(fa!=fb)
{
node[fb].pre=fa;
node[fb].up+=node[fa].total;
node[fa].total+=node[fb].total;
}
}
int main(void)
{
int n,i,j,a,b,x;
char ops[3];
while (~scanf("%d",&n))
{
init(n);
for (i=0; i<n; ++i)
{
scanf("%s",ops);
if(ops[0]=='M')
{
scanf("%d%d",&a,&b);
joint(a,b);
}
else
{
scanf("%d",&x);
int fx=find(x);
printf("%d\n",node[fx].total-node[x].up-1);
}
}
}
return 0;
}

POJ 1988 Cube Stacking(带权并查集)的更多相关文章

  1. POJ 1988 Cube Stacking(带权并查集)

    哈哈,一次AC. 题意:给你 1-n 编号的立方体,然后移动包含指定编号的立方体的堆移到另一个堆上边, 询问指定的编号立方体下面有多少个立方体. 思路:由于并查集是存储的是它的父亲,那么只能从父亲那里 ...

  2. poj1988 Cube Stacking 带权并查集

    题目链接:http://poj.org/problem?id=1988 题意:有n个方块,编号为1-n,现在存在两种操作: M  i  j  将编号为i的方块所在的那一堆方块移到编号为j的方块所在的那 ...

  3. USACO2004 cube stacking /// 带权并查集 oj1302

    题目大意: 以N ( 1 ≤ N ≤ 30,000 )个堆栈开始,每个堆栈包含一个单独的立方体.执行P(1≤ P ≤100,000)的操作. 有两种类型的操作:移动和计数. *在移动操作中,将 包含方 ...

  4. POJ 1773 Parity game 带权并查集

    分析:带权并查集,就是维护一堆关系 然后就是带权并查集的三步 1:首先确定权值数组,sum[i]代表父节点到子节点之间的1的个数(当然路径压缩后代表到根节点的个数) 1代表是奇数个,0代表偶数个 2: ...

  5. POJ 1182 食物链 【带权并查集】

    <题目链接> 题目大意: 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我 ...

  6. POJ 1182 食物链 (带权并查集)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 78551   Accepted: 23406 Description ...

  7. POJ 1182 食物链 【带权并查集/补集法】

    动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种.有人用两种说 ...

  8. POJ 1733 Parity game (带权并查集)

    题意:有序列A[1..N],其元素值为0或1.有M条信息,每条信息表示区间[L,R]中1的个数为偶数或奇数个,但是可能有错误的信息.求最多满足前多少条信息. 分析:区间统计的带权并查集,只是本题中路径 ...

  9. poj 1182 食物链【带权并查集】

    设相等的边权为0,吃的边权为,被吃的边权为2,然后用带权并查集在%3的意义下做加法即可 关系为简单环的基本都可以用模环长的方式是用带权并查集 #include<iostream> #inc ...

  10. A Bug's Life POJ - 2492 (种类或带权并查集)

    这个题目的写法有很多,用二分图染色也可以写,思路很好想,这里我们用关于并查集的两种写法来做. 题目大意:输入x,y表示x和y交配,然后判断是否有同性恋. 1 带权并查集: 我们可以用边的权值来表示一种 ...

随机推荐

  1. jsp回车键登录代码

    $("body").keydown(function(event) { if (event.keyCode == "13") {// keyCode=13是回车 ...

  2. HDU1003MAX SUM

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  3. 比较StringBuffer字符串内容是否相等?

    为什么会有这个问题呢?首先得看看String和StringBuffer的比较区别: ==只能比较两个字符串的内存地址是否一样,不能比较字符串内容: String的equals方法因为重写了Object ...

  4. javaWeb项目中web.xml的xsd( XML Schemas Definition)文件

    <?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns="http://w ...

  5. 在MongoDB中使用JOIN操作

    SQL与NoSQL最大的不同之一就是不支持JOIN,在传统的数据库中,SQL JOIN子句允许你使用普通的字段,在两个或者是更多表中的组合表中的每行数据.例如,如果你有表books和publisher ...

  6. 安卓通过putExtra传递数据的几种方式

    通过intent传递数据时,使用以下代码报错: hMap<string, object=""> map=(Map<string, object="&qu ...

  7. html 音频视频

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. JVM的类加载机制

    虚拟机把描述类的数据从Class文件加载到内存,并对数据进行校验.转换解析和初始化,最终形成可以被虚拟机直接使用的Java类型,这就是虚拟机的类加载机制. 类加载的过程: 包括加载.链接(含验证.准备 ...

  9. Xamarin Android项目运行失败

    Xamarin Android项目运行失败 错误信息:Build Failed: MonoDroid does not support running the previous version.  P ...

  10. 如何选中一个Checkbox,设置无效?

    document.all.cb1[0].disabled = true;