题意:有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;
}