QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#107846 | #6507. Recover the String | batrr | WA | 684ms | 331668kb | C++14 | 7.3kb | 2023-05-22 22:25:49 | 2023-05-22 22:25:51 |
Judging History
answer
#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
const int N = 2000500, inf = 1e9, mod = 998244353;
const ll INF = 1e18;
int sum(int a, int b) {
a += b;
if (a >= mod)
a -= mod;
return a;
}
int sub(int a, int b) {
a -= b;
if (a < 0)
a += mod;
return a;
}
int mult(int a, int b) {
return 1ll * a * b % mod;
}
int bp(int a, int b) {
int res = 1;
while (b) {
if (b & 1)
res = mult(res, a);
a = mult(a, a);
b >>= 1;
}
return res;
}
int inv(int x) {
return bp(x, mod - 2);
}
vector<int> g[N], gr[N];
vector<int> gd[N];
int n, m;
bool was[N];
int depth[N];
char ans[N];
map<int, pii> mt[N];
vector<int> pre[N], nxt[N];
int ls[N], rs[N];
void dfs(int v) {
if (was[v])
return;
was[v] = 1;
for (auto to: gr[v]) {
dfs(to);
depth[v] = depth[to] + 1;
}
if (gr[v].empty())
depth[v] = 1;
}
void norm(vector<int> &v) {
while (v.size() > 5)
v.pop_back();
for (int i = 0; i + 1 < v.size(); i++)
if (v[i] == v.back()) {
v.pop_back();
return;
}
}
void set_link(int v, int u) {
if (v == 0 || u == 0)
return;
nxt[v].pb(u);
pre[u].pb(v);
norm(nxt[v]);
norm(pre[u]);
}
void f1(int v) {
if (pre[v].empty() && nxt[v].empty()) {
int u1 = gr[v][0];
int u2 = gr[v][gr[v].size() - 1];
ls[v] = u1;
rs[v] = u2;
set_link(u1, u2);
return;
}
for (auto pv: pre[v]) {
pii x = mt[v][pv];
int u = x.f;
if (x.s != 0) {
if (rs[v] == 0)
continue;
u = rs[v] ^ x.f ^ x.s;
}
assert(u != 0);
set_link(ls[pv], u);
set_link(u, rs[v]);
ls[v] = u;
rs[pv] = u;
}
for (auto nv: nxt[v]) {
pii x = mt[v][nv];
int u = x.f;
if (x.s != 0) {
if (ls[v] == 0)
continue;
u = ls[v] ^ x.f ^ x.s;
}
assert(u != 0);
set_link(ls[v], u);
set_link(u, rs[nv]);
rs[v] = u;
ls[nv] = u;
}
}
void f2(int v) {
if (depth[v] == 1)
return;
if (ls[v] != 0 && rs[v] != 0)
return;
if (ls[v] == 0 && rs[v] == 0)
return;
if (gr[v].size() == 1) {
int u = gr[v][0];
set_link(u, u);
ls[v] = rs[v] = u;
} else if (gr[v].size() == 2) {
int u1 = ls[v] | rs[v];
int u2 = gr[v][0] ^ gr[v][1] ^ u1;
if (ls[v] == 0)
ls[v] = u2;
else
rs[v] = u2;
} else
assert(0);
set_link(ls[v], rs[v]);
}
void f3(int v) {
if (ls[v] == 0 && rs[v] == 0) {
int u1 = gr[v][0];
int u2 = gr[v][gr[v].size() - 1];
ls[v] = u1;
rs[v] = u2;
}
set_link(ls[v], rs[v]);
}
int ftimer = 0;
int fwas[N], fdp[N];
int k;
void flex(int v, bool flag) {
if (depth[v] == 1) {
if(fwas[v] < ftimer){
fwas[v] = ftimer;
ans[v] = 'a' + k;
k++;
}
cout << ans[v];
return;
}
if (flag) {
flex(ls[v], 1);
flex(rs[v], 0);
ans[v] = ans[rs[v]];
} else {
if (fwas[v] == ftimer) {
cout << ans[v];
} else {
fwas[v] = ftimer;
flex(rs[v], 0);
ans[v] = ans[rs[v]];
}
}
}
void solve() {
ftimer++;
k = 0;
// string s = "";
// for(int i = 0; i < 10; i++)
// n = 0;
// map<string, int> a;
// set<pii> b;
// for (int i = 0; i < s.size(); i++) {
// string t = "";
// for (int j = i; j < s.size(); j++) {
// t += s[j];
// if (a.find(t) == a.end()) {
// n++;
// a[t] = n;
// }
// }
// }
// for (int i = 0; i < s.size(); i++) {
// string t = "";
// for (int j = i; j < s.size(); j++) {
// t += s[j];
// for (char q = 'a'; q <= 'z'; q++) {
// if (a.find(t + q) != a.end())
// b.insert({a[t], a[t + q]});
// if (a.find(q + t) != a.end())
// b.insert({a[t], a[q + t]});
// }
// }
// }
// n = a.size();
// m = b.size();
// for (int i = 0; i < m; i++) {
// int v, u;
// cin >> v >> u;
// v = b.begin()->f;
// u = b.begin()->s;
// b.erase(b.begin());
//// cerr << v << " " << u << endl;
// g[v].pb(u);
// gr[u].pb(v);
// }
cin >> n >> m;
for (int i = 0; i < m; i++) {
int v, u;
cin >> v >> u;
g[v].pb(u);
gr[u].pb(v);
}
for (int i = 1; i <= n; i++)
if (!was[i])
dfs(i);
for (int i = 1; i <= n; i++)
gd[depth[i]].pb(i);
for (int i = 1; i <= n; i++)
assert(g[i].size() <= 52);
for (int i = 1; i <= n; i++)
for (auto x: g[i])
for (auto y: g[i]) {
if (mt[x][y].f != 0)
mt[x][y].s = i;
else
mt[x][y].f = i;
}
int root = -1;
for (int d = n; d >= 1; d--)
if (!gd[d].empty()) {
root = gd[d][0];
break;
}
for (int d = n; d >= 2; d--) {
while (!gd[d].empty()) {
bool found = 0;
for (int i = 0; i < gd[d].size(); i++) {
int v = gd[d][i];
f1(v);
f2(v);
}
for (int i = 0; i < gd[d].size(); i++) {
int v = gd[d][i];
f1(v);
f2(v);
if (ls[v] > 0 && rs[v] > 0) {
found = 1;
swap(gd[d][i], gd[d][gd[d].size() - 1]);
gd[d].pop_back();
i--;
}
}
if (!found) {
f3(gd[d][0]);
}
}
}
// for (int i = 1; i <= n; i++) {
// cerr << endl;
// cerr << "V " << i << endl;
// cerr << "ls rs " << ls[i] << " " << rs[i] << endl;
// cerr << "PRE: ";
// for (auto x: pre[i])
// cerr << x << " ";
// cerr << endl;
// cerr << "NXT: ";
// for (auto x: nxt[i])
// cerr << x << " ";
// cerr << endl;
// }
for (int i = 0; i < gd[1].size(); i++)
ans[gd[1][i]] = 'a' + i;
flex(root, 1);
cout << "\n";
for (int i = 1; i <= n; i++) {
was[i] = 0;
ls[i] = rs[i] = 0;
g[i].clear();
gr[i].clear();
gd[i].clear();
pre[i].clear();
nxt[i].clear();
mt[i].clear();
}
}
int main() {
#ifdef DEBUG
freopen("input.txt", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
int t = 1;
cin >> t;
for (int i = 1; i <= t; i++) {
// cout << "Case #" << i << endl;
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 55ms
memory: 331584kb
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
Wrong Answer
time: 684ms
memory: 331668kb
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 abcb abba abbb abbc abca abac abcc abcd aaaaa abbbb aaaba aaabb abccc aabaa aabab abcbb abbaa aaabb abbcc aabca abacc aabcc aabcd abaaa abbab abbcb ababa aabab abcbc abaca abacb aabcb abcdc aabba abbab abbac abbba abbbb abbbc abbca abaac...
result:
wrong answer 16th lines differ - expected: 'abac', found: 'abcb'