QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#294072 | #7121. Beech Tree | nhuang685 | 0 | 102ms | 86828kb | C++20 | 4.2kb | 2023-12-30 03:42:13 | 2024-04-28 08:27:45 |
Judging History
answer
/**
* @file qoj7121-1.cpp
* @author n685
* @brief
* @date 2023-12-29
*
*
*/
#include "beechtree.h"
#include <bits/stdc++.h>
#ifdef LOCAL
#include "dd/debug.h"
#else
#define dbg(...) 42
#define dbgR(...) 4242
#define dbgP(...) 420
#define dbgRP(...) 420420
void nline() {}
#endif
struct Tree {
static constexpr int LG = 20;
int n;
const std::vector<std::vector<int>> &adj;
std::vector<int> p, d, sub;
std::vector<std::vector<int>> a;
void dfs(int node, int par) {
p[node] = par;
for (int i : adj[node]) {
if (i == par) {
continue;
}
d[i] = d[node] + 1;
dfs(i, node);
sub[node] += sub[i];
}
}
Tree(const std::vector<std::vector<int>> &_adj)
: n((int)_adj.size()), adj(_adj), p(n, -1), d(n), sub(n, 1),
a(LG, std::vector<int>(n, -1)) {
dfs(0, -1);
a[0] = p;
for (int i = 1; i < LG; ++i) {
for (int j = 0; j < n; ++j) {
if (a[i - 1][j] != -1) {
a[i][j] = a[i - 1][a[i - 1][j]];
}
}
}
}
int up(int u, int dist) {
if (dist < 0) {
return -1;
}
if (d[u] < dist) {
return -1;
}
for (int i = 0; i < LG; ++i) {
if ((1 << i) & dist) {
u = a[i][u];
}
}
return u;
}
// is v an ancestor of u?
bool ancestor(int u, int v) { return (up(u, d[u] - d[v]) == v); }
int lca(int u, int v) {
if (d[u] < d[v]) {
std::swap(u, v);
}
for (int i = LG - 1; i >= 0; --i) {
if (d[u] >= (1 << i) && !ancestor(v, a[i][u])) {
u = a[i][u];
}
}
return p[u];
}
};
std::vector<int> beechtree(int N, int M, std::vector<int> P,
std::vector<int> C) {
int n = N;
std::vector<int> c = C;
std::vector<std::vector<int>> adj(n);
for (int i = 1; i < n; ++i) {
adj[P[i]].push_back(i);
}
Tree tr(adj);
std::vector<int> good(n, true);
for (int i = 0; i < n; ++i) {
std::sort(adj[i].begin(), adj[i].end(),
[&](int a, int b) { return c[a] < c[b]; });
for (int j = 0; j < (int)adj[i].size() - 1; ++j) {
if (c[adj[i][j]] == c[adj[i][j + 1]]) {
good[i] = false;
break;
}
}
}
auto gg = [&](int a, int b, int par) {
if (!good[a] || !good[b] || !good[par]) {
return false;
}
if (tr.sub[a] > tr.sub[b]) {
std::swap(a, b);
}
int r = 0;
for (int l = 0; l < (int)adj[a].size(); ++l) {
// while (r < (int)adj[b].size() && c[adj[a][l]] > c[adj[b][r]]) {
// r++;
// }
r = (int)std::distance(
adj[b].begin(),
std::lower_bound(adj[b].begin(), adj[b].end(), c[adj[b][r]],
[&](auto a, auto b) { return c[a] < b; }));
if (r >= (int)adj[b].size() || c[adj[a][l]] < c[adj[b][r]] ||
tr.sub[adj[a][l]] > tr.sub[adj[b][r]]) {
return false;
} else if (tr.sub[a] == tr.sub[b] &&
tr.sub[adj[a][l]] < tr.sub[adj[b][r]]) {
return false;
}
}
return true;
};
auto dfs = [&](auto &self, int node) -> std::set<std::pair<int, int>> * {
if ((int)adj[node].size() == 0) {
return new std::set{std::pair{tr.sub[node], node}};
}
std::set<std::pair<int, int>> *mx = nullptr;
std::vector<std::set<std::pair<int, int>> *> v;
for (int i : adj[node]) {
auto s = self(self, i);
if (!good[i]) {
good[node] = false;
}
if (!mx) {
mx = s;
} else if (mx->size() < s->size()) {
v.push_back(mx);
mx = s;
} else {
v.push_back(s);
}
}
for (auto s : v) {
for (auto [$, i] : *s) {
auto it = mx->lower_bound({tr.sub[i], i});
if (it != mx->end()) {
auto [$$, j] = *it;
if (!gg(i, j, node)) {
good[node] = false;
}
}
if (it != mx->begin()) {
auto [$$, j] = *std::prev(it);
if (!gg(i, j, node)) {
good[node] = false;
}
}
}
mx->merge(*s);
}
mx->insert(std::pair{tr.sub[node], node});
return mx;
};
delete dfs(dfs, 0);
return good;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 9
Accepted
time: 0ms
memory: 3864kb
input:
j2DRV0nYbs0y1xUYUGaiqtOKUU9vM9zi 8 500 -1 0 1 2 3 4 5 6 0 281 281 281 281 281 281 281
output:
p89vHUOQJ7iyHtdrgGXzKx8iRtXLL6wH OK 1 1 1 1 1 1 1 1
result:
ok
Test #2:
score: -9
Wrong Answer
time: 0ms
memory: 3804kb
input:
j2DRV0nYbs0y1xUYUGaiqtOKUU9vM9zi 8 500 -1 0 1 2 3 4 5 6 0 11 169 169 169 169 169 169
output:
p89vHUOQJ7iyHtdrgGXzKx8iRtXLL6wH OK 1 1 1 1 1 1 1 1
result:
wrong answer 2nd lines differ - on the 1st token, expected: '0', found: '1'
Subtask #2:
score: 0
Wrong Answer
Test #11:
score: 0
Wrong Answer
time: 87ms
memory: 86828kb
input:
j2DRV0nYbs0y1xUYUGaiqtOKUU9vM9zi 200000 200000 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86...
output:
p89vHUOQJ7iyHtdrgGXzKx8iRtXLL6wH OK 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
result:
wrong answer 2nd lines differ - on the 1st token, expected: '0', found: '1'
Subtask #3:
score: 0
Wrong Answer
Test #25:
score: 0
Wrong Answer
time: 71ms
memory: 29384kb
input:
j2DRV0nYbs0y1xUYUGaiqtOKUU9vM9zi 103965 200000 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
output:
p89vHUOQJ7iyHtdrgGXzKx8iRtXLL6wH OK 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
result:
wrong answer 2nd lines differ - on the 1st token, expected: '0', found: '1'
Subtask #4:
score: 0
Wrong Answer
Test #48:
score: 0
Wrong Answer
time: 102ms
memory: 54536kb
input:
j2DRV0nYbs0y1xUYUGaiqtOKUU9vM9zi 199979 200000 -1 0 1 1 1 1 1 1 1 1 1 1 11 11 1 11 11 11 11 11 11 11 0 22 23 23 23 23 23 23 23 23 23 23 33 22 35 36 36 36 36 36 36 36 36 38 38 38 35 48 49 49 49 49 49 49 49 53 53 48 59 60 60 60 60 60 59 66 67 67 67 67 67 67 67 67 67 67 73 71 66 80 81 81 81 81 81 81 81...
output:
p89vHUOQJ7iyHtdrgGXzKx8iRtXLL6wH OK 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 ...
result:
wrong answer 2nd lines differ - on the 37th token, expected: '0', found: '1'
Subtask #5:
score: 0
Skipped
Dependency #1:
0%
Subtask #6:
score: 0
Wrong Answer
Test #65:
score: 14
Accepted
time: 1ms
memory: 4900kb
input:
j2DRV0nYbs0y1xUYUGaiqtOKUU9vM9zi 2000 2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 ...
output:
p89vHUOQJ7iyHtdrgGXzKx8iRtXLL6wH OK 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
result:
ok
Test #66:
score: -14
Wrong Answer
time: 1ms
memory: 4604kb
input:
j2DRV0nYbs0y1xUYUGaiqtOKUU9vM9zi 2000 2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 ...
output:
p89vHUOQJ7iyHtdrgGXzKx8iRtXLL6wH OK 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
result:
wrong answer 2nd lines differ - on the 1st token, expected: '0', found: '1'
Subtask #7:
score: 0
Skipped
Dependency #5:
0%
Subtask #8:
score: 0
Skipped
Dependency #6:
0%
Subtask #9:
score: 0
Skipped
Dependency #1:
0%