POJ 1988 Cube Stacking( 带权并查集 ) 非常棒的一道题!借鉴"找回失去的"博客 链接:传送门 题意: P次查询,每次查询有两种: M x y 将包含x的集合移动到y上面 C x, 计算x下面有几个元素.用p[x]表示x的根结点, 思路:cnt[x] 表示x所在集合的元素个数,top[x] 表示x上面有几个元素.每次进行路径压缩时,top[x] 都要加上 top[ par[x] ],cnt 和 par 的操作就是并查集的基本操作.最后计算结果是用 x 所在集合元素…
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 w…
设s[x]为x所在栈里的个数,c[x]表示x下面有几个,合并的时候直接合并s,然后路径压缩的时候更新c即可 #include<iostream> #include<cstdio> using namespace std; const int N=30005; int n=30000,m,f[N],s[N],c[N]; char o[5]; int read() { int r=0,f=1; char p=getchar(); while(p>'9'||p<'0') {…
Cube Stacking 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) op…