QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#640637 | #8551. DFS Order 5 | zhenghanyun | WA | 21ms | 5680kb | C++14 | 2.6kb | 2024-10-14 14:55:38 | 2024-10-14 14:55:39 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
const int B = 18;
struct edge {
int to, Next;
edge() {}
edge(int to, int Next): to(to), Next(Next) {}
} G[N << 1];
int head[N], cnt;
inline void add_edge(int u, int v) {
G[++cnt] = edge(v, head[u]);
head[u] = cnt;
}
int n, q, k, fa[N], dep[N], siz[N], f[N][B];
vector <int> vec, tmp;
bitset <N> vis;
inline void dfs(int u) {
dep[u] = dep[fa[u]] + 1;
siz[u] = 1;
for (int i = head[u]; i; i = G[i].Next) {
int v = G[i].to;
if (v != fa[u]) {
fa[v] = u;
dfs(v);
siz[u] += siz[v];
}
}
}
inline void init() {
dfs(1);
for (int i = 1; i <= n; ++i) {
f[i][0] = fa[i];
}
for (int j = 1; j < B; ++j) {
for (int i = 1; i <= n; ++i) {
f[i][j] = f[f[i][j - 1]][j - 1];
}
}
}
inline int kfa(int u, int k) {
for (int i = 0; i < B; ++i) {
if (k & (1 << i)) {
u = f[u][i];
}
}
return u;
}
inline bool p(int u, int v) {
if (!u) {
return false;
}
if (dep[u] > dep[v]) {
return false;
}
return kfa(v, dep[v] - dep[u]) == u;
}
inline bool check(int l, int r) {
int u = vec[l];
for (int i = l; i <= r; ++i) {
if (!p(u, vec[i])) {
return false;
}
}
return true;
}
inline bool solve() {
for (int i = 0; i < k - 1; ++i) {
if (fa[vec[i + 1]] != vec[i] && !p(fa[vec[i + 1]], vec[i])) {
return false;
}
if (p(vec[i + 1], vec[i])) {
return false;
}
}
int pos = 0;
while (pos < k) {
int r = min(k - 1, pos + siz[vec[pos]] - 1);
if (!check(pos, r)) {
return false;
}
for (int i = pos; i <= r; ++i) {
if (vis[vec[i]]) {
return false;
}
}
if (r != k - 1) {
int u = kfa(vec[pos], dep[fa[vec[r + 1]]] - dep[vec[pos]] - 1);
vis[u] = true;
tmp.emplace_back(u);
}
pos = r + 1;
}
return true;
}
int main() {
#ifdef LOCAL
assert(freopen("test.in", "r", stdin));
assert(freopen("test.out", "w", stdout));
#endif
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> n >> q;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
add_edge(u, v);
add_edge(v, u);
}
init();
while (q--) {
cin >> k;
vec.resize(k);
bool flg = false;
for (int i = 0; i < k; ++i) {
cin >> vec[i];
flg |= vis[vec[i]];
vis[vec[i]] = true;
}
for (auto u: vec) {
vis[u] = false;
}
if (flg) {
cout << "No\n";
continue;
}
if (solve()) {
cout << "Yes\n";
} else {
cout << "No\n";
}
for (auto u: tmp) {
vis[u] = false;
}
tmp.clear();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5672kb
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: 21ms
memory: 5680kb
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 Yes 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 Yes No No No No No No No...
result:
wrong answer 20th words differ - expected: 'No', found: 'Yes'