2025 United Kingdom and Ireland Programming Contest (UKIEPC 2025) 2025 英国 ICPC——J. Joust Sort(拓扑排序解决依赖排序问题) · znzryb
## 题目大意
### 题目总结:J. Joust排序 (Joust Sort)
**题目核心:**
给定一个由小写字母组成的字符串 ,以及若干条字母之间的“大小关系”约束。要求根据这些约束对字符串进行重新排列。
**规则说明:**
1. **强约束:** 若给定 ,则在最终字符串中,**所有**字母 必须出现在**所有**字母 之前。
1. **无约束:** 若字母 和 之间没有直接或间接的约束关系(既不是 也不是 ),则它们在字符串中的位置可以**任意交错**。
1. **合法性:** 如果约束关系中存在冲突(如环状依赖 且 ),则判定为无法排序。
**输出要求:**
- 输出满足所有约束的任意一种排列方案。
- 若不存在合法方案,输出 `IMPOSSIBLE`。
---
**样例解释:**
- **样例 1:**
- 输入:`m > i`, `n < i`, `i > o`,字符串 `minion`。
- 推导:由 且 (即 )可知, 和 都在 之前。由 (即 )可知, 在 之前。
- 关系链:。
- 结果:`noniim` 符合 。
- **样例 2:**
- 输入:`b < n`,字符串 `banana`。
- 推导:仅要求所有的 `b` 在所有的 `n` 之前。对字母 `a` 没有约束。
- 结果:`banana`。此时所有的 `b`(位置0)确实在所有的 `n`(位置2, 4)之前,`a` 可以穿插其中。
- **样例 3:**
- 输入:包含多条重复及推导关系,如 且 。
- 推导:关系链为 。
- 结果:`nnnbaaama`。所有的 `n` 在 `b` 前,所有的 `b` 在 `a` 前。
- **样例 4:**
- 输入:`a < b`, `b < a`。
- 推导:逻辑冲突,存在环。
- 结果:`IMPOSSIBLE`。
## 思路讲解
拓扑排序。
## AC代码
AC
[https://codeforces.com/gym/106157/submission/361649348](https://codeforces.com/gym/106157/submission/361649348)
::: 源代码
```cpp
/**
* Problem: J. Joust Sort
* Contest: 2025 United Kingdom and Ireland Programming Contest (UKIEPC 2025)
* Judge: Codeforces
* URL: https://codeforces.com/gym/106157/problem/J
* Created: 2026-02-06 13:10:57
* Author: Gospel_rock
*
* Powered by AutoCp https://github.com/Pushpavel/AutoCp
*/
#include <bits/stdc++.h>
#define all(vec) vec.begin(),vec.end()
#define lson(o) (o<<1)
#define rson(o) (o<<1|1)
#define SZ(a) ((long long) a.size())
#define debug(var) cerr << #var <<":"<<var<<"\n";
#define cend cerr<<"\n-----------\n"
#define fsp(x) fixed<<setprecision(x)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using DB = double;
// using i128 = __int128;
using CD = complex<double>;
static constexpr ll MAXN = (ll)1e6+10, INF = (1ll<<61)-1;
static constexpr ll mod = 998244353; // (ll)1e9+7;
static constexpr double eps = 1e-8;
const double pi = acos(-1.0);
ll lT,testcase;
/*
*
*/
void Solve() {
ll N;
cin >> N;
vector<vector<ll>> g(30);
vector<ll> in_degree(30);
for (int i=1;i<=N;++i) {
char a,op,b;
cin>>a>>op>>b;
if (op=='<') {
swap(a,b);
}
g[b-'a'].push_back(a-'a');
in_degree[a-'a']++;
}
map<char,ll> ch_cnt;
string s;
cin>>s;
for (auto &ch:s) {
ch_cnt[ch]++;
}
vector<char> vis(30);
string ans;
auto topo_sort=[&](ll node) -> vector<ll> {
queue<ll> q;
vector<ll> res;
vis[node]=true;
q.push(node);
res.push_back(node);
while (!q.empty()) {
ll u=q.front();
q.pop();
for (auto v:g[u]) {
if (vis[v]) {
continue;
}
in_degree[v]--;
if (in_degree[v]==0) {
vis[v]=true;
res.push_back(v);
q.push(v);
}
}
}
return res;
};
for (int i=0;i<26;++i) {
if (vis[i]) {
continue;
}
if (in_degree[i]==0) {
vector<ll> res=topo_sort(i);
for (auto id:res) {
char a='a'+id;
ans+=string(ch_cnt[a],a);
}
}
}
if (SZ(ans)!=SZ(s)) {
cout<<"IMPOSSIBLE\n";
return;
}
cout<<ans<<"\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#ifdef LOCAL
cout.setf(ios::unitbuf); // 无缓冲流,方便我们调试
#endif
// cin >> lT;
// for (testcase=1;testcase<=lT;++testcase)
Solve();
return 0;
}
/*
AC
https://codeforces.com/gym/106157/submission/361649348
*/
```
:::
## 心路历程(WA,TLE,MLE……)