POJ 1988 Cube Stacking

Source: Internet
Author: User

Main topic:

There are a bunch of identical cubes, a total of N (1≤n≤30,000), numbered 1 ~ N, at the beginning each cube is a heap (that is, only one cube per heap), there are two operations, the first is "M x Y", meaning that the heap of the number X cube is stacked on top of the heap where Y is located, The second operation is "C X", which means that you are required to output how many cubes are under the X cube.

Now there is only one example, the beginning will give the operand P (1≤p≤100,000), followed by the order of the operation, but the input does not give n, the input is guaranteed not to be a heap heap on their own top of the operation, as required output each time "C" query can be.

Topic links

Comment Code:

/* * Problem ID:POJ 1988 Cube Stacking * Author:lirx.t.una * language:c++ * Run time:282 MS * Run memory:332 KB */#include <stdio.h>//Cube Maximum number of #define MAXN 30000 short FATH[MAXN + 1];//and check set, range not exceeding short//indicates the number of nodes on the tree with I as root//root storage is always stacked at the bottom of the block//Because the total number does not exceed the total number of cubes, the range is also
No more than short short SUM[MAXN + 1]; Relationship, Under[i] represents the number of squares between I and fath[i]//Sometimes this quantity does not include I and fath[i] this two//sometimes this quantity is the final answer//when no path compression is the previous//when the path is compressed after the number is the latter a short under[

MAXN + 1];
	int find (int x) {int fx;
	
	int root;
	
	if (x = = Fath[x]) return x;
	First, the path compression, this operation to ensure that the relationship between FX and Root is correct//and FX has been compressed by the path, so UNDER[FX] is the correct answer root = find (FX = fath[x]);
	
	The x is also connected to the root, but before that, the relationship between X and Gen needs to be updated, as long as the relationship between X and FX and the relationship between the FX and the root can pass through the relationship between X and the root under[x] + = Under[fx];
	
	return fath[x] = root;//finally again x on the root} void merge (int x, int y) {int fx, FY;
	FX = find (x);
	
	FY = find (y); if (FX! = FY) {FATH[FX] = fy;//stacks FX on the same heap as the FY//Because FX is directly connected to the FY, it is equivalent to the FXPath compression//So the relationship between FX and FY should correctly reflect how many cubes are under FX//So UNDER[FX] should be the correct answer under[fx] = Sum[fy];
	Sum[fy] + = sum[fx];//Update the total number of FY heaps}} int main () {int p;//query count char cmd;
	
	int x, y;//accepts the temporary cube number int i;
		for (i = 1; I <= maxn; i++) {//initialize fath[i] = i;
	Sum[i] = 1;
	} scanf ("%d", &p);
		while (p--) {scanf ("\n%c", &cmd);
				Switch (cmd) {case ' M ': scanf ("%d%d", &x, &y);
				Merge (x, y);
			
			Break
				Case ' C ': scanf ("%d", &i);
				Find (i);//first path compression, so I received on the root of printf ("%d\n", Under[i]);
				
			Break
		Default:break;
}} return 0; }
No comment code:

#include <stdio.h>

#define	maxn		30000

Short	FATH[MAXN + 1];
Short	SUM[MAXN + 1];
Short	UNDER[MAXN + 1];

int
Find (int x) {

	int		fx;
	int		root;

	if (x = = Fath[x]) return x;

	Root = Find (FX = fath[x]);
	UNDER[X] + = Under[fx];

	return fath[x] = root;
}

void
Merge (int x, int y) {

	int		fx, FY;

	FX = find (x);
	FY = find (y);

	if (FX! = FY) {

		fath[fx]   = FY;
		UNDER[FX]  = Sum[fy];
		SUM[FY]   + = Sum[fx];}
}

int
Main () {

	int		p;
	Char	cmd;
	int		x, y;
	
	int		i;

	for (i = 1; I <= maxn; i++) {
		
		fath[i] = i;
		Sum[i]  = 1;
	}

	scanf ("%d", &p);
	while (p--) {

		scanf ("\n%c", &cmd);
		Switch (CMD) {case
		
			' M ':

				scanf ("%d%d", &x, &y);
				Merge (x, y);
				break;

			Case ' C ':

				scanf ("%d", &i);
				Find (i);
				printf ("%d\n", Under[i]);
				break;

			Default:break;
		}
	}

	return 0;
}
Word Explanation:

VERIFY:VT, check, verify.

Bessie: Bessie (Woman's name)

Cube:n, Cube

Identical:adj, the same, exactly the same.

Betsy: Becky (Woman's name, equals Elizabeth)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.