Box Relations
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1033 Accepted Submission(s): 384
Special Judge
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):
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.
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
Source
题目分析:
首先是长方体的关系确定的方法,就是如果相交,那么就是每个方向上a < b+n ,b<a+n,类似于线段树中向下查询区间的判断,其他的就是直接判断大小,建单向边,然后拓扑排序即可,很恶心的模拟题
首先是长方体的关系确定的方法,就是如果相交,那么就是每个方向上a < b+n ,b<a+n,类似于线段树中向下查询区间的判断,其他的就是直接判断大小,建单向边,然后拓扑排序即可,很恶心的模拟题
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#define MAX 10007
using namespace std;
int n,m;
int ans_x[MAX<<1];
int ans_y[MAX<<1];
int ans_z[MAX<<1];
int in[5][MAX<<1];
vector<int> seq[5];
char s[5];
struct Edge
{
int v,next;
}e[4][MAX*50];
int head[4][MAX];
int cc;
void add ( int u , int v , int flag )
{
e[flag][cc].v = v;
e[flag][cc].next = head[flag][u];
head[flag][u] = cc++;
in[flag][v]++;
}
bool topSort ( )
{
for ( int i = 1 ; i < 4 ; i++ )
{
seq[i].clear();
queue<int> q;
for ( int j = 1 ; j <= 2*n ; j++ )
if ( !in[i][j] )
{
in[i][j]--;
q.push( j );
}
int num = 0;
while ( !q.empty() )
{
int u = q.front();
q.pop();
num++;
seq[i].push_back ( u );
for ( int j = head[i][u] ; ~j ; j = e[i][j].next )
{
int v = e[i][j].v;
in[i][v]--;
if ( in[i][v] == 0 )
{
q.push ( v );
in[i][v]--;
}
}
}
if ( num != 2*n ) return false;
}
return true;
}
int main ( )
{
int a,b,c=1;
while ( ~scanf ( "%d%d" , &n , &m ) , n+m )
{
memset ( head , -1 , sizeof ( head ) );
memset ( in , 0 , sizeof ( in ) );
cc = 0;
for ( int i = 1 ; i <= n ; i++ )
for ( int j = 1 ; j <= 3 ; j++ )
add ( i, i+n , j );
for ( int i = 0 ; i < m ; i++ )
{
scanf ( "%s" , s );
scanf ( "%d%d" , &a , &b );
if ( s[0] == 'I' )
for ( int k = 1 ; k < 4 ; k++ )
{
add ( a , b+n , k );
add ( b , a+n , k );
}
if ( s[0] == 'X' )
{
//add ( a , a+n , 1 );
//in[1][a+n]++;
add ( a+n , b , 1 );
//add ( b , b+n , 1 );
//in[1][b+n]++;
}
if ( s[0] == 'Y' )
{
//add ( a , a+n , 2 );
//in[2][a+n]++;
add ( a+n , b , 2 );
//add ( b , b+n , 2 );
//in[2][b+n]++;
}
if ( s[0] == 'Z' )
{
//add ( a , a+n , 3 );
//in[3][a+n]++;
add ( a+n , b , 3 );
//add ( b , b+n , 3 );
//in[3][b+n]++;
}
}
printf ( "Case %d: " , c++ );
if ( !topSort ( ) )
{
printf ( "IMPOSSIBLE\n" );
puts ("");
continue;
}
for ( int i = 1 ; i < 4 ; i++ )
{
int num = 0;
int len = seq[i].size();
for ( int j = 0 ; j < len ; j++ )
if ( i == 1 )
ans_x[seq[i][j]] = num++;
else if ( i == 2 )
ans_y[seq[i][j]] = num++;
else if ( i == 3 )
ans_z[seq[i][j]] = num++;
}
puts ("POSSIBLE");
for ( int i = 1 ; i <= n ; i++ )
printf ( "%d %d %d %d %d %d\n" , ans_x[i] , ans_y[i] , ans_z[i] , ans_x[i+n] , ans_y[i+n] , ans_z[i+n] );
puts ("");
}
}