QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#603455 | #8720. BFS 序 0 | lllei | RE | 204ms | 77488kb | C++20 | 4.3kb | 2024-10-01 16:40:31 | 2024-10-01 16:40:33 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
struct LCA {
const int N;
int n, idx;
vector<int> id, rid, dep, fa;
vector<vector<int>> e, st;
LCA(int n) : N(__lg(n) + 1), n(n), idx(0), id(n, -1), rid(n), dep(n), fa(n), e(n), st(N, vector<int>(n)) {}
void add(int u, int v) {
e[u].push_back(v);
e[v].push_back(u);
}
int getMin(int x, int y) {
return dep[x] < dep[y] ? x : y;
}
void dfs(int u) {
id[u] = idx;
rid[idx] = u;
idx++;
for (int v : e[u]) {
if (id[v] != -1) {
continue;
}
dep[v] = dep[u] + 1;
fa[v] = u;
dfs(v);
}
}
void work(int rt = 0) {
dep[rt] = 1;
dfs(rt);
for (int i = 0; i < n; ++i) {
st[0][i] = rid[i];
}
for (int i = 1; i < N; ++i) {
for (int j = 0; j + (1 << i) - 1 < n; ++j) {
st[i][j] = getMin(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
}
}
}
int getLca(int x, int y) {
if (x == y) {
return x;
}
x = id[x], y = id[y];
if (x > y) {
swap(x, y);
}
++x;
int z = __lg(y - x + 1);
return fa[getMin(st[z][x], st[z][y - (1 << z) + 1])];
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
LCA lca(n);
vector<vector<int>> e(n);
for (int i = 1; i < n; ++i) {
int f;
cin >> f;
--f;
lca.add(f, i);
e[f].push_back(i);
}
lca.work();
vector<int> dep(n, -1);
vector<int> dfn(n);
int tot = 0;
auto dfs = [&](auto &&self, int u) -> void {
dfn[u] = tot++;
for (int v : e[u]) {
dep[v] = dep[u] + 1;
self(self, v);
}
};
dep[0] = 0;
dfs(dfs, 0);
int q;
cin >> q;
vector<vector<int>> ee(n);
vector<int> d(n);
auto solve = [&]() {
int m;
cin >> m;
vector<int> a(m);
vector<int> c(m);
for (int i = 0; i < m; ++i) {
cin >> a[i];
--a[i];
c[i] = a[i];
if (i) {
ee[a[i - 1]].push_back(a[i]);
d[a[i]]++;
}
}
for (int i = 1; i < m; ++i) {
if (dep[a[i]] < dep[a[i - 1]]) {
for (auto x : c) {
ee[x].clear();
d[x] = 0;
}
cout << "No\n";
return;
}
}
for (int i = 0; i < m;) {
int j = i;
while (j < m && dep[a[j]] == dep[a[i]]) {
++j;
}
for (int k = i; k < j - 1; ++k) {
int l = lca.getLca(a[k], a[k + 1]);
int t1 = *prev(upper_bound(e[l].begin(), e[l].end(), a[k], [&](int x, int y) {
return dfn[x] < dfn[y];
}));
int t2 = *prev(upper_bound(e[l].begin(), e[l].end(), a[k + 1], [&](int x, int y) {
return dfn[x] < dfn[y];
}));
assert(t1 != t2);
ee[t1].push_back(t2);
d[t2]++;
c.push_back(t1);
c.push_back(t2);
}
i = j;
}
sort(c.begin(), c.end());
c.erase(unique(c.begin(), c.end()), c.end());
queue<int> q;
for (auto x : c) {
if (d[x] == 0) {
q.push(x);
}
}
int cnt = 0;
while (q.size()) {
int u = q.front();
q.pop();
++cnt;
for (int v : ee[u]) {
--d[v];
if (d[v] == 0) {
q.push(v);
}
}
}
for (auto x : c) {
ee[x].clear();
d[x] = 0;
}
if (cnt != c.size()) {
cout << "No\n";
} else {
cout << "Yes\n";
}
};
while (q--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3572kb
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: 204ms
memory: 76000kb
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: 201ms
memory: 77488kb
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: 203ms
memory: 77276kb
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 ...