QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#451540#8720. BFS 序 0propane#AC ✓110ms43284kbC++206.9kb2024-06-23 16:34:432024-06-23 16:34:43

Judging History

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

  • [2024-06-23 16:34:43]
  • 评测
  • 测评结果:AC
  • 用时:110ms
  • 内存:43284kb
  • [2024-06-23 16:34:43]
  • 提交

answer

#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
using LL = long long;

// 1_based HLD
// struct Edge{
//     int to;
//     int w;
//     operator int() const { return to; }
// };

using Edge = int;
struct HLD{
    int n;
    vector<int> sz, top, dep, fa, in, out, seq;
    vector<vector<Edge> > g;
    int ts;

    HLD(const vector<vector<Edge> > &g, int root = 1) : n(int(g.size()) - 1), g(g)  {
        ts = 0;
        sz.resize(n + 1);
        top.resize(n + 1);
        dep.resize(n + 1);
        fa.resize(n + 1);
        in.resize(n + 1);
        out.resize(n + 1);
        seq.resize(n + 1);
        dep[root] = 1;
        top[root] = root;
        dfs_sz(root);
        dfs_hld(root);
    }

    void dfs_sz(int u){
        if (fa[u]){
            for(auto it = g[u].begin(); it != g[u].end(); it++){
                if (*it == fa[u]){
                    g[u].erase(it);
                    break;
                }
            }
        }
        sz[u] = 1;
        for(auto &j : g[u]){
            fa[j] = u;
            dep[j] = dep[u] + 1;
            dfs_sz(j);
            sz[u] += sz[j];
            if (sz[j] > sz[g[u][0]])
                swap(j, g[u][0]);
        }
    }

    void dfs_hld(int u){
        in[u] = ++ts;
        seq[in[u]] = u;
        for (auto j : g[u]){
            top[j] = (j == g[u][0] ? top[u] : j);
            dfs_hld(j);
        }
        out[u] = ts;
    }

    int 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;
    }

    int dist(int u, int v){
        return dep[u] + dep[v] - 2 * dep[lca(u, v)];
    }

    bool in_subtree(int u, int v){
        return in[v] <= in[u] && in[u] <= out[v];
    }
    
    int jump(int u, int k) {
        if (dep[u] < k){
            return -1;
        }
        int d = dep[u] - k;
        while (dep[top[u]] > d){
            u = fa[top[u]];
        }
        return seq[in[u] - dep[u] + d];
    }
    
    int rooted_lca(int a, int b, int c){
        return lca(a, b) ^ lca(b, c) ^ lca(c, a);
    }

    template<typename Q>
    void modify_path(int u, int v, const Q &q, bool edge = false){
        while(top[u] != top[v]){
            if (dep[top[u]] < dep[top[v]]) swap(u, v);
            q(in[top[u]], in[u]);
            u = fa[top[u]];        
        }
        if (dep[u] > dep[v]) swap(u, v);
        q(in[u] + edge, in[v]);
    }

    template<typename Q>
    void modify_subtree(int u, const Q &q){
        q(in[u], out[u]);
    }  

    template<typename T, typename Q>
    T query_path(int u, int v, const Q &q, bool edge = false){
        T ret = T();
        while(top[u] != top[v]){
            if (dep[top[u]] < dep[top[v]]) swap(u, v);
            ret = q(in[top[u]], in[u]) + ret;
            u = fa[top[u]];
        }
        if (dep[u] > dep[v]) swap(u, v);
        return q(in[u] + edge, in[v]) + ret;
    }

    template<typename T, typename Q>
    T query_subtree(int u, const Q &q){
        return q(in[u], out[u]);
    }

    template<typename T, typename Q, typename F>
    T query_path_noncommutative(int u, int v, const Q &q, const F &f, bool edge = false){
        T left = T(), right = T();
        while(top[u] != top[v]){
            if (dep[top[u]] < dep[top[v]]) swap(u, v), swap(left, right);
            left = q(in[top[u]], in[u]) + left;
            u = fa[top[u]];
        }
        if (dep[u] > dep[v]) swap(u, v), swap(left, right);
        return f(left, q(in[u] + edge, in[v]) + right);
    }
    
    pair<vector<int>, vector<pair<int, int> > > compress(vector<int> v){
        auto cmp = [&](int a, int b) { return in[a] < in[b]; };
        sort(v.begin(), v.end(), cmp);
        v.erase(unique(v.begin(), v.end()), v.end());
        const int k = (int)v.size();
        for(int i = 0; i + 1 < k; i++){
            v.push_back(lca(v[i], v[i + 1]));
        }
        sort(v.begin(), v.end(), cmp);
        v.erase(unique(v.begin(), v.end()), v.end());
        vector<pair<int, int> > edges;
        vector<int> stk;
        for(auto x : v){
            while(!stk.empty() && out[stk.back()] < in[x]){
                stk.pop_back();
            }
            if (!stk.empty()){
                edges.push_back({stk.back(), x});
            }
            stk.push_back(x);
        }
        return {v, edges};
    }

};

