Box Relations //2009 Asia Wuhan Regional Contest Hosted by Wuhan University 拓扑排序

Box Relations

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 301    Accepted Submission(s): 94
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 <=  ni 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
 

Source
2009 Asia Wuhan Regional Contest Hosted by Wuhan University
 

Recommend
chenrui

 

 

 

#include<stdio.h>

#include<string.h>

int n,r,e;

struct NODE

{

    int v,next;

}p[900010];

int point[4][2010];

int q[4][2010],in[4][2010],ans[4][2010];

 

void addedge(int type,int u,int v)

{

    p[e].v=v;

    p[e].next=point[type][u];

    point[type][u]=e++;

    in[type][v]++;

}

 

void init()

{

    e=0;

    memset(in,0,sizeof(in));

    memset(point,-1,sizeof(point));

    for(int i=1;i<=n;i++)

      for(int j=1;j<=3;j++)

          addedge(j,i,i+n);

}

 

bool tuopu(int type)

{

    int front=0,top=0;

    for(int i=1;i<=2*n;i++)

    if(in[type][i]==0) q[type][top++]=i,in[type][i]--;

    while(front<top)

    {

        int u=q[type][front++];

        for(int i=point[type][u];i!=-1;i=p[i].next)

        {

            int v=p[i].v;

            in[type][v]--;

            if(in[type][v]==0)  q[type][top++]=v,in[type][v]--;

        }

    }

    return top==2*n;

}

void solve()

{

    for(int i=1;i<=3;i++)

    {

        if(!tuopu(i))

        {

            puts("IMPOSSIBLE");

            return ;

        }

    }

    puts("POSSIBLE");

    for(int j=1;j<=3;j++)

      for(int i=0;i<2*n;i++)

      ans[j][q[j][i]]=i;

     for(int i=1;i<=n;i++)

     printf("%d %d %d %d %d %d/n",ans[1][i],ans[2][i],ans[3][i],ans[1][i+n],ans[2][i+n],ans[3][i+n]);

}

 

int main()

{

    char str[10];

    int x,y,cas=1;

    while(scanf("%d%d",&n,&r)!=EOF)

    {

        if(n==0 && r==0) break;

        init();

        for(int i=1;i<=r;i++)

        {

            scanf("%s %d %d",str,&x,&y);

            if(str[0]=='I')

            {

                for(int j=1;j<=3;j++)

                {

                    addedge(j,x,y+n);

                    addedge(j,y,x+n);

                }

            }

            else if(str[0]=='X') addedge(1,x+n,y);

            else if(str[0]=='Y') addedge(2,x+n,y);

            else if(str[0]=='Z') addedge(3,x+n,y);

        }

        printf("Case %d: ",cas++);

        solve();

        puts("");

    }

    return 0;

}

你可能感兴趣的:(input,each,IIS,output,parallel,2010)