HDU 3231 Box Relations 拓扑排序+构造

http://acm.hdu.edu.cn/showproblem.php?pid=3231
Problem Description
There are n boxes C1, C2, …, Cn in 3D space. The edges of the boxes are parallel to the x, y or z-axis. We provide some relations of the boxes, and your task is to construct a set of boxes satisfying all these relations.

There are four kinds of relations (1 <= i,j <= n, i is different from j):
I i j: The intersection volume of Ci and Cj is positive.

X i j: The intersection volume is zero, and any point inside Ci has smaller x-coordinate than any point inside Cj.

Y i j: The intersection volume is zero, and any point inside Ci has smaller y-coordinate than any point inside Cj.

Z i j: The intersection volume is zero, and any point inside Ci has smaller z-coordinate than any point inside Cj.
.

Input
There will be at most 30 test cases. Each case begins with a line containing two integers n (1 <= n <= 1,000) and R (0 <= R <= 100,000), the number of boxes and the number of relations. Each of the following R lines describes a relation, written in the format above. The last test case is followed by n=R=0, which should not be processed.

Output
For each test case, print the case number and either the word POSSIBLE or IMPOSSIBLE. If it’s possible to construct the set of boxes, the i-th line of the following n lines contains six integers x1, y1, z1, x2, y2, z2, that means the i-th box is the set of points (x,y,z) satisfying x1 <= x <= x2, y1 <= y <= y2, z1 <= z <= z2. The absolute values of x1, y1, z1, x2, y2, z2 should not exceed 1,000,000.

Print a blank line after the output of each test case.

Sample Input
3 2
I 1 2
X 2 3
3 3
Z 1 2
Z 2 3
Z 3 1
1 0
0 0

Sample Output
Case 1: POSSIBLE
0 0 0 2 2 2
1 1 1 3 3 3
8 8 8 9 9 9

Case 2: IMPOSSIBLE

Case 3: POSSIBLE
0 0 0 1 1 1
题目大意:给出 n n n B o x Box Box m m m个关系,边均平行与坐标轴, 让你构造这样的 n n n B o x Box Box, 使它们以下关系:(1) I   i   j I\ i\ j I i j :表示 B o x i Box_{i} Boxi B o x j Box _{j} Boxj相交;(2) X   i   j X\ i\ j X i j : 表示 B o x i Box_{i} Boxi的任意点的 x x x坐标小于 B o x j Box_{j} Boxj的任意点的 x x x坐标;(3) Y   i   j Y\ i\ j Y i j ::表示 B o x i Box_{i} Boxi的任意点的 y y y坐标小于 B o x j Box_{j} Boxj的任意点的 y y y坐标;(4) Z   i   j Z\ i\ j Z i j :表示 B o x i Box_{i} Boxi的任意点的 z z z坐标小于 B o x j Box_{j} Boxj的任意点的 z z z坐标。如果可以构造出来的话,输出这 n n n B o x Box Box底面左上方顶点和顶面右下方顶点的坐标。

思路:可以单独考虑每一维,这里以 x x x坐标为例,如果我们能得到确切的大小关系,比如 x i < x j < x k x_{i}<x_{j}<x_{k} xi<xj<xk,那么是很好构造的,可以令 B o x i Box_{i} Boxi的两个顶点的横坐标分别为 1 、 2 1、2 12 B o x j Box_{j} Boxj的分别为 3 、 4 3、4 34 B o x k Box_{k} Boxk的分别为 5 、 6 5、6 56,如果我们不知道确切的大小关系,比如 x i < x j x_{i}<x_{j} xi<xj x i < x k x_{i}<x_{k} xi<xk,此时 B o x j Box_{j} Boxj B o x k Box_{k} Boxk的关系我们并不知道,也就是这两个谁大谁小并没有关系,可以随便选一个。这个过程其实和拓扑排序的过程很像,考虑怎么给这些点连边。首先只有 n n n B o x Box Box,所以我们可以考虑令 [ 1 , n ] [1,n] [1,n]表示这 n n n B o x Box Box左下角的顶点编号, [ n + 1 , 2 n ] [n+1,2n] [n+1,2n]表示这 n n n B o x Box Box右上角的顶点编号,对于任意一个维度,只要 v i < v j v_{i}<v_{j} vi<vj,我们就在这个维度下连一条从 i i i j j j的有向边,这样做拓扑排序点 i i i一定在点 j j j之前被选中,因此给 i i i分配的值一定比给 j j j分配的值小。会连边的话这题就没什么好说的了,不过要注意 B o x i Box_{i} Boxi完全包含 B o x j Box_{j} Boxj的情况也属于相交

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#define pr pair<long long,int>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;

const int maxm=1e5+5;

int n,m,top;
int head[3][2005],ans[3][2005],Stack[2005],ind[2005],tot[3];
char op[10];

struct Edge
{
	int to,nxt;
}edge[3][maxm*3];

void addedge(int id,int u,int v)
{
	edge[id][++tot[id]].to=v,edge[id][tot[id]].nxt=head[id][u],head[id][u]=tot[id];
}

void init()
{
	memset(head,0,sizeof(head));
	tot[0]=tot[1]=tot[2]=0;
	for(int i=0;i<3;i++) //三维
		for(int j=1;j<=n;j++)
			addedge(i,j,j+n);
}

bool topo(int id)
{
	memset(ind,0,sizeof(ind));
	top=0;
	int nn=n*2;
	for(int i=1;i<=nn;i++)
		for(int j=head[id][i];j;j=edge[id][j].nxt)
			ind[edge[id][j].to]++;
	for(int i=1;i<=nn;i++)
		if(ind[i]==0)
			Stack[++top]=i;
	int cnt=0,tmp;
	while(top!=0)
	{
		tmp=Stack[top--];
		ans[id][tmp]=++cnt;
		for(int i=head[id][tmp];i;i=edge[id][i].nxt)
			if(--ind[edge[id][i].to]==0)
				Stack[++top]=edge[id][i].to;
	}
	return cnt==nn?1:0;
}

int main()
{
	int times=0;
	while(~scanf("%d%d",&n,&m)&&(n||m))
	{
		init();
		int u,v;
		for(int i=0;i<m;i++)
		{
			scanf("%s%d%d",op,&u,&v);
			if(op[0]=='I')
			{
				for(int i=0;i<3;i++)//三维
					addedge(i,u,v+n),addedge(i,v,u+n);
			}
			else
				addedge(op[0]-'X',u+n,v);
		}
		bool flag=0;
		for(int i=0;i<3;i++)
			if(!topo(i))
				flag=1;
		printf("Case %d: ",++times);
		if(flag)
			printf("IMPOSSIBLE\n");
		else
		{
			printf("POSSIBLE\n");
			for(int i=1;i<=n;i++)
				printf("%d %d %d %d %d %d\n",ans[0][i],ans[1][i],ans[2][i],ans[0][i+n],ans[1][i+n],ans[2][i+n]);
		}
		printf("\n");
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值