QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#602795 | #8720. BFS 序 0 | UESTC_OldEastWest# | RE | 154ms | 52440kb | C++17 | 3.3kb | 2024-10-01 12:52:38 | 2024-10-01 12:52:38 |
Judging History
answer
#include <bits/stdc++.h>
const int N = 20;
void charming() {
int n; std::cin >> n;
std::vector<std::array<int, N> > fa(n);
std::vector<std::vector<int> > G(n);
for (int i = 1; i < n; ++i) {
std::cin >> fa[i][0], --fa[i][0];
G[fa[i][0]].emplace_back(i);
}
std::vector<int> dep(n);
std::function<void(int, int)> DFS = [&](int f, int u) {
fa[u][0] = f;
for (int i = 1; i < N; ++i) {
if (fa[u][i - 1] == -1) fa[u][i] = fa[u][i - 1];
else fa[u][i] = fa[fa[u][i - 1]][i - 1];
}
for (int v : G[u]) {
if (v == f) continue;
else dep[v] = dep[u] + 1, DFS(u, v);
}
};
DFS(-1, 0);
std::function<int(int, int)> jump = [&](int x, int d) {
assert(d <= dep[x]);
for (int i = N - 1; i >= 0; --i) {
if (d >> i & 1) x = fa[x][i];
}
return x;
};
std::function<int(int, int)> getLca = [&](int u, int v) {
if (dep[u] != dep[v]) {
if (dep[u] < dep[v]) std::swap(u, v);
u = jump(u, dep[u] - dep[v]);
}
if (u == v) return u;
for (int i = N - 1; i >= 0; --i) {
if (fa[u][i] != fa[v][i]) {
u = fa[u][i];
v = fa[v][i];
}
}
return fa[u][0];
};
int q; std::cin >> q;
std::vector<int> vis(n), du(n);
std::vector<std::vector<int> > nG(n);
for (int i = 0, m; i < q; ++i) {
std::cin >> m;
bool ok = true;
std::vector<int> vec(m);
for (int j = 0; j < m; ++j) {
std::cin >> vec[j], --vec[j];
if (j && dep[vec[j]] < dep[vec[j - 1]]) ok = false;
}
if (!ok) std::cout << "No\n";
else {
int now = 0;
std::vector<int> used;
while (now < m) {
int nxt = now;
while (nxt < m && dep[vec[nxt]] == dep[vec[now]]) ++nxt;
// std::cout << now << ' ' << nxt << std::endl;
for (int j = now + 1, u, v; j < nxt; ++j) {
u = vec[j - 1], v = vec[j];
int lca = getLca(u, v);
int fu = jump(u, dep[u] - dep[lca] - 1);
int fv = jump(v, dep[v] - dep[lca] - 1);
// std::cout << u << ' ' << v << ' ' << lca << std::endl;
// std::cout << u << ' ' << fu << std::endl;
// std::cout << v << ' ' << fv << std::endl;
if (!vis[fu]) vis[fu] = 1, used.emplace_back(fu);
if (!vis[fv]) vis[fv] = 1, used.emplace_back(fv);
nG[fu].emplace_back(fv);
}
now = nxt;
// std::cout << "Finished." << std::endl;
}
for (int u : used) {
for (int v : nG[u]) ++du[v];
}
std::vector<int> que;
int head = 0, tail = 0;
for (int u : used) if (!du[u]) que.emplace_back(u), ++tail;
while (head < tail) {
int u = que[head++];
vis[u] = 0;
for (int v : nG[u]) {
--du[v];
if (!du[v]) que.emplace_back(v), ++tail;
}
}
for (int u : used) {
nG[u].clear();
if (vis[u] || du[u]) ok = false, vis[u] = du[u] = 0;
}
used.clear();
if (!ok) std::cout << "No\n";
else std::cout << "Yes\n";
}
}
}
signed main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
charming();
return 0;
}
/*
12
1 1 2 3 4 5 6 6 6 7 8
1
5
2 3 6 8 11
*/
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3484kb
input:
6 1 1 3 2 4 10 4 3 6 2 5 1 4 3 2 4 5 5 2 5 4 6 3 3 1 4 2 3 5 6 3 5 4 5 2 6 1 4 4 3 2 5 4 4 6 2 3 3 3 2 6
output:
No Yes Yes No No No No No No Yes
result:
ok 10 token(s): yes count is 3, no count is 7
Test #2:
score: 0
Accepted
time: 154ms
memory: 51772kb
input:
300000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...
output:
No Yes Yes Yes No No No No No No Yes Yes No Yes No Yes Yes Yes No No No No Yes No No Yes No No No No Yes Yes Yes No No Yes No No No No No No No Yes No Yes No Yes No No No No Yes Yes Yes No No Yes No No No No No Yes Yes No No No No No Yes Yes No Yes No No No Yes No No Yes No No Yes Yes Yes No No No Y...
result:
ok 254 token(s): yes count is 89, no count is 165
Test #3:
score: 0
Accepted
time: 138ms
memory: 52212kb
input:
300000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...
output:
Yes No No Yes No No Yes Yes No Yes No Yes No No No Yes No No No No Yes No No No No No Yes Yes No No No Yes No No No No No Yes Yes Yes No No No No No No No Yes No Yes No No No Yes No No No Yes No No Yes No Yes Yes No No No Yes No Yes Yes Yes No Yes No Yes Yes Yes Yes Yes No Yes No No No No No Yes No ...
result:
ok 266 token(s): yes count is 102, no count is 164
Test #4:
score: 0
Accepted
time: 137ms
memory: 52440kb
input:
300000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...
output:
No No Yes No Yes No No No No No No No No Yes No No No No No No No No Yes No No No No Yes No No No No No No No Yes Yes No No No No No No Yes No No No Yes Yes No No No Yes Yes No Yes No Yes No Yes No Yes No Yes No No Yes No No No No No No No Yes Yes No Yes No No No No No No Yes Yes No Yes Yes No No No...
result:
ok 252 token(s): yes count is 87, no count is 165
Test #5:
score: -100
Runtime Error
input:
10 1 1 3 3 1 3 1 3 9 10000 4 3 5 6 9 5 3 1 4 9 6 5 6 4 3 5 7 28 8 3 4 3 1 6 3 5 8 7 2 4 4 4 2 7 5 9 2 4 10 8 4 10 3 4 5 7 3 5 2 9 6 4 7 2 3 9 5 5 9 2 5 6 1 12 3 5 8 8 1 3 5 8 4 5 9 1 29 3 9 9 1 5 8 5 5 6 10 8 8 4 8 4 2 8 10 5 7 3 7 1 8 4 7 5 1 6 7 5 9 8 3 2 1 6 4 4 1 9 5 22 10 9 5 4 5 8 10 3 2 1 10 ...