QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#418891#8720. BFS 序 0oO_OoWA 142ms64092kbC++146.1kb2024-05-23 16:19:542024-05-23 16:19:55

Judging History

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

  • [2024-05-23 16:19:55]
  • 评测
  • 测评结果:WA
  • 用时:142ms
  • 内存:64092kb
  • [2024-05-23 16:19:54]
  • 提交

answer

#include <bits/stdc++.h>
// #include <ext/pb_ds/hash_policy.hpp>
// #include <ext/pb_ds/assoc_container.hpp>

// #pragma GCC optimize(1)
// #pragma GCC optimize(2)
// #pragma GCC optimize(3,"Ofast","inline")

#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f

#define lowbit(x)   ((x) & -(x))
#define all(x)      x.begin(), x.end()
#define set_all(x, y)   memset(x, y, sizeof (x))
// #define endl    '\n'
// #define int long long
#define ff  first
#define ss  second
#define pb   push_back

#define typet typename T
#define typeu typename U
#define types typename... Ts
#define tempt template <typet>
#define tempu template <typeu>
#define temps template <types>
#define tandu template <typet, typeu>

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) do {} while (false)
#endif

using namespace std;
// using namespace __gnu_pbds;

using ULL = unsigned long long;
using LL = long long;
using PII = pair<int, int>;
using PLL = pair<LL, LL>;
using PIB = pair<int, bool>;
using PDD = pair<double, double>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<LL>;
using vvl = vector<vl>;
using vpi = vector<PII>;
// typedef __int128_t i128;

struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x ^= x << 13;
    x ^= x >> 7;
    x ^= x << 17;
    return x;
  }
  size_t operator () (uint64_t x) const {
    static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};

const int N = 300010,  M = 2 * N, K = 31, Mod = 1e9 + 7;
const double pi = acos(-1), eps = 1e-7;
const ULL P = 13331;
struct node {
    int l, r;
    int val;
}tr[N << 2];

void pushup(int u) {
    tr[u].val = max(tr[u << 1].val, tr[u << 1 | 1].val);
}

void build(int u, int l, int r) {
    if (l == r) {
        tr[u] = {l, r, 0};
        return;
    }
    tr[u] = {l, r, 0};
    int mid = l + r >> 1;
    build(u << 1, l, mid);
    build(u << 1 | 1, mid + 1, r);
    pushup(u);
}

int query(int u, int l, int r) {
    if (tr[u].l >= l && tr[u].r <= r) return tr[u].val;
    int mid = tr[u].l + tr[u].r >> 1;
    int res = 0;
    if (l <= mid) res = max(res, query(u << 1, l, r));
    if (r > mid) res = max(res, query(u << 1 | 1, l, r));
    return res;
}

void modify(int u, int x, int d) {
    if (tr[u].l == tr[u].r) {
        tr[u].val = d;
    } else {
        int mid = tr[u].l + tr[u].r >> 1;
        if (x <= mid) modify(u << 1, x, d);
        else modify(u << 1 | 1, x, d);
        pushup(u);
    }
}

