#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 = 70;
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 |
7 ms |
860 KB |
Output is correct |
6 |
Correct |
7 ms |
852 KB |
Output is correct |
7 |
Correct |
7 ms |
860 KB |
Output is correct |
8 |
Correct |
8 ms |
1384 KB |
Output is correct |
9 |
Correct |
8 ms |
1372 KB |
Output is correct |
10 |
Correct |
8 ms |
1372 KB |
Output is correct |
11 |
Correct |
9 ms |
1372 KB |
Output is correct |
12 |
Correct |
10 ms |
1116 KB |
Output is correct |
13 |
Correct |
9 ms |
1488 KB |
Output is correct |
14 |
Correct |
8 ms |
1116 KB |
Output is correct |
15 |
Correct |
10 ms |
860 KB |
Output is correct |
16 |
Correct |
9 ms |
1120 KB |
Output is correct |
17 |
Correct |
10 ms |
1232 KB |
Output is correct |
18 |
Correct |
10 ms |
1116 KB |
Output is correct |
19 |
Correct |
9 ms |
1116 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 |
7 ms |
860 KB |
Output is correct |
6 |
Correct |
7 ms |
852 KB |
Output is correct |
7 |
Correct |
7 ms |
860 KB |
Output is correct |
8 |
Correct |
8 ms |
1384 KB |
Output is correct |
9 |
Correct |
8 ms |
1372 KB |
Output is correct |
10 |
Correct |
8 ms |
1372 KB |
Output is correct |
11 |
Correct |
9 ms |
1372 KB |
Output is correct |
12 |
Correct |
10 ms |
1116 KB |
Output is correct |
13 |
Correct |
9 ms |
1488 KB |
Output is correct |
14 |
Correct |
8 ms |
1116 KB |
Output is correct |
15 |
Correct |
10 ms |
860 KB |
Output is correct |
16 |
Correct |
9 ms |
1120 KB |
Output is correct |
17 |
Correct |
10 ms |
1232 KB |
Output is correct |
18 |
Correct |
10 ms |
1116 KB |
Output is correct |
19 |
Correct |
9 ms |
1116 KB |
Output is correct |
20 |
Correct |
800 ms |
4176 KB |
Output is correct |
21 |
Correct |
775 ms |
4216 KB |
Output is correct |
22 |
Correct |
777 ms |
4220 KB |
Output is correct |
23 |
Correct |
867 ms |
4260 KB |
Output is correct |
24 |
Correct |
1496 ms |
90368 KB |
Output is correct |
25 |
Correct |
1531 ms |
91732 KB |
Output is correct |
26 |
Correct |
1517 ms |
91488 KB |
Output is correct |
27 |
Correct |
888 ms |
115536 KB |
Output is correct |
28 |
Correct |
910 ms |
115356 KB |
Output is correct |
29 |
Correct |
926 ms |
115536 KB |
Output is correct |
30 |
Correct |
1012 ms |
114768 KB |
Output is correct |
31 |
Correct |
1545 ms |
112720 KB |
Output is correct |
32 |
Correct |
1016 ms |
114864 KB |
Output is correct |
33 |
Correct |
889 ms |
78216 KB |
Output is correct |
34 |
Correct |
1564 ms |
86952 KB |
Output is correct |
35 |
Correct |
879 ms |
77696 KB |
Output is correct |
36 |
Correct |
914 ms |
96336 KB |
Output is correct |
37 |
Correct |
1654 ms |
91988 KB |
Output is correct |
38 |
Correct |
905 ms |
96592 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 |
7 ms |
860 KB |
Output is correct |
6 |
Correct |
7 ms |
852 KB |
Output is correct |
7 |
Correct |
7 ms |
860 KB |
Output is correct |
8 |
Correct |
8 ms |
1384 KB |
Output is correct |
9 |
Correct |
8 ms |
1372 KB |
Output is correct |
10 |
Correct |
8 ms |
1372 KB |
Output is correct |
11 |
Correct |
9 ms |
1372 KB |
Output is correct |
12 |
Correct |
10 ms |
1116 KB |
Output is correct |
13 |
Correct |
9 ms |
1488 KB |
Output is correct |
14 |
Correct |
8 ms |
1116 KB |
Output is correct |
15 |
Correct |
10 ms |
860 KB |
Output is correct |
16 |
Correct |
9 ms |
1120 KB |
Output is correct |
17 |
Correct |
10 ms |
1232 KB |
Output is correct |
18 |
Correct |
10 ms |
1116 KB |
Output is correct |
19 |
Correct |
9 ms |
1116 KB |
Output is correct |
20 |
Correct |
800 ms |
4176 KB |
Output is correct |
21 |
Correct |
775 ms |
4216 KB |
Output is correct |
22 |
Correct |
777 ms |
4220 KB |
Output is correct |
23 |
Correct |
867 ms |
4260 KB |
Output is correct |
24 |
Correct |
1496 ms |
90368 KB |
Output is correct |
25 |
Correct |
1531 ms |
91732 KB |
Output is correct |
26 |
Correct |
1517 ms |
91488 KB |
Output is correct |
27 |
Correct |
888 ms |
115536 KB |
Output is correct |
28 |
Correct |
910 ms |
115356 KB |
Output is correct |
29 |
Correct |
926 ms |
115536 KB |
Output is correct |
30 |
Correct |
1012 ms |
114768 KB |
Output is correct |
31 |
Correct |
1545 ms |
112720 KB |
Output is correct |
32 |
Correct |
1016 ms |
114864 KB |
Output is correct |
33 |
Correct |
889 ms |
78216 KB |
Output is correct |
34 |
Correct |
1564 ms |
86952 KB |
Output is correct |
35 |
Correct |
879 ms |
77696 KB |
Output is correct |
36 |
Correct |
914 ms |
96336 KB |
Output is correct |
37 |
Correct |
1654 ms |
91988 KB |
Output is correct |
38 |
Correct |
905 ms |
96592 KB |
Output is correct |
39 |
Correct |
1518 ms |
89412 KB |
Output is correct |
40 |
Correct |
1438 ms |
89028 KB |
Output is correct |
41 |
Execution timed out |
2037 ms |
89544 KB |
Time limit exceeded |
42 |
Halted |
0 ms |
0 KB |
- |