#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<int> mp(n, -1);
vector<vector<pii>> best(n);
const auto merge = [&](int from, int to) -> void {
vector<int> seen;
for (auto [node, dist] : best[from]) {
mp[node] = max(mp[node], dist + 1);
seen.push_back(node);
}
for (auto [node, dist] : best[to]) {
mp[node] = max(mp[node], dist);
seen.push_back(node);
}
best[to].clear();
for (int x : seen) {
if (mp[x] == -1) { continue; }
best[to].push_back({x, mp[x]});
mp[x] = -1;
}
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 |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
456 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
7 |
Correct |
2 ms |
860 KB |
Output is correct |
8 |
Correct |
3 ms |
860 KB |
Output is correct |
9 |
Correct |
2 ms |
860 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
2 ms |
856 KB |
Output is correct |
13 |
Correct |
3 ms |
856 KB |
Output is correct |
14 |
Correct |
2 ms |
860 KB |
Output is correct |
15 |
Correct |
2 ms |
880 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
2 ms |
860 KB |
Output is correct |
18 |
Correct |
4 ms |
860 KB |
Output is correct |
19 |
Correct |
2 ms |
832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
456 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
7 |
Correct |
2 ms |
860 KB |
Output is correct |
8 |
Correct |
3 ms |
860 KB |
Output is correct |
9 |
Correct |
2 ms |
860 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
2 ms |
856 KB |
Output is correct |
13 |
Correct |
3 ms |
856 KB |
Output is correct |
14 |
Correct |
2 ms |
860 KB |
Output is correct |
15 |
Correct |
2 ms |
880 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
2 ms |
860 KB |
Output is correct |
18 |
Correct |
4 ms |
860 KB |
Output is correct |
19 |
Correct |
2 ms |
832 KB |
Output is correct |
20 |
Correct |
160 ms |
3664 KB |
Output is correct |
21 |
Correct |
156 ms |
3676 KB |
Output is correct |
22 |
Correct |
160 ms |
3708 KB |
Output is correct |
23 |
Correct |
159 ms |
3704 KB |
Output is correct |
24 |
Correct |
267 ms |
55640 KB |
Output is correct |
25 |
Correct |
273 ms |
54700 KB |
Output is correct |
26 |
Correct |
280 ms |
55236 KB |
Output is correct |
27 |
Correct |
233 ms |
65732 KB |
Output is correct |
28 |
Correct |
248 ms |
65712 KB |
Output is correct |
29 |
Correct |
237 ms |
65736 KB |
Output is correct |
30 |
Correct |
237 ms |
65108 KB |
Output is correct |
31 |
Correct |
272 ms |
67664 KB |
Output is correct |
32 |
Correct |
234 ms |
65104 KB |
Output is correct |
33 |
Correct |
196 ms |
49320 KB |
Output is correct |
34 |
Correct |
250 ms |
57424 KB |
Output is correct |
35 |
Correct |
198 ms |
49488 KB |
Output is correct |
36 |
Correct |
219 ms |
56464 KB |
Output is correct |
37 |
Correct |
256 ms |
59132 KB |
Output is correct |
38 |
Correct |
222 ms |
56404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
456 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
7 |
Correct |
2 ms |
860 KB |
Output is correct |
8 |
Correct |
3 ms |
860 KB |
Output is correct |
9 |
Correct |
2 ms |
860 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
2 ms |
856 KB |
Output is correct |
13 |
Correct |
3 ms |
856 KB |
Output is correct |
14 |
Correct |
2 ms |
860 KB |
Output is correct |
15 |
Correct |
2 ms |
880 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
2 ms |
860 KB |
Output is correct |
18 |
Correct |
4 ms |
860 KB |
Output is correct |
19 |
Correct |
2 ms |
832 KB |
Output is correct |
20 |
Correct |
160 ms |
3664 KB |
Output is correct |
21 |
Correct |
156 ms |
3676 KB |
Output is correct |
22 |
Correct |
160 ms |
3708 KB |
Output is correct |
23 |
Correct |
159 ms |
3704 KB |
Output is correct |
24 |
Correct |
267 ms |
55640 KB |
Output is correct |
25 |
Correct |
273 ms |
54700 KB |
Output is correct |
26 |
Correct |
280 ms |
55236 KB |
Output is correct |
27 |
Correct |
233 ms |
65732 KB |
Output is correct |
28 |
Correct |
248 ms |
65712 KB |
Output is correct |
29 |
Correct |
237 ms |
65736 KB |
Output is correct |
30 |
Correct |
237 ms |
65108 KB |
Output is correct |
31 |
Correct |
272 ms |
67664 KB |
Output is correct |
32 |
Correct |
234 ms |
65104 KB |
Output is correct |
33 |
Correct |
196 ms |
49320 KB |
Output is correct |
34 |
Correct |
250 ms |
57424 KB |
Output is correct |
35 |
Correct |
198 ms |
49488 KB |
Output is correct |
36 |
Correct |
219 ms |
56464 KB |
Output is correct |
37 |
Correct |
256 ms |
59132 KB |
Output is correct |
38 |
Correct |
222 ms |
56404 KB |
Output is correct |
39 |
Correct |
288 ms |
53844 KB |
Output is correct |
40 |
Correct |
250 ms |
51948 KB |
Output is correct |
41 |
Correct |
1131 ms |
52428 KB |
Output is correct |
42 |
Correct |
393 ms |
53188 KB |
Output is correct |
43 |
Correct |
302 ms |
53216 KB |
Output is correct |
44 |
Correct |
184 ms |
4944 KB |
Output is correct |
45 |
Correct |
189 ms |
3980 KB |
Output is correct |
46 |
Correct |
295 ms |
4124 KB |
Output is correct |
47 |
Correct |
204 ms |
3848 KB |
Output is correct |
48 |
Correct |
166 ms |
3736 KB |
Output is correct |
49 |
Correct |
270 ms |
64336 KB |
Output is correct |
50 |
Correct |
1197 ms |
63688 KB |
Output is correct |
51 |
Correct |
179 ms |
4692 KB |
Output is correct |
52 |
Correct |
315 ms |
4180 KB |
Output is correct |
53 |
Correct |
247 ms |
54688 KB |
Output is correct |
54 |
Correct |
288 ms |
56492 KB |
Output is correct |
55 |
Correct |
1179 ms |
54152 KB |
Output is correct |
56 |
Correct |
1163 ms |
55016 KB |
Output is correct |
57 |
Correct |
177 ms |
4688 KB |
Output is correct |
58 |
Correct |
173 ms |
4776 KB |
Output is correct |
59 |
Correct |
296 ms |
3860 KB |
Output is correct |
60 |
Correct |
298 ms |
3920 KB |
Output is correct |
61 |
Correct |
374 ms |
63684 KB |
Output is correct |
62 |
Correct |
347 ms |
54220 KB |
Output is correct |
63 |
Correct |
370 ms |
56020 KB |
Output is correct |
64 |
Correct |
553 ms |
63432 KB |
Output is correct |
65 |
Correct |
555 ms |
54104 KB |
Output is correct |
66 |
Correct |
577 ms |
54988 KB |
Output is correct |
67 |
Correct |
762 ms |
63436 KB |
Output is correct |
68 |
Correct |
748 ms |
54272 KB |
Output is correct |
69 |
Correct |
767 ms |
55900 KB |
Output is correct |
70 |
Correct |
287 ms |
64216 KB |
Output is correct |
71 |
Correct |
291 ms |
54864 KB |
Output is correct |
72 |
Correct |
300 ms |
55756 KB |
Output is correct |
73 |
Correct |
297 ms |
63736 KB |
Output is correct |
74 |
Correct |
272 ms |
54476 KB |
Output is correct |
75 |
Correct |
304 ms |
57512 KB |
Output is correct |
76 |
Correct |
259 ms |
64596 KB |
Output is correct |
77 |
Correct |
865 ms |
63688 KB |
Output is correct |
78 |
Correct |
249 ms |
64456 KB |
Output is correct |
79 |
Correct |
177 ms |
4948 KB |
Output is correct |
80 |
Correct |
264 ms |
4108 KB |
Output is correct |
81 |
Correct |
159 ms |
3672 KB |
Output is correct |
82 |
Correct |
261 ms |
64084 KB |
Output is correct |
83 |
Correct |
280 ms |
67412 KB |
Output is correct |
84 |
Correct |
871 ms |
63320 KB |
Output is correct |
85 |
Correct |
952 ms |
69060 KB |
Output is correct |
86 |
Correct |
247 ms |
63912 KB |
Output is correct |
87 |
Correct |
293 ms |
66564 KB |
Output is correct |
88 |
Correct |
179 ms |
4948 KB |
Output is correct |
89 |
Correct |
184 ms |
5204 KB |
Output is correct |
90 |
Correct |
265 ms |
3920 KB |
Output is correct |
91 |
Correct |
271 ms |
3924 KB |
Output is correct |
92 |
Correct |
166 ms |
3672 KB |
Output is correct |
93 |
Correct |
157 ms |
3676 KB |
Output is correct |
94 |
Correct |
217 ms |
48464 KB |
Output is correct |
95 |
Correct |
265 ms |
59004 KB |
Output is correct |
96 |
Correct |
843 ms |
47564 KB |
Output is correct |
97 |
Correct |
882 ms |
58316 KB |
Output is correct |
98 |
Correct |
214 ms |
48588 KB |
Output is correct |
99 |
Correct |
272 ms |
57120 KB |
Output is correct |
100 |
Correct |
185 ms |
4956 KB |
Output is correct |
101 |
Correct |
181 ms |
4952 KB |
Output is correct |
102 |
Correct |
258 ms |
3920 KB |
Output is correct |
103 |
Correct |
253 ms |
3920 KB |
Output is correct |
104 |
Correct |
162 ms |
3668 KB |
Output is correct |
105 |
Correct |
161 ms |
3668 KB |
Output is correct |
106 |
Correct |
240 ms |
55124 KB |
Output is correct |
107 |
Correct |
267 ms |
55892 KB |
Output is correct |
108 |
Correct |
888 ms |
54728 KB |
Output is correct |
109 |
Correct |
897 ms |
56120 KB |
Output is correct |
110 |
Correct |
255 ms |
55500 KB |
Output is correct |
111 |
Correct |
273 ms |
56264 KB |
Output is correct |
112 |
Correct |
192 ms |
4944 KB |
Output is correct |
113 |
Correct |
175 ms |
4948 KB |
Output is correct |
114 |
Correct |
268 ms |
3920 KB |
Output is correct |
115 |
Correct |
262 ms |
4108 KB |
Output is correct |
116 |
Correct |
176 ms |
3668 KB |
Output is correct |
117 |
Correct |
158 ms |
3676 KB |
Output is correct |
118 |
Correct |
239 ms |
62996 KB |
Output is correct |
119 |
Correct |
219 ms |
53840 KB |
Output is correct |
120 |
Correct |
267 ms |
57172 KB |
Output is correct |
121 |
Correct |
254 ms |
63056 KB |
Output is correct |
122 |
Correct |
221 ms |
53784 KB |
Output is correct |
123 |
Correct |
259 ms |
55360 KB |
Output is correct |
124 |
Correct |
1592 ms |
63572 KB |
Output is correct |
125 |
Correct |
1568 ms |
54220 KB |
Output is correct |
126 |
Correct |
1586 ms |
56520 KB |
Output is correct |