题目
大概就是让盘子从小到大排序
题目链接:http://codeforces.com/gym/102219/problem/J
思路
拓扑排序的思想就是把入度为0的找出来,然后让和他连接的入度都-1,重复。
代码
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<ctime>
#include<iostream>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<iomanip>
#include<list>
#include<bitset>
#include<sstream>
#include<fstream>
#include<complex>
#include<algorithm>
#if __cplusplus >= 201103L
#include <unordered_map>
#include <unordered_set>
#endif
#define ll long long
using namespace std;
const int INF = 0x3f3f3f3f;
char a[5]={'A','B','C','D','E'};
int b[10];
bool vis[10],dir[10][10];
vector<int> v;
priority_queue<int ,vector<int>,greater<int>> q;
int f(){
for(int i=0;i<5;i++){
if(!b[i]) q.push(i);
}
while(!q.empty()){
int x=q.top();
q.pop();
v.push_back(x);
for(int i=0;i<5;i++){
if(dir[x][i]){
b[i]--;
if(b[i]==0) q.push(i);
}
}
}
}
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t=5;
map<char,int> mp;
mp['A']=0;
mp['B']=1;
mp['C']=2;
mp['D']=3;
mp['E']=4;
while(t--){
string s;
cin>>s;
if(s[1]=='<') swap(s[0],s[2]);
b[mp[s[2]]]++;
dir[mp[s[0]]][mp[s[2]]]=vis[mp[s[0]]]=vis[mp[s[2]]]=1;
}
f();
if(v.size()<5) cout<<"impossible"<<endl;
else{
for(int i=4;i>=0;i--) cout<<a[v[i]];
cout<<endl;
}
return 0;
}