void solut(int I) {
    int n;
    cin >> n;
    vector<vector<int>> g(n + 1);
    for (int i = 2; i <= n; i ++) {
        int x;
        cin >> x;
        g[x].push_back(i);
    }
    vector<int> sz(n + 1);
    vector<int> d(n + 1);
    vector<int> dfn(n + 1);
    int tot = 0;
    vector<vector<int>> fa(n + 1, vector<int>(20));
    auto dfs = [&](auto&& self, int u, int f) -> void{
        dfn[u] = ++ tot;
        sz[u] = 1;
        fa[u][0] = f;
        for (int i = 1; i < 20; i ++) {
            fa[u][i] = fa[fa[u][i - 1]][i - 1];
        }
        for (auto v: g[u]) {
            if (v == f) continue;
            d[v] = d[u] + 1;
            self(self, v, u);
            sz[u] += sz[v];
        }
    };
    d[1] = 1;
    dfs(dfs, 1, 1);
    auto lca = [&](int u, int v) -> PII {
        if (d[u] < d[v]) {
            assert(false);
            swap(u, v);
        }
        for (int i = 19; i >= 0; i --) {
            if (d[fa[u][i]] >= d[v]) u = fa[u][i];
        }
        if (u == v) return {u, -1};
        for (int i = 19; i >= 0; i --) {
            if (fa[u][i] != fa[v][i]) {
                u = fa[u][i];
                v = fa[v][i];
            }
        }
        return {u, v};
    };
    build(1, 1, n);

    int q;
    cin >> q;
    while (q --) {
        bool check = true;
        int m;
        cin >> m;
        vector<int> b(m + 1);
        for (int i = 1; i <= m; i ++) {
            cin >> b[i];
        } 
        // debug(1);
        // return;
        int prev = 0;
        for (int i = 1; i <= m; i ++) {
            int j = i + 1;
            if (d[b[i]] < prev) {
                check = false;
                goto label;
            }
            // modify(1, dfn[b[i]], i);
            debug(1);
            // debug(b[i], i, query(1, dfn[b[i]], dfn[b[i]] + sz[b[i]] - 1));
            while (j <= m && d[b[j]] == d[b[i]]) {
                j ++;
            }
            if (j - i == 1) {
                modify(1, dfn[b[j - 1]], j - 1);
            } else {
                auto [x, y] = lca(b[i], b[i + 1]);
                int lx = query(1, dfn[x], dfn[x] + sz[x] - 1);
                int rx = query(1, dfn[y], dfn[y] + sz[y] - 1);
                if (lx > rx) {
                    check = false;
                    goto label;
                }
                modify(1, dfn[b[i]], i);
                modify(1, dfn[b[i + 1]], i + 1);
                for (int k = i + 2; k < j; k ++) {
                    auto [x, y] = lca(b[k - 1], b[k]);
                    assert(y != -1);
                    // debug(b[j - 1], b[j]);
                    int lx = query(1, dfn[x], dfn[x] + sz[x] - 1);
                    int rx = query(1, dfn[y], dfn[y] + sz[y] - 1);
                    debug(lx, rx);
                    if (lx > rx) {
                        check = false;
                        goto label;
                    }
                    modify(1, dfn[b[k]], k);
                }
            }

            prev = d[b[i]];
            i = j - 1;
        }

        label:
        for (int i = 1; i <= m; i ++) {
            modify(1, dfn[b[i]], 0);
        }
        if (check) cout << "Yes" << '\n';
        else cout << "No" <<'\n';
        assert(query(1, 1, n) == 0);
    }
}

/*
6
1 1 3 2 4
10
4 3 6 2 5
1 4
3 2 4 5
5 2 5 4 6 3
3 1 4 2
3 5 6 3
5 4 5 2 6 1
4 4 3 2 5
4 4 6 2 3
3 3 2 6
*/

signed main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int T;
    T = 1;
    // cin >> T;
    for (int _ = 1; _ <= T; _ ++) {
        solut(_);
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

6
1 1 3 2 4
10
4 3 6 2 5
1 4
3 2 4 5
5 2 5 4 6 3
3 1 4 2
3 5 6 3
5 4 5 2 6 1
4 4 3 2 5
4 4 6 2 3
3 3 2 6

output:

No
Yes
Yes
No
No
No
No
No
No
Yes

result:

ok 10 token(s): yes count is 3, no count is 7

Test #2:

score: -100
Wrong Answer
time: 142ms
memory: 64092kb

input:

300000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...

output:

No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
Yes
Yes
No
No
No
No
No
No
No
No
Yes
No
No
No
No
No
Yes
No
No
No
Yes
No
No
No
No
No
No
No
Yes
No
Yes
No
No
No
No
No
No
No
Yes
Yes
No
No
Yes
No
No
No
No
No
No
Yes
No
No
No
No
No
No
No
No
Yes
No
No
No
Yes
No
No
No
No
No
No
No
Yes
No
No
No
Yes
No
No
No
No
No
...

result:

wrong answer expected YES, found NO [2nd token]