QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#455435 | #8551. DFS Order 5 | ucup-team3678 | WA | 45ms | 7952kb | C++14 | 2.1kb | 2024-06-26 14:04:10 | 2024-06-26 14:04:11 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
vector<int> G[N];
int n, dfn[N], fa[N], siz[N];
void dfs(int x) {
dfn[x] = ++dfn[0], siz[x] = 1;
for (auto v : G[x]) if (v != fa[x]) {
fa[v] = x, dfs(v), siz[x] += siz[v];
}
}
int s[N], p[N];
void modify(int x, int y) {
for (; x <= n; x += x & -x) s[x] += y;
}
int query(int x) {
int res = 0;
for (; x; x -= x & -x) res += s[x];
return res;
}
signed main() {
int q; scanf("%d%d", &n, &q);
for (int i = 1, x, y; i < n; ++i) {
scanf("%d%d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
dfs(1);
for (int id = 1; id <= q; ++id) {
int k; scanf("%d", &k);
for (int i = 1; i <= k; ++i) {
scanf("%d", &p[i]);
if (id == 89) {
printf("%d\n", p[i]);
}
}
if (k == 1) {
puts("Yes");
continue;
}
modify(dfn[p[1]], 1);
int flg = 1;
for (int i = 2; i <= k; ++i) {
if (fa[p[i]] == p[i - 1]) {
if (query(dfn[p[i]] - 1) != query(dfn[p[i]] + siz[p[i]] - 1)) {
flg = 0;
}
} else {
if (dfn[fa[p[i]]] > dfn[p[i - 1]] || dfn[fa[p[i]]] + siz[fa[p[i]]] - 1 < dfn[p[i - 1]] ||
!(dfn[p[i]] > dfn[p[i - 1]] || dfn[p[i]] + siz[p[i]] - 1 < dfn[p[i - 1]])) {
flg = 0;
} else {
int now = p[i - 1];
while (query(dfn[now]) != query(dfn[now] - 1) && now != fa[p[i]]) {
if (query(dfn[now] + siz[now] - 1) - query(dfn[now] - 1) != siz[now]) {
flg = 0;
}
now = fa[now];
}
}
}
modify(dfn[p[i]], 1);
}
if (flg) {
puts("Yes");
} else {
puts("No");
}
for (int i = 1; i <= k; ++i) {
modify(dfn[p[i]], -1);
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7952kb
input:
6 7 1 2 1 3 2 4 3 5 2 6 2 4 1 2 4 2 2 4 3 2 4 4 2 4 5 2 4 6 6 1 2 6 4 3 5
output:
No No Yes No No Yes Yes
result:
ok 7 tokens
Test #2:
score: -100
Wrong Answer
time: 45ms
memory: 7684kb
input:
10 100000 7 2 1 7 7 10 8 6 8 7 1 3 4 5 9 5 5 8 8 8 9 7 2 8 1 6 1 4 8 3 5 2 6 7 10 3 9 9 1 1 1 8 10 3 2 9 3 8 7 3 7 5 6 2 8 5 9 1 6 3 4 6 2 1 3 5 8 9 2 4 9 1 3 2 1 5 5 8 5 1 7 9 10 5 2 9 2 6 4 10 6 3 8 3 4 5 8 2 8 4 9 4 10 1 2 4 3 3 6 3 1 3 6 1 1 6 8 3 1 3 7 3 2 3 9 1 5 4 3 7 8 10 9 4 2 3 10 2 5 4 3 ...
output:
No No No Yes No No No No Yes No No No No No No Yes No No No No No No No No No No No No No No No Yes No No No No No No Yes No No No No No Yes Yes No No No No No No No No No No No No No No No No No No No No No No No No Yes No No Yes Yes No No No No Yes No No No No No No No No 4 2 8 Yes No No No No No ...
result:
wrong answer 89th words differ - expected: 'No', found: '4'