hdu 3231 Box Relations(拓扑排序)

Box Relations

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1108    Accepted Submission(s): 420
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 followingn 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
 

题意:三维座标下让你构造满足条件的n个长方体。有R个要求,I i j表示第i个长方体和第j个长方体必须有交集;   X  i  j 表示 i 任意一点的x座标必须小于 j 的;

            Y i j ,Z i j同理。

题解 拓扑排序。分别把三个面拆成三条线段,再拆成两个点,根据要求构造关系,再进行三次拓扑排序即可。

#include<cstring>
#include<algorithm>
#include<cstdio>
#include<iostream>
#include<queue>
#include<set>
using namespace std;

const int N=2005;
const int M=2e5+5;

int n,m;
int c[N];
int topo[N],t;
vector<int>X[N],Y[N],Z[N];
int ans[3][N];

void init() {
    for(int i=0; i<=2*n; i++) {
        X[i].clear();
        Y[i].clear();
        Z[i].clear();
    }
    for(int i=0; i<n; i++) {
        X[i].push_back(i+n);
        Y[i].push_back(i+n);
        Z[i].push_back(i+n);
    }
}

bool dfs(int u,vector<int>G[]) {
    c[u]=-1;
    for(int v=0; v<G[u].size(); v++) {
        int to=G[u][v];
        if(c[to]<0)return false;
        else if(!c[to]&&!dfs(to,G))return false;
    }
    c[u]=1;
    topo[--t]=u;
    return true;
}

bool toposort(vector<int>G[]) {
    t=2*n;
    memset(c,0,sizeof (c));
    for(int u=0; u<2*n; u++) {
        if(!c[u])if(!dfs(u,G))return false;
    }
    return true;
}

bool solve() {
    if(!toposort(X))return false;
    for(int i=0; i<2*n; i++)
        ans[0][topo[i]]=i;
    if(!toposort(Y))return false;
    for(int i=0; i<2*n; i++)
        ans[1][topo[i]]=i;
    if(!toposort(Z))return false;
    for(int i=0; i<2*n; i++)
        ans[2][topo[i]]=i;
    return true;
}

int main() {
   // freopen("test.in","r",stdin);
    int ca=1;
    while(~scanf("%d%d",&n,&m)&&(n+m)) {
        char c[2];
        int x,y;
        init();
        while(m--) {
            scanf("%s%d%d",c,&x,&y);
            x--,y--;
            if(c[0]=='I') {
                X[x].push_back(y+n);
                X[y].push_back(x+n);
                Y[x].push_back(y+n);
                Y[y].push_back(x+n);
                Z[x].push_back(y+n);
                Z[y].push_back(x+n);
            } else if(c[0]=='X') {
                X[x+n].push_back(y);
            } else if(c[0]=='Y') {
                Y[n+x].push_back(y);
            } else {
                Z[n+x].push_back(y);
            }
        }
        if(!solve()) {
            printf("Case %d: IMPOSSIBLE\n\n",ca++);
            continue;
        }
        printf("Case %d: POSSIBLE\n",ca++);
        for(int i=0; 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]);
        }
        putchar(10);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章