Submission #1316576

#TimeUsernameProblemLanguageResultExecution timeMemory
1316576aaaaaaaaBitaro’s Party (JOI18_bitaro)C++20
7 / 100
2096 ms134892 KiB
#include <bits/stdc++.h>
using namespace std;
const int inf = -1e9;
const int mxN = 1e5 + 10;
const int mxB = 343;
vector<vector<int>> Query[mxN];
vector<int> adj[mxN], radj[mxN];
int dist[mxB][mxN];
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(nullptr); cout.tie(nullptr);
    int n, m, q;
    cin >> n >> m >> q;
    vector<int> ans(q + 1, -1);

    int block_size = sqrt(n + 1) + 1;
    
    for(int i = 0, u, v; i < m; ++i){
        cin >> u >> v; --u, --v;
        adj[u].push_back(v);
        radj[v].push_back(u);
    }

    vector<int> dp(n + 1, inf);

    auto dfs = [&](auto &self, int u, int x) -> int {
        if(u > x) return inf;
        if(u == x) return 0;
        if(dp[u] != inf) return dp[u];
        int ans = inf;
        for(auto it : adj[u]){
            ans = max(ans, self(self, it, x) + 1);
        }
        return dp[u] = ans;
    };


    auto get = [&](int l, int r, int x) -> int {
        int cans = -1;
        for(int i = l; i <= r;){
            if(i % block_size == 0 && i + block_size - 1 <= r){
                cans = max(cans, dist[i / block_size][x]);
                i += block_size;
            }else{
                cans = max(cans, dfs(dfs, i, x));
                i += 1;
            }
        }
        return cans;
    };

    auto process = [&](int x) -> void {
        dp = vector<int>(n + 1, inf);
        for(auto cur : Query[x]){
            int cur_ans = -1;
            for(int j = 0; j < (int) cur.size() - 2; ++j){
                int l = cur[j] + 1, r = cur[j + 1] - 1;
                if(l <= r){
                    cur_ans = max(cur_ans, get(l, r, x));
                }
            }
            ans[cur.back()] = cur_ans;
        }
    };

    for(int i = 0, block = 0; i < n; i += block_size, block += 1){
        int j = i + block_size - 1;
        priority_queue<pair<int, int>> pq;
        for(int k = 1; k <= n; ++k){
            dist[block][k] = -1;
        }
        for(int k = i; k <= j; ++k){
            pq.push({0, k});
            dist[block][k] = 0;
        }
        while(pq.size()){
            auto [d, u] = pq.top();
            pq.pop();
            for(auto v : adj[u]){
                if(dist[block][v] < d + 1){
                    dist[block][v] = d + 1;
                    pq.push({d + 1, v});
                }
            }
        }
    }
    for(int i = 1, x, y; i <= q; ++i){
        cin >> x >> y; --x;
        vector<int> banned = {-1};
        int mx = 0;
        for(int j = 0, vrtx; j < y; ++j){
            cin >> vrtx; --vrtx;
            if(vrtx <= x){
                banned.push_back(vrtx);
            }
            mx = max(mx, vrtx);
        }
        if(mx > x){
            banned.push_back(mx);
        }else{
            banned.push_back(n);
        }
        sort(banned.begin(), banned.end());
        banned.push_back(i);
        Query[x].push_back(banned);
    }
    for(int i = 0; i < n; ++i){
        process(i);
    }
    for(int i = 1; i <= q; ++i) cout << ans[i] << "\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...