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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值