QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#805755#8551. DFS Order 5kjhhjki#WA 30ms3832kbC++203.7kb2024-12-08 18:24:192024-12-08 18:24:20

Judging History

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

  • [2024-12-08 18:24:20]
  • 评测
  • 测评结果:WA
  • 用时:30ms
  • 内存:3832kb
  • [2024-12-08 18:24:19]
  • 提交

answer

#include <bits/stdc++.h>

template<typename T, typename U>
void chkmin(T &a, U b) { if (a > b) a = b; }

using i64 = long long;

struct Fenwick {
    int n;
    std::vector<int> t;
    Fenwick(int _n = 0, int x = 0) { init(std::vector<int>(_n + 1, x)); }
    Fenwick(const std::vector<int> &a) { init(a); }
    static constexpr int lowbit(int x) { return x & -x; }
    void init(const std::vector<int> &a) {
        n = a.size() - 1;
        t.assign(n + 1, 0);
        for (int i = 1; i <= n; ++i) {
            t[i] += a[i];
            if (i + lowbit(i) <= n) {
                t[i + lowbit(i)] += t[i];
            }
        }
    }
    void add(int i, int k) { while (i <= n) t[i] += k, i += lowbit(i); }
    int query(int i) { int ans = 0; while (i) ans += t[i], i -= lowbit(i); return ans; }
    int query(int l, int r) { return query(r) - query(l - 1); }
};

void solve()
{
    int n, q;
    std::cin >> n >> q;
    std::vector<std::vector<int>> adj(n);
    for (int i = 1; i < n; ++i) {
        int u, v;
        std::cin >> u >> v;
        adj[--u].push_back(--v);
        adj[v].push_back(u);
    }
    std::vector<int> dfn(n, -1), seq = dfn, top(n), dep(n), pre = dfn, sz(n, 1);
    auto dfs1 = [&](auto &&self, int u) -> void {
        if (pre[u] != -1) {
            adj[u].erase(std::ranges::find(adj[u], pre[u]));
        }
        for (auto &v: adj[u]) {
            dep[v] = dep[u] + 1;
            pre[v] = u;
            self(self, v);
            sz[u] += sz[v];
            if (sz[v] > sz[adj[u][0]]) {
                std::swap(v, adj[u][0]);
            }
        }
    };
    dfs1(dfs1, 0);
    int t = 0;
    auto dfs2 = [&](auto &&self, int u) -> void {
        dfn[u] = t; seq[t++] = u;
        for (auto v: adj[u]) {
            top[v] = (v == adj[u][0]? top[u]: v);
            self(self, v);
        }
    };
    dfs2(dfs2, 0);
    auto lca = [&](int u, int v) {
        while (top[u] != top[v]) {
            if (dep[top[u]] < dep[top[v]]) {
                v = pre[top[v]];
            } else {
                u = pre[top[u]];
            }
        }
        return dep[u] < dep[v]? u: v;
    };
    auto cmp = [&](int u, int v) { return dfn[u] < dfn[v]; };
    auto find = [&](int u, int v) {
        auto it = std::ranges::lower_bound(adj[u], v, cmp);
        return it != adj[u].end() && *it == v;
    };
    std::vector<bool> vis(n);
    while (q --) {
        int k;
        std::cin >> k;
        std::vector<int> v(k), st;
        for (auto &x: v) {
            std::cin >> x;
            --x;
            vis[x] = false;
        }
        bool f = true;
        for (int i = 0; i < k; ++i) {
            if ((!st.empty()) && !find(v[st.back()], v[i])) {
                int p = lca(v[st.back()], v[i]);
                if (!find(p, v[i])) {
                    f = false;
                    break;
                }
                while (!st.empty()) {
                    int t = st.back();
                    if (p == v[t]) {
                        break;
                    }
                    st.pop_back();
                    if (i - t < sz[v[t]]) {
                        f = false;
                        break;
                    }
                }
            }
            if (!f) {
                break;
            }
            st.push_back(i);
            vis[v[i]] = true;
        }
        std::cout << (f? "Yes": "No") << '\n';
    }
}

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int T = 1;
    // std::cin >> T;
    while (T --) solve();
    return 0;
}

/*
6 1
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
*/

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3532kb

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: 30ms
memory: 3832kb

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

result:

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