Submission #963267

# Submission time Handle Problem Language Result Execution time Memory
963267 2024-04-14T19:53:40 Z TimAni Bitaro’s Party (JOI18_bitaro) C++17
7 / 100
2000 ms 17084 KB
//start-time: 2024-04-14 19:39:32
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const ll w = 330;

void solve(){
    ll n, m, q;
    cin >> n >> m >> q;
    vector<vector<ll>> g(n);
    vector<vector<ll>> gR(n);
    for(ll i = 0; i < m; i++){
        ll u, v;
        cin >> u >> v;
        u--; v--;
        g[u].push_back(v);
        gR[v].push_back(u);
    }
    
    vector<ll> vis(n), topo;
    auto dfs = [&](ll u, auto&&dfs) -> void {
        vis[u] = 1;
        for(auto v : g[u]){
            if(!vis[v]){
                dfs(v, dfs);
            }
        }
        topo.push_back(u);
    };

    for(int i = 0; i < n; i++){
        if(vis[i] == 0){
            dfs(i, dfs);
        }
    }

    reverse(topo.begin(), topo.end());
    
    vector<vector<array<ll, 2>>> dp(n);
    vector<ll> val(n, -1);

    for(auto u : topo){
        val.assign(n, -1);
        vector<ll> aux;
        aux.push_back(u);
        val[u] = 0;

        for(auto v : gR[u]){
            for(auto [value, ind] : dp[v]){
                aux.push_back(ind);
                val[ind] = max(val[ind], value + 1);
            }
        }

        sort(aux.begin(), aux.end());
        aux.erase(unique(aux.begin(), aux.end()), aux.end());
        sort(aux.begin(), aux.end(), [&](int a, int b){
            return val[a] > val[b];
        });
        for(auto el : aux){
            if(dp[u].size() < w){
                dp[u].push_back({val[el], el});
            }
        }
    }
    vector<bool> c(100'001);
    vector<int> naive(n);
    for(ll i = 0; i < q; i++){
        c.assign(100'001, 0);
        ll u, y;
        cin >> u >> y;
        u--;
        for(ll j = 0; j < y; j++){
            ll a; cin >> a;
            c[--a] = 1;
        }

        ll ans = -1;
        if(y < w) {
            for(auto [v, i] : dp[u]){
                if(!c[i]){
                    ans = max(ans, v);
                }
            }
        }
        else {
            naive.assign(n, 0);
            for(auto v : topo){
                naive[v] = c[v] ? -1 : 0;
                for(auto el : gR[v]){
                    naive[v] = max(naive[v], naive[el] + (naive[el] != -1));
                }
            }
            ans = naive[u];
        }
        
            //for(auto el : naive){
                //cout << el << ' ';
            //}
            //cout << endl;
        cout << ans << endl;
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    ll T = 1;
    //cin >> T;
    while(T--) solve();
    return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 4 ms 1376 KB Output is correct
6 Correct 4 ms 1372 KB Output is correct
7 Correct 4 ms 1372 KB Output is correct
8 Correct 21 ms 7260 KB Output is correct
9 Correct 20 ms 7260 KB Output is correct
10 Correct 23 ms 7252 KB Output is correct
11 Correct 22 ms 6484 KB Output is correct
12 Correct 10 ms 3164 KB Output is correct
13 Correct 25 ms 5980 KB Output is correct
14 Correct 19 ms 4444 KB Output is correct
15 Correct 10 ms 2652 KB Output is correct
16 Correct 16 ms 4444 KB Output is correct
17 Correct 16 ms 4956 KB Output is correct
18 Correct 10 ms 3160 KB Output is correct
19 Correct 16 ms 4960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 4 ms 1376 KB Output is correct
6 Correct 4 ms 1372 KB Output is correct
7 Correct 4 ms 1372 KB Output is correct
8 Correct 21 ms 7260 KB Output is correct
9 Correct 20 ms 7260 KB Output is correct
10 Correct 23 ms 7252 KB Output is correct
11 Correct 22 ms 6484 KB Output is correct
12 Correct 10 ms 3164 KB Output is correct
13 Correct 25 ms 5980 KB Output is correct
14 Correct 19 ms 4444 KB Output is correct
15 Correct 10 ms 2652 KB Output is correct
16 Correct 16 ms 4444 KB Output is correct
17 Correct 16 ms 4956 KB Output is correct
18 Correct 10 ms 3160 KB Output is correct
19 Correct 16 ms 4960 KB Output is correct
20 Correct 1538 ms 15376 KB Output is correct
21 Correct 1474 ms 15272 KB Output is correct
22 Correct 1490 ms 15264 KB Output is correct
23 Execution timed out 2048 ms 17084 KB Time limit exceeded
24 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 4 ms 1376 KB Output is correct
6 Correct 4 ms 1372 KB Output is correct
7 Correct 4 ms 1372 KB Output is correct
8 Correct 21 ms 7260 KB Output is correct
9 Correct 20 ms 7260 KB Output is correct
10 Correct 23 ms 7252 KB Output is correct
11 Correct 22 ms 6484 KB Output is correct
12 Correct 10 ms 3164 KB Output is correct
13 Correct 25 ms 5980 KB Output is correct
14 Correct 19 ms 4444 KB Output is correct
15 Correct 10 ms 2652 KB Output is correct
16 Correct 16 ms 4444 KB Output is correct
17 Correct 16 ms 4956 KB Output is correct
18 Correct 10 ms 3160 KB Output is correct
19 Correct 16 ms 4960 KB Output is correct
20 Correct 1538 ms 15376 KB Output is correct
21 Correct 1474 ms 15272 KB Output is correct
22 Correct 1490 ms 15264 KB Output is correct
23 Execution timed out 2048 ms 17084 KB Time limit exceeded
24 Halted 0 ms 0 KB -