QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#672206 | #8551. DFS Order 5 | Ri_Nai# | WA | 39ms | 3640kb | C++23 | 3.4kb | 2024-10-24 16:01:20 | 2024-10-24 16:01:21 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, q;
cin >> n >> q;
vector<vector<int>> edge(n + 1);
vector<int> son(n + 1), top(n + 1), id(n + 1),
dep(n + 1), fa(n + 1), sz(n + 1);
for (int i = 1; i < n; ++i)
{
int x, y;
scanf("%d%d", &x, &y);
edge[x].push_back(y);
edge[y].push_back(x);
}
int num = 0;
auto dfs = [&](auto &&self, int u, int f) -> void
{
fa[u] = f;
sz[u] = 1;
dep[u] = dep[f] + 1;
for (int v : edge[u])
{
if (v == f)
continue;
self(self, v, u);
sz[u] += sz[v];
if (sz[son[u]] < sz[v])
son[u] = v;
}
};
auto dfs_ = [&](auto &&self, int u, int tp) -> void
{
top[u] = tp;
id[u] = ++num;
if (son[u])
self(self, son[u], tp);
for (int v : edge[u])
if (v != fa[u] && v != son[u])
self(self, v, v);
};
dfs(dfs, 1, 0);
dfs_(dfs_, 1, 1);
auto LCA = [&](int u, int v)
{
while (top[u] ^ top[v])
{
if (dep[top[u]] > dep[top[v]])
u = fa[top[u]];
else
v = fa[top[v]];
}
return dep[u] < dep[v] ? u : v;
};
auto solve = [&]()
{
auto isAscent = [&](int u, int v)
{
return LCA(u, v) == v;
};
auto isValid = [&](int u, int v)
{
return isAscent(u, fa[v]) && !isAscent(u, v) && fa[v] != u;
};
auto inValid = [&](int u, int v)
{
return isAscent(u, v);
};
int m;
scanf("%d", &m);
vector<int> series(m + 1);
for (int i = 1; i <= m; ++i)
scanf("%d", &series[i]);
vector<int> need;
need.push_back(1);
for (int i = 2; i <= m; ++i)
if(isValid(series[1], series[i]))
need.push_back(i);
else if (inValid(series[1], series[i]))
return false;
for (int i = 1; i < need.size(); ++i)
if (need[i] - need[i - 1] != sz[series[need[i - 1]]])
return false;
for (int i = 1; i < need.size(); ++i)
if (dep[series[need[i]]] > dep[series[need[i - 1]]])
return false;
int pos = 1, flag = 1;
auto dfs = [&](auto &&self, int u) -> void
{
++pos;
if (!flag)
return;
if (pos == m + 1)
return;
int v = series[pos];
int cnt = 0;
while (flag && pos != m + 1 && fa[series[pos]] == u)
++cnt, self(self, series[pos]);
// cout << u << ' ' << cnt << endl;
if (pos != m + 1 && cnt != edge[u].size() - (u != 1))
flag = 0;
};
for (int u : need)
{
int last = pos;
dfs(dfs, series[u]);
if (pos == m + 1)
return true;
if (!flag)
return false;
if (pos - last != sz[series[u]])
return false;
}
return pos == m + 1;
};
while (q--)
if (solve())
puts("Yes");
else puts("No");
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3596kb
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: 39ms
memory: 3640kb
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 No No No No No No No No N...
result:
wrong answer 114th words differ - expected: 'No', found: 'Yes'