天天看點

HDU3231Box Relations題目代碼

題目

http://acm.hdu.edu.cn/showproblem.php?pid=3231

第一想法就是拓撲排序,想了一想,發現并不難

代碼

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<queue>

#ifdef L_JUDGE
#pragma warning(disable:4996)
#endif

using namespace std;

#define MAXN 1010
#define MAXE 2020

int inx[MAXE], iny[MAXE], inz[MAXE];
int rex[MAXE], rey[MAXE], rez[MAXE];
vector<int> mx[MAXE], my[MAXE], mz[MAXE];
int N, R;

void init(){
    memset(inx, , sizeof(inx));
    memset(iny, , sizeof(iny));
    memset(inz, , sizeof(inz));

    for(int i=*N;i>=;i--){
        mx[i].clear();
        my[i].clear();
        mz[i].clear();
    }
}

bool solve(int in[], vector<int> map[], int ans[]){
    queue<int> qe;
    int maxi=*N;
    for(int i=;i<=maxi;i++){
        if(in[i]==){
            qe.push(i);
        }
    }
    int num=;
    while(!qe.empty()){
        int tp=qe.front();
        qe.pop();
        ans[tp]=num++;
        int maxi=map[tp].size()-;
        for(int i=;i<=maxi;i++){
            int temp=map[tp][i];
            in[temp]--;
            if(==in[temp]){
                qe.push(temp);
            }
        }
    }
    if(num==*N)return true;
    else return false;

}

int main(){
    #ifdef L_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    int casei=;
    while(scanf("%d%d", &N, &R), N+R){
        init();
        for(int ni=;ni<=N;ni++){
            inx[ni+N]++;
            iny[ni+N]++;
            inz[ni+N]++;
            mx[ni].push_back(ni+N);
            my[ni].push_back(ni+N);
            mz[ni].push_back(ni+N);
        }
        char s[];
        int a, b;
        for(int ni=;ni<=R;ni++){
            scanf("%s%d%d", s, &a, &b);
            switch(s[]){
                case 'I':
                    inx[b+N]++;
                    inx[a+N]++;
                    mx[a].push_back(b+N);
                    mx[b].push_back(a+N);
                    iny[b+N]++;
                    iny[a+N]++;
                    my[a].push_back(b+N);
                    my[b].push_back(a+N);
                    inz[b+N]++;
                    inz[a+N]++;
                    mz[a].push_back(b+N);
                    mz[b].push_back(a+N);
                    break;
                case 'X':
                    inx[b]++;
                    mx[a+N].push_back(b);
                    break;
                case 'Y':
                    iny[b]++;
                    my[a+N].push_back(b);
                    break;
                case 'Z':
                    inz[b]++;
                    mz[a+N].push_back(b);
                    break;
                default:
                    break;
            }
        }
        bool flag=true;
        if(!solve(inx, mx, rex))flag=false;
        if(!solve(iny, my, rey))flag=false;
        if(!solve(inz, mz, rez))flag=false;
        if(flag){
            printf("Case %d: POSSIBLE\n", casei++);
            for(int i=;i<=N;i++){
                printf("%d %d %d %d %d %d\n", 
                        rex[i], rey[i], rez[i], 
                        rex[i+N], rey[i+N], rez[i+N] );
            }
        }else{
            printf("Case %d: IMPOSSIBLE\n", casei++);
        }
        printf("\n");
    }
    #ifdef L_JUDGE
        fclose(stdin);
        fclose(stdout);
        system("out.txt");
    #endif

    return ;
}