QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#708228#8551. DFS Order 5ucup-team1264#WA 78ms12004kbC++202.2kb2024-11-03 20:36:072024-11-03 20:36:14

Judging History

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

  • [2024-11-03 20:36:14]
  • 评测
  • 测评结果:WA
  • 用时:78ms
  • 内存:12004kb
  • [2024-11-03 20:36:07]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

constexpr int w = 1 << 19, N = 101145;
int tr[w + N];
vector<int> adj[N];
int st[N], ed[N], dep[N];
int cnt = 0;
int fa[19][N];
int n, q;

void add(int p, int v) {
    for (p += w; p; p >>= 1) {
        tr[p] += v;
    }
}

// 左闭右开
int sum(int l, int r) {
    int ans = 0;
    l += w, r += w;
    while (l < r) {
        if (l & 1) ans += tr[l++];
        if (r & 1) ans += tr[--r];
        l >>= 1, r >>= 1;
    }
    return ans;
}

void dfs(int u, int f) {
    fa[0][u] = f;
    st[u] = cnt++;
    for (int v: adj[u]) {
        if (v == f) continue;
        dep[v] = dep[u] + 1;
        dfs(v, u);
    }
    ed[u] = cnt;
}

int lca(int u, int v) {
    if (dep[u] < dep[v]) swap(u, v);
    int diff = dep[u] - dep[v];
    for (int b = 18; b >= 0; b--) {
        if (diff >> b & 1) u = fa[b][u];
    }
    for (int b = 18; b >= 0; b--) {
        if (fa[b][u] != fa[b][v]) {
            u = fa[b][u]; v = fa[b][v];
        }
    }
    if (u == v) return u;
    return fa[0][u];
}

void solve() {
    int m; cin >> m;
    vector<int> a(m);
    for (int &x: a) cin >> x, x--;
    add(st[a[0]], 1);
    bool ans = true;
    for (int i = 1; i < m; i++) {
        if (sum(st[a[i]], ed[a[i]])) ans = false;
        if (lca(a[i - 1], a[i]) != fa[0][a[i]]) ans = false;
        if (a[i - 1] != fa[0][a[i]]) {
            for (int u = a[i - 1]; !(st[u] <= st[a[0]] && st[a[0]] < ed[u]) && u != fa[0][a[i]]; u = fa[0][u]) {
                if (sum(st[u], ed[u]) != ed[u] - st[u]) {
                    ans = false;
                    break;
                }
            }
        }
        add(st[a[i]], 1);
    }
    for (int x: a) add(st[x], -1);
    cout << (ans ? "Yes" : "No") << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> q;
    for (int e = 1; e < n; e++) {
        int u, v; cin >> u >> v;
        u--, v--;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(0, 0);
    for (int b = 1; b < 19; b++) {
        for (int u = 0; u < n; u++)
            fa[b][u] = fa[b - 1][fa[b - 1][u]];
    }
    while (q--) {
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 12004kb

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: 78ms
memory: 9728kb

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
Yes
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 30th words differ - expected: 'No', found: 'Yes'