Submission #521196

#TimeUsernameProblemLanguageResultExecution timeMemory
521196JomnoiBitaro’s Party (JOI18_bitaro)C++17
0 / 100
2 ms2636 KiB
#include <bits/stdc++.h>
#define DEBUG 0
using namespace std;

const int N = 1e5 + 10;
const int M = 30;
const int INF = 1e9 + 7;

vector <int> rev[N];
pair <int, int> node[N][M];
int sz[N];
stack <int> tmp, tmp2;
bool notuse[N];
int dp[N];

int solve(int u) {
    if(dp[u] != -1) {
        return dp[u];
    }

    dp[u] = -INF;
    for(auto v : rev[u]) {
        int x = solve(v);
        if(x != -INF) {
            dp[u] = max(dp[u], solve(v) + 1);
        }
    }

    if(notuse[u] == false) {
        dp[u] = max(dp[u], 0);
    }
    return dp[u];
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n, m, q;
    cin >> n >> m >> q;
    while(m--) {
        int s, e;
        cin >> s >> e;
        rev[e].push_back(s);
    }

    for(int i = 1; i <= n; i++) {
        sz[i] = 1;
        for(int j = 1; j <= M; j++) {
            node[i][j] = make_pair(-INF, -1);
        }
    }
    for(int u = 1; u <= n; u++) {
        for(auto v : rev[u]) {
            tmp.push(v);
        }

        for(int i = 1; i <= M; i++) {
            pair <int, int> best = make_pair(-INF, -1);
            int idx = -1;
            for(auto v : rev[u]) {
                while(notuse[node[v][sz[v]].second] == true) {
                    sz[v]++;
                }
                if(best.first < node[v][sz[v]].first) {
                    best = node[v][sz[v]];
                    idx = v;
                }
            }
            if(best.first == -INF) {
                node[u][i] = make_pair(0, u);
                break;
            }

            node[u][i] = make_pair(best.first + 1, best.second);
            tmp2.push(best.second);
            notuse[best.second] = true;
            sz[idx]++;
        }

        while(!tmp.empty()) {
            sz[tmp.top()] = 1;
            tmp.pop();
        }
        while(!tmp2.empty()) {
            notuse[tmp2.top()] = false;
            tmp2.pop();
        }
    }

    if(DEBUG) {
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= M; j++) {
                if(node[i][j].first == -INF) {
                    break;
                }

                cout << i << " : " << node[i][j].first << " " << node[i][j].second << endl;
            }
            cout << endl;
        }
    }

    while(q--) {
        int t, y;
        cin >> t >> y;
        while(y--) {
            int c;
            cin >> c;
            notuse[c] = true;
            tmp.push(c);
        }

        int ans = -INF;
        if(y < M) {
            for(int i = 1; i <= M; i++) {
                if(notuse[node[t][i].second] == false and node[t][i].first != -INF) {
                    ans = node[t][i].first;
                    break;
                }
            }
        }
        else {
            for(int i = 1; i <= t; i++) {
                dp[i] = -1;
            }
            ans = solve(t);
        }

        if(ans == -INF) {
            ans = -1;
        }
        cout << ans << '\n';

        while(!tmp.empty()) {
            notuse[tmp.top()] = false;
            tmp.pop();
        }
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...