QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#115895 | #6507. Recover the String | jzh | RE | 1ms | 3536kb | C++14 | 5.0kb | 2023-06-27 18:02:44 | 2023-06-27 18:02:47 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template<class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template<class T>
ostream &operator<<(ostream &os, const vector<T> &as) {
const int sz = as.size();
os << "[";
for (int i = 0; i < sz; ++i) {
if (i >= 256) {
os << ", ...";
break;
}
if (i > 0) { os << ", "; }
os << as[i];
}
return os << "]";
}
template<class T>
void pv(T a, T b) {
for (T i = a; i != b; ++i) cerr << *i << " ";
cerr << endl;
}
template<class T>
bool chmin(T &t, const T &f) {
if (t > f) {
t = f;
return true;
}
return false;
}
template<class T>
bool chmax(T &t, const T &f) {
if (t < f) {
t = f;
return true;
}
return false;
}
int N, M;
vector<int> A, B;
vector<vector<int>> graph, hparg;
struct Str {
int head, tail;
int pre, suf;
};
int main() {
for (int numCases; ~scanf("%d", &numCases);) {
for (int caseId = 1; caseId <= numCases; ++caseId) {
scanf("%d%d", &N, &M);
A.resize(M);
B.resize(M);
for (int i = 0; i < M; ++i) {
scanf("%d%d", &A[i], &B[i]);
--A[i];
--B[i];
}
graph.assign(N, {});
hparg.assign(N, {});
for (int i = 0; i < M; ++i) {
graph[A[i]].push_back(B[i]);
hparg[B[i]].push_back(A[i]);
}
vector<int> ds(N, -1);
vector<vector<int>> uss(N + 1);
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());
assert(uss[L].size() == 1);
uss.resize(L + 1);
int V = 0;
vector<Str> ss(2 * N + 1);
vector<vector<int>> xss(N);
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 (int i = 0; i < ps.size(); i++) {
auto [sl, sr] = ps[i];
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());
ps.clear();
}
}
string ans = "~";
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) {
const int a = ss[y].head;
if (!tr[a]) tr[a] = c++;
s[i] = tr[a];
y = ss[y].suf;
}
}
chmin(ans, s);
}
puts(ans.c_str());
}
#ifndef LOCAL
break;
#endif
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3536kb
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...