QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#661273#8551. DFS Order 5hjxddlWA 31ms9484kbC++204.3kb2024-10-20 15:34:452024-10-20 15:34:45

Judging History

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

  • [2024-10-20 15:34:45]
  • 评测
  • 测评结果:WA
  • 用时:31ms
  • 内存:9484kb
  • [2024-10-20 15:34:45]
  • 提交

answer

// Coded by hjxddl
#include <bits/stdc++.h>
#define ll  long long
#define db  double
#define mid (l + r >> 1)
const int N = 1e5 + 5;
std::vector<std::vector<int>> e(N);
int n, q;
int sum[N << 2], set[2][N << 2];
void pushdown(int x, int l, int r) {
    if (!set[0][x] && !set[1][x]) return;
    int tar = set[0][x] ? 0 : 1;
    // std::cerr << x << " " << l << " " << r << '\n';
    sum[x << 1] = (mid - l + 1) * tar;
    sum[x << 1 | 1] = (r - (mid + 1) + 1) * tar;
    set[tar][x << 1] = set[tar][x << 1 | 1] = 1;
    set[tar ^ 1][x << 1] = set[tar ^ 1][x << 1 | 1] = 0;
    set[0][x] = set[1][x] = 0;
}
void change(int opt, int l1, int r1, int x = 1, int l = 1, int r = n) {
    if (l1 <= l && r <= r1) {
        sum[x] = (r - l + 1) * opt;
        set[opt][x] = 1;
        set[opt ^ 1][x] = 0;
        return;
    }
    // std::cerr << opt << " " << l1 << " " << r1 << " " << x << " " << l << " " << r << '\n';
    pushdown(x, l, r);
    if (l1 <= mid)
        change(opt, l1, r1, x << 1, l, mid);
    if (r1 > mid)
        change(opt, l1, r1, x << 1 | 1, mid + 1, r);
    sum[x] = sum[x << 1] + sum[x << 1 | 1];
}
bool query(int k, int x = 1, int l = 1, int r = n) {
    if (sum[x] == r - l + 1)
        return true;
    if (sum[x] == 0)
        return false;
    // std::cerr << k << " " << x << " " << l << " " << r << " " << sum[x] << '\n';
    pushdown(x, l, r);
    int ans = false;
    if (k <= mid)
        ans = query(k, x << 1, l, mid);
    else
        ans = query(k, x << 1 | 1, mid + 1, r);
    sum[x] = sum[x << 1] + sum[x << 1 | 1];
    return ans;
}
int fa[N], son[N], dfn[N], tot, sz[N], dep[N], top[N], r_dfn[N];
void dfs1(int x, int f) {
    dep[x] = dep[f] + 1;
    fa[x] = f;
    sz[x] = 1;
    int maxson = 0;
    for (int y : e[x]) {
        if (y == f) continue;
        dfs1(y, x);
        sz[x] += sz[y];
        if (maxson < sz[y]) {
            son[x] = y;
            maxson = sz[y];
        }
    }
}
void dfs2(int x, int topx) {
    dfn[x] = ++tot;
    r_dfn[tot] = x;
    top[x] = topx;
    if (!son[x]) return;
    dfs2(son[x], topx);
    for (int y : e[x]) {
        if (y == fa[x] || y == son[x]) continue;
        dfs2(y, y);
    }
}
int lca(int x, int y) {
    while (top[x] != top[y]) {
        if (dep[top[x]] > dep[top[y]]) {
            x = fa[top[x]];
        } else
            y = fa[top[y]];
    }
    return dep[x] < dep[y] ? x : y;
}
int ance(int x, int k) {
    if (!k) return x;
    while (dep[x] - dep[fa[top[x]]] <= k) {
        k -= dep[x] - dep[fa[top[x]]];
        x = fa[top[x]];
    }
    return r_dfn[dfn[x] - k];
}
void solve() {
    std::cin >> n >> q;
    for (int i = 1; i < n; i++) {
        int u, v;
        std::cin >> u >> v;
        e[u].push_back(v);
        e[v].push_back(u);
    }
    dfs1(1, 1);
    dfs2(1, 1);
    std::vector<int> a(n + 5);
    while (q--) {
        change(0, 1, n);
        int k;
        std::cin >> k;
        for (int i = 1; i <= k; i++)
            std::cin >> a[i];
        int x = a[1];
        while (top[x] != 1) {
            change(1, dfn[top[x]], dfn[x]);
            x = fa[top[x]];
        }
        change(1, dfn[1], dfn[x]);
        bool vis = 0;
        for (int i = 2; i <= k; i++) {
            int x = a[i], lst = a[i - 1];
            if (son[lst]) {
                if (fa[x] != lst) {
                    std::cout << "No" << '\n';
                    vis = 1;
                    break;
                }
                change(1, dfn[x], dfn[x]);
            } else {
                int anc = lca(lst, x);
                int top1 = ance(x, dep[x] - dep[anc] - 1);
                // std::cerr << anc << " " << top1 << " " << x << " " << fa[x] << " " << lst << " " << query(dfn[x]) << '\n';
                if (fa[x] != anc || query(dfn[x])) {
                    std::cout << "No" << '\n';
                    vis = 1;
                    break;
                }
                change(1, dfn[top1], dfn[top1] + sz[top1] - 1);
            }
        }
        if (vis) continue;
        std::cout << "Yes\n";
    }
}
int main() {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0), std::cout.tie(0);
    int t = 1;
    // std::cin >> t;
    while (t--) {
        solve();
    }
    std::cout << std::flush;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 31ms
memory: 9348kb

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