int main(){

#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif

    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);

    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);
    }
    HLD hld(g);
    vector<vector<int> > ng(n + 1);
    vector<bool> v(n + 1);
    vector<int> din(n + 1);
    int m;
    cin >> m;
    while(m--){
        int k;
        cin >> k;
        bool ok = true;
        vector<int> a(k);
        for(int i = 0; i < k; i++){
            cin >> a[i];
            if (v[a[i]]){
                ok = false;
            }
            v[a[i]] = true;
        }
        for(auto x : a) v[x] = false;
        for(int i = 0; i + 1 < k; i++){
            if (hld.dep[a[i]] > hld.dep[a[i + 1]]){
                ok = false;
            }
        }
        if (!ok){
            cout << "No" << '\n';
            continue;
        }
        vector<pair<int, int> > edge;
        vector<int> p;
        for(int i = 0; i + 1 < k; i++){
            int x = a[i], y = a[i + 1];
            if (hld.dep[x] == hld.dep[y]){
                int anc = hld.lca(a[i], a[i + 1]);
                int t1 = hld.jump(x, hld.dep[x] - hld.dep[anc] - 1);
                int t2 = hld.jump(y, hld.dep[y] - hld.dep[anc] - 1);
                edge.push_back({t1, t2});
                p.push_back(t1); p.push_back(t2);
            }
        }
        sort(p.begin(), p.end());
        p.erase(unique(p.begin(), p.end()), p.end());
        for(auto x : p){
            ng[x].clear();
            din[x] = 0;
        }
        for(auto [x, y] : edge){
            ng[x].push_back(y);
            din[y] += 1;
        }
        int cnt = 0;
        queue<int> q;
        for(auto x : p){
            if (din[x] == 0){
                q.push(x);
            }
        }
        while(!q.empty()){
            int t = q.front();
            q.pop();
            cnt += 1;
            for(auto j : ng[t]){
                if (--din[j] == 0){
                    q.push(j);
                }
            }
        }
        if (cnt != p.size()) ok = false;
        cout << (ok ? "Yes" : "No") << '\n';
    }

}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0
Accepted
time: 110ms
memory: 43268kb

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

result:

ok 254 token(s): yes count is 89, no count is 165

Test #3:

score: 0
Accepted
time: 92ms
memory: 43208kb

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:

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

result:

ok 266 token(s): yes count is 102, no count is 164

Test #4:

score: 0
Accepted
time: 104ms
memory: 43284kb

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

result:

ok 252 token(s): yes count is 87, no count is 165

Test #5:

score: 0
Accepted
time: 4ms
memory: 3848kb

input:

10
1 1 3 3 1 3 1 3 9
10000
4 3 5 6 9
5 3 1 4 9 6
5 6 4 3 5 7
28 8 3 4 3 1 6 3 5 8 7 2 4 4 4 2 7 5 9 2 4 10 8 4 10 3 4 5 7
3 5 2 9
6 4 7 2 3 9 5
5 9 2 5 6 1
12 3 5 8 8 1 3 5 8 4 5 9 1
29 3 9 9 1 5 8 5 5 6 10 8 8 4 8 4 2 8 10 5 7 3 7 1 8 4 7 5 1 6
7 5 9 8 3 2 1 6
4 4 1 9 5
22 10 9 5 4 5 8 10 3 2 1 10 ...

output:

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

result:

ok 10000 token(s): yes count is 1058, no count is 8942

Test #6:

score: 0
Accepted
time: 0ms
memory: 3632kb

input:

9
1 2 1 2 2 4 2 6
10000
6 5 1 4 3 7 8
14 6 8 7 9 7 5 1 8 3 6 3 8 5 2
2 6 9
17 7 8 6 5 2 1 5 5 9 3 4 1 5 2 7 2 5
3 8 6 5
5 3 9 1 6 8
8 3 5 8 7 9 4 6 2
4 8 9 6 5
7 4 7 2 9 3 8 1
6 1 4 9 5 6 2
4 7 5 4 3
5 9 5 2 1 3
4 4 5 7 3
3 9 1 8
4 4 2 8 6
26 7 4 7 6 1 7 9 4 5 5 8 9 8 2 4 8 9 6 2 8 6 4 5 5 4 4
45 3 ...

output:

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

result:

ok 10000 token(s): yes count is 1492, no count is 8508

Test #7:

score: 0
Accepted
time: 0ms
memory: 3540kb

input:

8
1 1 2 4 2 4 6
10000
5 2 6 6 2 1
3 7 3 8
2 8 1
5 7 5 1 2 7
3 2 3 5
3 8 4 3
5 8 2 4 5 3
4 3 2 1 8
6 6 8 4 5 3 1
6 3 1 2 7 4 6
4 8 6 4 3
5 4 7 6 8 5
11 2 1 6 5 2 7 1 3 3 5 4
4 6 1 7 2
7 6 2 7 8 4 1 3
3 4 6 7
3 7 1 6
38 3 4 6 6 3 4 8 1 5 3 1 8 8 6 2 3 6 7 8 8 4 2 3 7 3 3 6 7 7 5 7 5 8 8 5 4 5 7
1 8
5 ...

output:

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

result:

ok 10000 token(s): yes count is 1568, no count is 8432

Test #8:

score: 0
Accepted
time: 3ms
memory: 3556kb

input:

7
1 2 3 1 2 5
10000
5 1 7 3 5 6
5 5 3 2 7 4
4 5 2 6 4
12 3 1 5 5 6 6 3 5 3 5 3 3
11 2 3 2 6 7 3 6 6 3 3 3
2 3 2
6 1 3 4 2 6 7
3 2 7 1
6 5 2 7 3 4 1
27 2 5 6 1 7 1 7 1 4 6 7 1 6 2 1 7 1 3 4 4 7 7 2 6 4 4 3
2 5 6
1 7
3 3 1 4
4 1 7 3 4
3 4 2 1
4 4 1 6 7
4 6 2 7 4
4 5 1 6 4
1 3
5 1 2 3 5 7
5 2 3 7 6 5
1...

output:

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

result:

ok 10000 token(s): yes count is 2205, no count is 7795

Extra Test:

score: 0
Extra Test Passed