QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#115928 | #6507. Recover the String | jzh | RE | 1ms | 3468kb | C++14 | 3.0kb | 2023-06-27 18:49:51 | 2023-06-27 18:49:51 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int n, m;
struct Str {
int head, tail;
int pre, suf;
};
int main() {
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
cin >> n >> m;
vector<vector<int>> graph(n + 1), hparg(n + 1);
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
u--, v--;
graph[u].push_back(v);
hparg[v].push_back(u);
}
vector<int> ds(n + 5, -1);
vector<vector<int>> uss(n + 5);
for (int u = 0; u < n; ++u)
if (hparg[u].empty()) {
uss[ds[u] = 1].push_back(u);
}
for (int d = 1; d < n; ++d) {
for (const int u: uss[d]) {
for (const int v: graph[u]) {
if (!~ds[v]) {
uss[ds[v] = d + 1].push_back(v);
}
}
}
}
const int L = *max_element(ds.begin(), ds.end());
int V = 0;
vector<Str> ss(2 * n + 1);
vector<vector<int>> xss(n + 5);
for (const int u: uss[1]) {
const int x = V++;
ss[x] = Str{x, x, -1, -1};
xss[u].push_back(x);
}
for (int d = 2; d <= L; ++d) {
for (const int u: uss[d]) {
vector<pair<int, int>> ps;
auto check = [&](int vL, int vR) -> void {
for (const int xL: xss[vL])
for (const int xR: xss[vR]) {
if (ss[xL].suf == ss[xR].pre) {
ps.emplace_back(xL, xR);
}
}
};
if (hparg[u].size() == 1) {
check(hparg[u][0], hparg[u][0]);
} else if (hparg[u].size() == 2) {
check(hparg[u][0], hparg[u][1]);
check(hparg[u][1], hparg[u][0]);
} else {
assert(false);
}
sort(ps.begin(), ps.end());
ps.erase(unique(ps.begin(), ps.end()), ps.end());
for (auto [sl, sr]: ps) {
const int x = V++;
ss[x] = Str{ss[sl].head, ss[sr].tail, sl, sr};
xss[u].push_back(x);
}
sort(xss[u].begin(), xss[u].end());
xss[u].erase(unique(xss[u].begin(), xss[u].end()), xss[u].end());
}
}
string ans = "~";
assert(uss[L].size() == 1);
for (const int x: xss[uss[L][0]]) {
string s(L, '?');
char c = 'a';
vector<char> tr(L, 0);
int y = x;
for (int i = 0; i < L; ++i) {
int a = ss[y].head;
if (!tr[a]) tr[a] = c++;
s[i] = tr[a];
y = ss[y].suf;
}
ans = min(ans, s);
}
cout << ans << "\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3468kb
input:
3 1 0 5 6 2 4 2 5 5 3 4 3 1 5 1 4 8 11 1 2 1 4 1 6 2 5 3 4 3 6 4 5 4 7 5 8 6 7 7 8
output:
a aba aaba
result:
ok 3 lines
Test #2:
score: -100
Dangerous Syscalls
input:
26837 1 0 2 1 2 1 3 2 1 3 2 3 3 2 3 1 1 2 5 5 4 3 5 1 1 2 3 2 5 3 5 6 2 4 2 5 5 3 4 3 1 5 1 4 5 5 1 5 2 1 2 4 4 5 3 4 6 6 1 4 5 3 2 4 4 6 3 6 2 3 4 3 1 3 3 4 2 1 7 8 2 5 1 3 7 1 3 5 7 6 1 2 4 6 6 3 8 11 2 6 2 7 3 7 8 1 6 4 4 5 7 4 7 1 3 8 2 8 1 5 8 10 8 1 4 5 7 8 3 5 3 1 7 3 1 2 5 2 6 4 6 3 9 11 5 2...
output:
a aa ab aaa aab aba aab abc aaaa aaab aaba aabb aabc aaba abab abac abba aaab abbc abca abac aabc abcd