QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#672129#8551. DFS Order 5Ri_Nai#WA 35ms3680kbC++233.2kb2024-10-24 15:44:382024-10-24 15:44:40

Judging History

你现在查看的是最新测评结果

  • [2024-10-24 15:44:40]
  • 评测
  • 测评结果:WA
  • 用时:35ms
  • 内存:3680kb
  • [2024-10-24 15:44:38]
  • 提交

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;
        auto dfs = [&](auto &&self, int u) -> void
        {
            ++pos;
            if (pos == m + 1)
                return;
            int v = series[pos];
            while (pos != m + 1 && fa[series[pos]] == u)
                self(self, series[pos]);
        };
        for (int u : need)
        {
            int last = pos;
            dfs(dfs, series[u]);
            // cout << last << "->" << pos << endl;
            if (pos == m + 1)
                return true;
            // cout << "this\n";
            if (pos - last != sz[series[u]])
                return false;
            // cout << "that\n";
        }
        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: 3680kb

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: 35ms
memory: 3648kb

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
No
No
No
No
No
No
No
No
...

result:

wrong answer 20th words differ - expected: 'No', found: 'Yes'