QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#408152 | #8280. Game of Strings | light_ink_dots# | RE | 0ms | 0kb | C++14 | 2.7kb | 2024-05-09 19:04:24 | 2024-05-09 19:04:41 |
answer
#include <bits/stdc++.h>
using namespace std;
int main() {
static const int maxn = 50010, maxlg = 20;
int t;
scanf("%d", &t);
while (t--) {
int n, s, t;
scanf("%d %d %d", &n, &s, &t);
static vector<int> G[maxn];
for (int i = 1; i <= n; i++) G[i].clear();
for (int i = 1, u, v; i < n; i++) {
scanf("%d %d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
if (n == 2) {
printf("%d %d\n", s, t);
continue;
}
if (n == 3) {
printf("%d %d %d\n", s, s ^ t, t);
continue;
}
static int lg[maxn], dep[maxn];
static int dfn[maxn], st[maxlg][maxn], cnt;
static auto get = [](const int x, const int y) { return dep[x] < dep[y] ? x : y; };
function<void(int, int)> dfs = [&dfs](const int u, const int fa) {
st[0][dfn[u] = ++cnt] = fa;
for (int i : G[u])
if (i != fa)
dep[i] = dep[u] + 1, dfs(i, u);
return;
};
cnt = 0, dfs(1, 0);
for (int i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1;
for (int l = 1; l <= lg[n]; l++)
for (int i = 1; i + (1 << l) - 1 <= n; i++)
st[l][i] = get(st[l - 1][i], st[l - 1][i + (1 << (l - 1))]);
auto dist = [](int u, int v) {
int ret = dep[u] + dep[v];
u = dfn[u], v = dfn[v];
if (u > v)
swap(u, v);
int Lg = lg[v - (++u) + 1];
return ret - (dep[get(st[Lg][u], st[Lg][v - (1 << Lg) + 1])] << 1);
};
static int p[maxn], val;
static int ans[maxn], Val;
p[1] = s, p[n] = t, val = 0;
for (int i = 2, x = 1; i < n; i++, x++) {
while (x == s || x == t) x++;
p[i] = x;
}
static random_device rd;
static mt19937 rng(rd());
for (int t = 0; true; t++) {
shuffle(p + 2, p + n, rng), val = 0;
for (int i = 1; i < n; i++) val ^= dist(p[i], p[i + 1]);
if (val < 16)
break;
}
memcpy(ans, p, sizeof(int) * (n + 5)), Val = val;
for (int t = 0; t < 1000; t++) {
int id = rng() % (n - 3) + 2;
for (int i = id; i <= id + 2; i++) val ^= dist(p[i - 1], p[i]);
swap(p[id], p[id + 1]);
for (int i = id; i <= id + 2; i++) val ^= dist(p[i - 1], p[i]);
if (Val > val)
memcpy(ans, p, sizeof(int) * (n + 5)), Val = val;
}
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
printf("\n");
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
abbabbbbabbaaaaabbbaabaaaaaabbbaabbabababaabababaababaaaaaabaaabaaabbbbababaaabbbabababbbabbbaabaaababbbaabaabbaaabbababaaaaabbbaabaaabbaabaabaaabaaabaababaabbbbbbbbabbbbbabbbaabababbaababbbbbabbbbaaabbaabaabaaabbabbaaabbaaababaabbbaabbbaaaabbababbabaababababaaaabbaaaabaabaaaababaaabababbbaaabababbb...