Hdu 3231 Box Relations(拓扑排序)

来源:互联网 发布:c语言培训班 编辑:程序博客网 时间:2024/05/22 08:01
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3231

思路:拓扑排序。将每个长方体的每对面看成一条线段,将线段看成两个点,则共有3条线段,6个点。

对于一组相交关系,若两个长方体相交,当且仅当每一维中一个长方体的面插入另一个长方体的内部。

将长方体分为左右,上下,前后三维。

例如一长方形设左面为x,右面为x+n,另一长方体左面为y,右面为y+n,则在该维中坐标x<y+n,y<x+n,x<x+n,其他两维同理。

则由大小关系,分别对每一维拓扑排序即可。

#include<stack>#include<queue>#include<cstdio>#include<vector>#include<cstring>#include<iostream>#include<algorithm>#define debuusing namespace std;const int maxn=2000+50;char ch;int n,m,x,y;queue<int> q;int d[3][maxn];int tmp[3][maxn];int ans[3][maxn];vector<int> g[3][maxn];int topsort(int id){    int tot=0;    while(!q.empty()) q.pop();    for(int i=1; i<=2*n; i++)    {        if(!d[id][i])        {            //cout<<id<<" "<<i<<endl;            tot++;            q.push(i);            tmp[id][tot]=i;        }    }    while(!q.empty())    {        int now=q.front();        q.pop();        for(int i=0; i<g[id][now].size(); i++)        {            int nt=g[id][now][i];            d[id][nt]--;            if(!d[id][nt])            {                tot++;                q.push(nt);                tmp[id][tot]=nt;            }        }    }    return (tot==2*n);}void init(){    memset(d,0,sizeof(d));    memset(tmp,0,sizeof(tmp));    for(int i=0; i<=2*n; i++)        for(int j=0; j<3; j++)            g[j][i].clear();    for(int i=0; i<3; i++)        for(int j=1; j<=n; j++)        {            g[i][j].push_back(j+n);            d[i][j+n]++;        }}int solve(){    for(int i=0; i<3; i++)        if(!topsort(i)) return 0;    for(int i=0; i<3; i++)        for(int j=1; j<=2*n; j++)            ans[i][tmp[i][j]]=j-1;    return 1;}int main(){#ifdef debug    freopen("in.in","r",stdin);#endif // debug    int cas=0;    while(scanf("%d%d",&n,&m)==2&&(n||m))    {        init();        printf("Case %d: ",++cas);        for(int i=0; i<m; i++)        {            getchar();            scanf("%c%d%d",&ch,&x,&y);            if(ch=='I')            {                g[0][x].push_back(y+n),d[0][y+n]++;                g[0][y].push_back(x+n),d[0][x+n]++;                g[1][x].push_back(y+n),d[1][y+n]++;                g[1][y].push_back(x+n),d[1][x+n]++;                g[2][x].push_back(y+n),d[2][y+n]++;                g[2][y].push_back(x+n),d[2][x+n]++;            }            else            {                if(ch=='X')                    g[0][x+n].push_back(y),d[0][y]++;                else if(ch=='Y')                    g[1][x+n].push_back(y),d[1][y]++;                else if(ch=='Z')                    g[2][x+n].push_back(y),d[2][y]++;            }        }        /*for(int i=0; i<3; i++)            for(int j=1; j<=2*n; j++)                cout<<i<<" "<<j<<" "<<d[i][j]<<endl;*/        if(!solve()) printf("IMPOSSIBLE\n\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;}



0 0