Submission #1085315

# Submission time Handle Problem Language Result Execution time Memory
1085315 2024-09-07T23:49:14 Z eysbutno Bitaro’s Party (JOI18_bitaro) C++17
14 / 100
2000 ms 114028 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = array<int, 2>;
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()

constexpr int B = 65;
int main() {
    cin.tie(0) -> sync_with_stdio(0);
    int n, m, q;
    cin >> n >> m >> q;
    vector<vector<int>> adj(n);
    vector<int> deg(n);
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        --u, --v;
        adj[u].push_back(v);
        ++deg[v];
    }
    vector<vector<pii>> best(n);
    auto cur = deg;
    queue<int> top;
    for (int i = 0; i < n; i++) {
        if (!cur[i]) { top.push(i); }
    }
    const auto merge = [&](int from, int to) -> void {
        map<int, int> mp;
        for (auto [node, dist] : best[from]) {
            mp[node] = max(mp[node], dist + 1);
        }
        for (auto [node, dist] : best[to]) {
            mp[node] = max(mp[node], dist);
        }
        best[to].clear();
        for (auto [node, dist] : mp) {
            best[to].push_back({node, dist});
        }
        sort(all(best[to]), [&](auto &x, auto &y) -> bool {
            return x[1] > y[1];
        });
        if (sz(best[to]) > B) { best[to].resize(B); }
    };
    while (!top.empty()) {
        int u = top.front(); top.pop();
        if (sz(best[u]) < B) {
            best[u].push_back({u, 0});
        }
        for (int v : adj[u]) {
            merge(u, v);
            if (!(--cur[v])) { top.push(v); }
        }
    }
    const auto check = [&](int t, const set<int> &bad) -> int {
        for (auto [node, dist] : best[t]) {
            if (!bad.count(node)) {
                return dist;
            }
        }
        return -1;
    };
    const auto brute_force = [&](int t, const set<int> &bad) -> int {
        queue<int> q;
        auto cur = deg;
        vector<int> best(n, -1);
        for (int i = 0; i < n; i++) {
            if (!cur[i]) { q.push(i); }
        }
        while (!q.empty()) {
            int u = q.front(); q.pop();
            if (!bad.count(u)) { best[u] = max(0, best[u]); }
            if (u == t) { break; }
            for (int v : adj[u]) {
                if (best[u] != -1) { best[v] = max(best[v], best[u] + 1); }
                if (!--cur[v]) { q.push(v); }
            }
        }
        return best[t];
    };
    while (q--) {
        int t, y;
        cin >> t >> y;
        --t;
        set<int> bad;
        for (int i = 0; i < y; i++) {
            int x; cin >> x;
            bad.insert(--x);
        }
        if (y >= B) {
            cout << brute_force(t, bad) << "\n";
        } else {
            cout << check(t, bad) << "\n";
        }
    }
    /*
    for (int i = 0; i < n; i++) {
        cout << "node " << i + 1 << "\n";
        for (auto [node, dist] : best[i]) {
            cout << node + 1 << " " << dist << "\n";
        }
    }
    */
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 6 ms 1108 KB Output is correct
6 Correct 6 ms 860 KB Output is correct
7 Correct 5 ms 860 KB Output is correct
8 Correct 8 ms 1372 KB Output is correct
9 Correct 8 ms 1372 KB Output is correct
10 Correct 7 ms 1380 KB Output is correct
11 Correct 8 ms 1372 KB Output is correct
12 Correct 9 ms 1308 KB Output is correct
13 Correct 9 ms 1392 KB Output is correct
14 Correct 6 ms 1116 KB Output is correct
15 Correct 9 ms 844 KB Output is correct
16 Correct 7 ms 1116 KB Output is correct
17 Correct 7 ms 1116 KB Output is correct
18 Correct 9 ms 1116 KB Output is correct
19 Correct 7 ms 1112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 6 ms 1108 KB Output is correct
6 Correct 6 ms 860 KB Output is correct
7 Correct 5 ms 860 KB Output is correct
8 Correct 8 ms 1372 KB Output is correct
9 Correct 8 ms 1372 KB Output is correct
10 Correct 7 ms 1380 KB Output is correct
11 Correct 8 ms 1372 KB Output is correct
12 Correct 9 ms 1308 KB Output is correct
13 Correct 9 ms 1392 KB Output is correct
14 Correct 6 ms 1116 KB Output is correct
15 Correct 9 ms 844 KB Output is correct
16 Correct 7 ms 1116 KB Output is correct
17 Correct 7 ms 1116 KB Output is correct
18 Correct 9 ms 1116 KB Output is correct
19 Correct 7 ms 1112 KB Output is correct
20 Correct 724 ms 2752 KB Output is correct
21 Correct 736 ms 2900 KB Output is correct
22 Correct 712 ms 2728 KB Output is correct
23 Correct 846 ms 2896 KB Output is correct
24 Correct 1148 ms 89680 KB Output is correct
25 Correct 1292 ms 90448 KB Output is correct
26 Correct 1178 ms 90448 KB Output is correct
27 Correct 871 ms 113956 KB Output is correct
28 Correct 907 ms 114028 KB Output is correct
29 Correct 879 ms 114000 KB Output is correct
30 Correct 954 ms 113420 KB Output is correct
31 Correct 1346 ms 111496 KB Output is correct
32 Correct 907 ms 113484 KB Output is correct
33 Correct 745 ms 77140 KB Output is correct
34 Correct 1181 ms 82608 KB Output is correct
35 Correct 715 ms 76880 KB Output is correct
36 Correct 829 ms 95512 KB Output is correct
37 Correct 1299 ms 91012 KB Output is correct
38 Correct 778 ms 95840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 6 ms 1108 KB Output is correct
6 Correct 6 ms 860 KB Output is correct
7 Correct 5 ms 860 KB Output is correct
8 Correct 8 ms 1372 KB Output is correct
9 Correct 8 ms 1372 KB Output is correct
10 Correct 7 ms 1380 KB Output is correct
11 Correct 8 ms 1372 KB Output is correct
12 Correct 9 ms 1308 KB Output is correct
13 Correct 9 ms 1392 KB Output is correct
14 Correct 6 ms 1116 KB Output is correct
15 Correct 9 ms 844 KB Output is correct
16 Correct 7 ms 1116 KB Output is correct
17 Correct 7 ms 1116 KB Output is correct
18 Correct 9 ms 1116 KB Output is correct
19 Correct 7 ms 1112 KB Output is correct
20 Correct 724 ms 2752 KB Output is correct
21 Correct 736 ms 2900 KB Output is correct
22 Correct 712 ms 2728 KB Output is correct
23 Correct 846 ms 2896 KB Output is correct
24 Correct 1148 ms 89680 KB Output is correct
25 Correct 1292 ms 90448 KB Output is correct
26 Correct 1178 ms 90448 KB Output is correct
27 Correct 871 ms 113956 KB Output is correct
28 Correct 907 ms 114028 KB Output is correct
29 Correct 879 ms 114000 KB Output is correct
30 Correct 954 ms 113420 KB Output is correct
31 Correct 1346 ms 111496 KB Output is correct
32 Correct 907 ms 113484 KB Output is correct
33 Correct 745 ms 77140 KB Output is correct
34 Correct 1181 ms 82608 KB Output is correct
35 Correct 715 ms 76880 KB Output is correct
36 Correct 829 ms 95512 KB Output is correct
37 Correct 1299 ms 91012 KB Output is correct
38 Correct 778 ms 95840 KB Output is correct
39 Correct 1216 ms 89348 KB Output is correct
40 Correct 1150 ms 89352 KB Output is correct
41 Execution timed out 2064 ms 90468 KB Time limit exceeded
42 Halted 0 ms 0 KB -