码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1988 Cube Stacking(转)

时间:2014-08-13 12:57:16      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:style   color   os   io   for   ar   amp   line   

这道题的思路,就是每次记下该点到父结点的个数,并记录下其下的结点个数。之后,每次"C"时,将总的减去它所压的方块,即答案!!!(也是参考别人的~-?)

<pre name="code" class="cpp">#include<stdio.h>
#include<iostream>
using namespace std;
#define max 30010
struct node
{
	int parent;//最上面的元素
	int up;//   上面的个数
	int down;// 下面的个数
};
struct node st[max];
int find(int x)
{
	int p;
	if(st[x].parent !=x)
	{
		p=st[x].parent ;
		st[x].parent =find(st[x].parent);
		st[x].up+=st[p].up;
	}
	return st[x].parent ;
}
void uion(int x,int y)
{
	x=find(x);
	y=find(y);
	st[y].parent=x;
	st[y].up=st[x].down ;
	st[x].down+=st[y].down ;
}
int main()
{
	char s[3];
	int i,x,y;
	int parent;
	int n;
	scanf("%d",&n);
	for(i=0;i<max;i++)
	{
		st[i].parent=i;
		st[i].down=1;
		st[i].up=0;
	}
	while(n--)
	{
		scanf("%s",s);
		if(s[0]==‘M‘)
		{
			scanf("%d%d",&x,&y); 
			uion(x,y);
		}
		else if(s[0]==‘C‘)
		{
			scanf("%d",&x);
			parent=find(x);
			printf("%d\n",st[parent].down -st[x].up -1);
		}
	}
	return 0;
}



 

POJ 1988 Cube Stacking(转),布布扣,bubuko.com

POJ 1988 Cube Stacking(转)

标签:style   color   os   io   for   ar   amp   line   

原文地址:http://www.cnblogs.com/bofengyu/p/3909734.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!