#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 = 45;
int main() {
cin.tie(0) -> sync_with_stdio(0);
int n, m, q;
cin >> n >> m >> q;
vector<vector<int>> adj(n);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
--u, --v;
adj[u].push_back(v);
}
vector<vector<pii>> best(n);
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); }
};
for (int u = 0; u < n; u++) {
if (sz(best[u]) < B) {
best[u].push_back({u, 0});
}
for (int v : adj[u]) {
merge(u, 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 {
vector<int> opt(n, -1);
for (int u = 0; u <= t; u++) {
if (!bad.count(u)) { opt[u] = max(0, opt[u]); }
for (int v : adj[u]) {
if (opt[u] != -1) { opt[v] = max(opt[v], opt[u] + 1); }
}
}
return opt[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 |
0 ms |
344 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 |
860 KB |
Output is correct |
6 |
Correct |
6 ms |
792 KB |
Output is correct |
7 |
Correct |
5 ms |
860 KB |
Output is correct |
8 |
Correct |
6 ms |
1004 KB |
Output is correct |
9 |
Correct |
5 ms |
860 KB |
Output is correct |
10 |
Correct |
5 ms |
996 KB |
Output is correct |
11 |
Correct |
5 ms |
860 KB |
Output is correct |
12 |
Correct |
8 ms |
860 KB |
Output is correct |
13 |
Correct |
6 ms |
860 KB |
Output is correct |
14 |
Correct |
5 ms |
860 KB |
Output is correct |
15 |
Correct |
7 ms |
860 KB |
Output is correct |
16 |
Correct |
6 ms |
860 KB |
Output is correct |
17 |
Correct |
6 ms |
912 KB |
Output is correct |
18 |
Correct |
8 ms |
860 KB |
Output is correct |
19 |
Correct |
6 ms |
876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
860 KB |
Output is correct |
6 |
Correct |
6 ms |
792 KB |
Output is correct |
7 |
Correct |
5 ms |
860 KB |
Output is correct |
8 |
Correct |
6 ms |
1004 KB |
Output is correct |
9 |
Correct |
5 ms |
860 KB |
Output is correct |
10 |
Correct |
5 ms |
996 KB |
Output is correct |
11 |
Correct |
5 ms |
860 KB |
Output is correct |
12 |
Correct |
8 ms |
860 KB |
Output is correct |
13 |
Correct |
6 ms |
860 KB |
Output is correct |
14 |
Correct |
5 ms |
860 KB |
Output is correct |
15 |
Correct |
7 ms |
860 KB |
Output is correct |
16 |
Correct |
6 ms |
860 KB |
Output is correct |
17 |
Correct |
6 ms |
912 KB |
Output is correct |
18 |
Correct |
8 ms |
860 KB |
Output is correct |
19 |
Correct |
6 ms |
876 KB |
Output is correct |
20 |
Correct |
468 ms |
3168 KB |
Output is correct |
21 |
Correct |
480 ms |
3240 KB |
Output is correct |
22 |
Correct |
473 ms |
2208 KB |
Output is correct |
23 |
Correct |
534 ms |
2164 KB |
Output is correct |
24 |
Correct |
887 ms |
52564 KB |
Output is correct |
25 |
Correct |
940 ms |
51796 KB |
Output is correct |
26 |
Correct |
932 ms |
52328 KB |
Output is correct |
27 |
Correct |
568 ms |
62804 KB |
Output is correct |
28 |
Correct |
569 ms |
62804 KB |
Output is correct |
29 |
Correct |
572 ms |
62720 KB |
Output is correct |
30 |
Correct |
652 ms |
62032 KB |
Output is correct |
31 |
Correct |
908 ms |
64848 KB |
Output is correct |
32 |
Correct |
616 ms |
62036 KB |
Output is correct |
33 |
Correct |
562 ms |
46488 KB |
Output is correct |
34 |
Correct |
995 ms |
54864 KB |
Output is correct |
35 |
Correct |
557 ms |
47948 KB |
Output is correct |
36 |
Correct |
596 ms |
55120 KB |
Output is correct |
37 |
Correct |
1016 ms |
58108 KB |
Output is correct |
38 |
Correct |
574 ms |
55180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
860 KB |
Output is correct |
6 |
Correct |
6 ms |
792 KB |
Output is correct |
7 |
Correct |
5 ms |
860 KB |
Output is correct |
8 |
Correct |
6 ms |
1004 KB |
Output is correct |
9 |
Correct |
5 ms |
860 KB |
Output is correct |
10 |
Correct |
5 ms |
996 KB |
Output is correct |
11 |
Correct |
5 ms |
860 KB |
Output is correct |
12 |
Correct |
8 ms |
860 KB |
Output is correct |
13 |
Correct |
6 ms |
860 KB |
Output is correct |
14 |
Correct |
5 ms |
860 KB |
Output is correct |
15 |
Correct |
7 ms |
860 KB |
Output is correct |
16 |
Correct |
6 ms |
860 KB |
Output is correct |
17 |
Correct |
6 ms |
912 KB |
Output is correct |
18 |
Correct |
8 ms |
860 KB |
Output is correct |
19 |
Correct |
6 ms |
876 KB |
Output is correct |
20 |
Correct |
468 ms |
3168 KB |
Output is correct |
21 |
Correct |
480 ms |
3240 KB |
Output is correct |
22 |
Correct |
473 ms |
2208 KB |
Output is correct |
23 |
Correct |
534 ms |
2164 KB |
Output is correct |
24 |
Correct |
887 ms |
52564 KB |
Output is correct |
25 |
Correct |
940 ms |
51796 KB |
Output is correct |
26 |
Correct |
932 ms |
52328 KB |
Output is correct |
27 |
Correct |
568 ms |
62804 KB |
Output is correct |
28 |
Correct |
569 ms |
62804 KB |
Output is correct |
29 |
Correct |
572 ms |
62720 KB |
Output is correct |
30 |
Correct |
652 ms |
62032 KB |
Output is correct |
31 |
Correct |
908 ms |
64848 KB |
Output is correct |
32 |
Correct |
616 ms |
62036 KB |
Output is correct |
33 |
Correct |
562 ms |
46488 KB |
Output is correct |
34 |
Correct |
995 ms |
54864 KB |
Output is correct |
35 |
Correct |
557 ms |
47948 KB |
Output is correct |
36 |
Correct |
596 ms |
55120 KB |
Output is correct |
37 |
Correct |
1016 ms |
58108 KB |
Output is correct |
38 |
Correct |
574 ms |
55180 KB |
Output is correct |
39 |
Correct |
928 ms |
52048 KB |
Output is correct |
40 |
Correct |
822 ms |
50768 KB |
Output is correct |
41 |
Correct |
1622 ms |
51128 KB |
Output is correct |
42 |
Correct |
1037 ms |
52788 KB |
Output is correct |
43 |
Correct |
905 ms |
52680 KB |
Output is correct |
44 |
Correct |
494 ms |
4944 KB |
Output is correct |
45 |
Correct |
492 ms |
4104 KB |
Output is correct |
46 |
Correct |
641 ms |
4120 KB |
Output is correct |
47 |
Correct |
480 ms |
3924 KB |
Output is correct |
48 |
Correct |
493 ms |
3736 KB |
Output is correct |
49 |
Correct |
619 ms |
63828 KB |
Output is correct |
50 |
Correct |
1547 ms |
63188 KB |
Output is correct |
51 |
Correct |
499 ms |
4828 KB |
Output is correct |
52 |
Correct |
623 ms |
3864 KB |
Output is correct |
53 |
Correct |
620 ms |
54356 KB |
Output is correct |
54 |
Correct |
980 ms |
56296 KB |
Output is correct |
55 |
Correct |
1548 ms |
53680 KB |
Output is correct |
56 |
Correct |
1795 ms |
54732 KB |
Output is correct |
57 |
Correct |
485 ms |
4792 KB |
Output is correct |
58 |
Correct |
485 ms |
4692 KB |
Output is correct |
59 |
Correct |
595 ms |
3924 KB |
Output is correct |
60 |
Correct |
592 ms |
3920 KB |
Output is correct |
61 |
Correct |
725 ms |
63224 KB |
Output is correct |
62 |
Correct |
698 ms |
53964 KB |
Output is correct |
63 |
Correct |
1063 ms |
55836 KB |
Output is correct |
64 |
Correct |
912 ms |
63196 KB |
Output is correct |
65 |
Correct |
937 ms |
53640 KB |
Output is correct |
66 |
Correct |
1340 ms |
55072 KB |
Output is correct |
67 |
Correct |
1055 ms |
63176 KB |
Output is correct |
68 |
Correct |
1097 ms |
53960 KB |
Output is correct |
69 |
Correct |
1474 ms |
55876 KB |
Output is correct |
70 |
Correct |
666 ms |
63596 KB |
Output is correct |
71 |
Correct |
618 ms |
54472 KB |
Output is correct |
72 |
Correct |
1018 ms |
55732 KB |
Output is correct |
73 |
Correct |
619 ms |
63432 KB |
Output is correct |
74 |
Correct |
652 ms |
54168 KB |
Output is correct |
75 |
Correct |
1028 ms |
58044 KB |
Output is correct |
76 |
Correct |
570 ms |
64336 KB |
Output is correct |
77 |
Correct |
1217 ms |
63416 KB |
Output is correct |
78 |
Correct |
567 ms |
64300 KB |
Output is correct |
79 |
Correct |
483 ms |
4948 KB |
Output is correct |
80 |
Correct |
572 ms |
4132 KB |
Output is correct |
81 |
Correct |
469 ms |
3924 KB |
Output is correct |
82 |
Correct |
637 ms |
63572 KB |
Output is correct |
83 |
Correct |
1031 ms |
67404 KB |
Output is correct |
84 |
Correct |
1244 ms |
62644 KB |
Output is correct |
85 |
Correct |
1775 ms |
68628 KB |
Output is correct |
86 |
Correct |
632 ms |
63480 KB |
Output is correct |
87 |
Correct |
1000 ms |
66764 KB |
Output is correct |
88 |
Correct |
505 ms |
4948 KB |
Output is correct |
89 |
Correct |
493 ms |
4952 KB |
Output is correct |
90 |
Correct |
599 ms |
3920 KB |
Output is correct |
91 |
Correct |
598 ms |
4028 KB |
Output is correct |
92 |
Correct |
481 ms |
3664 KB |
Output is correct |
93 |
Correct |
490 ms |
3668 KB |
Output is correct |
94 |
Correct |
585 ms |
48208 KB |
Output is correct |
95 |
Correct |
1030 ms |
58540 KB |
Output is correct |
96 |
Correct |
1218 ms |
47244 KB |
Output is correct |
97 |
Correct |
1617 ms |
57800 KB |
Output is correct |
98 |
Correct |
574 ms |
48072 KB |
Output is correct |
99 |
Correct |
996 ms |
57032 KB |
Output is correct |
100 |
Correct |
542 ms |
4948 KB |
Output is correct |
101 |
Correct |
564 ms |
4944 KB |
Output is correct |
102 |
Correct |
616 ms |
3928 KB |
Output is correct |
103 |
Correct |
668 ms |
4056 KB |
Output is correct |
104 |
Correct |
539 ms |
3924 KB |
Output is correct |
105 |
Correct |
532 ms |
3724 KB |
Output is correct |
106 |
Correct |
612 ms |
54616 KB |
Output is correct |
107 |
Correct |
920 ms |
55884 KB |
Output is correct |
108 |
Correct |
1267 ms |
54148 KB |
Output is correct |
109 |
Correct |
1512 ms |
55712 KB |
Output is correct |
110 |
Correct |
593 ms |
54984 KB |
Output is correct |
111 |
Correct |
965 ms |
56060 KB |
Output is correct |
112 |
Correct |
490 ms |
4884 KB |
Output is correct |
113 |
Correct |
517 ms |
5072 KB |
Output is correct |
114 |
Correct |
574 ms |
3924 KB |
Output is correct |
115 |
Correct |
568 ms |
3924 KB |
Output is correct |
116 |
Correct |
459 ms |
3744 KB |
Output is correct |
117 |
Correct |
468 ms |
3668 KB |
Output is correct |
118 |
Correct |
560 ms |
62804 KB |
Output is correct |
119 |
Correct |
623 ms |
53592 KB |
Output is correct |
120 |
Correct |
1025 ms |
57692 KB |
Output is correct |
121 |
Correct |
589 ms |
62736 KB |
Output is correct |
122 |
Correct |
593 ms |
53328 KB |
Output is correct |
123 |
Correct |
939 ms |
55484 KB |
Output is correct |
124 |
Correct |
1901 ms |
63192 KB |
Output is correct |
125 |
Correct |
1943 ms |
53956 KB |
Output is correct |
126 |
Execution timed out |
2037 ms |
56408 KB |
Time limit exceeded |
127 |
Halted |
0 ms |
0 KB |
- |