Codeforces Kitchen Plates

題意:有5個未知量,給五個不等式,要求你按照大小給出一個排序,如果自相矛盾的話就輸出impossible。

分析:拓撲排序即可,如果成環的話,輸出隊列裏的元素個數會小於五。
暴力便利所有排列然後根據給的條件判斷是否成立也可以。

拓撲排序代碼

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#define N 100000+200
#define ll long long
#define INF 0x7fffffff
using namespace std;

int edge[5][5];
int in[5];
queue<int>V;
queue<int>Q;
string s;

void tuopu(){
    for(int i=0;i<5;i++){
        if(in[i] == 0){
            Q.push(i);
        }
    }
    while (!Q.empty())
    {
        int x = Q.front();Q.pop();
        V.push(x);
        for(int i=0;i<5;i++){
            if(edge[x][i]){
                in[i]--;
                if(!in[i]){
                    Q.push(i);
                }
            }
        }
    }
    
}

int main(){
    int i,j,k;
    char a,b,c;
    for(i=1;i<=5;i++){
        scanf("%c%c%c",&a,&b,&c);
        getchar();
        if(b == '>'){swap(a,c);}
        in[c-'A']++;
        edge[a-'A'][c-'A'] = 1;
    }

    tuopu();
    if(V.size() < 5){
        printf("impossible\n");
        return 0;
    };
    while (!V.empty())
    {
        int x = V.front();V.pop();
        printf("%c",'A'+x);
    }
    

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