//start-time: 2024-04-14 19:39:32
#include <vector>
#include <set>
#include <iostream>
#include <algorithm>
#include <array>
using namespace std;
using ll = long long;
const int w = 330;
void solve(){
int n, m, q;
cin >> n >> m >> q;
vector<vector<int>> gR(n);
for(int i = 0; i < m; i++){
int u, v;
cin >> u >> v;
u--; v--;
gR[v].push_back(u);
}
vector<vector<array<int, 2>>> dp(n);
for(int u = 0; u < n; u++){
vector<int> val(n, -1);
set<array<int, 2>> aux;
aux.insert({0, u});
val[u] = 0;
for(auto v : gR[u]){
for(auto [value, ind] : dp[v]){
val[ind] = max(val[ind], value + 1);
}
}
for(auto v : gR[u]){
for(auto [value, ind] : dp[v]){
aux.insert({val[ind], ind});
if(aux.size() > w){
aux.erase(aux.begin());
}
}
}
dp[u] = vector<array<int, 2>>(aux.begin(), aux.end());
}
for(int i = 0; i < q; i++){
int u, y;
cin >> u >> y;
u--;
vector<bool> c(n);
for(int j = 0; j < y; j++){
int a; cin >> a;
c[--a] = 1;
}
int ans = -1;
if(y < w) {
for(auto [v, i] : dp[u]){
if(!c[i]){
ans = max(ans, v);
}
}
}
else {
vector<int> naive(n);
for(int v = 0; v <= u; v++){
naive[v] = c[v] ? -1 : 0;
for(auto el : gR[v]){
naive[v] = max(naive[v], naive[el] + (naive[el] != -1));
}
}
ans = naive[u];
}
cout << ans << endl;
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
ll T = 1;
//cin >> T;
while(T--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 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 |
772 KB |
Output is correct |
6 |
Correct |
7 ms |
860 KB |
Output is correct |
7 |
Correct |
7 ms |
604 KB |
Output is correct |
8 |
Correct |
37 ms |
2580 KB |
Output is correct |
9 |
Correct |
36 ms |
2620 KB |
Output is correct |
10 |
Correct |
33 ms |
2532 KB |
Output is correct |
11 |
Correct |
34 ms |
2388 KB |
Output is correct |
12 |
Correct |
18 ms |
1368 KB |
Output is correct |
13 |
Correct |
33 ms |
2160 KB |
Output is correct |
14 |
Correct |
29 ms |
1628 KB |
Output is correct |
15 |
Correct |
19 ms |
1372 KB |
Output is correct |
16 |
Correct |
27 ms |
1652 KB |
Output is correct |
17 |
Correct |
30 ms |
1792 KB |
Output is correct |
18 |
Correct |
17 ms |
1116 KB |
Output is correct |
19 |
Correct |
30 ms |
1872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 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 |
772 KB |
Output is correct |
6 |
Correct |
7 ms |
860 KB |
Output is correct |
7 |
Correct |
7 ms |
604 KB |
Output is correct |
8 |
Correct |
37 ms |
2580 KB |
Output is correct |
9 |
Correct |
36 ms |
2620 KB |
Output is correct |
10 |
Correct |
33 ms |
2532 KB |
Output is correct |
11 |
Correct |
34 ms |
2388 KB |
Output is correct |
12 |
Correct |
18 ms |
1368 KB |
Output is correct |
13 |
Correct |
33 ms |
2160 KB |
Output is correct |
14 |
Correct |
29 ms |
1628 KB |
Output is correct |
15 |
Correct |
19 ms |
1372 KB |
Output is correct |
16 |
Correct |
27 ms |
1652 KB |
Output is correct |
17 |
Correct |
30 ms |
1792 KB |
Output is correct |
18 |
Correct |
17 ms |
1116 KB |
Output is correct |
19 |
Correct |
30 ms |
1872 KB |
Output is correct |
20 |
Correct |
1681 ms |
3964 KB |
Output is correct |
21 |
Correct |
1571 ms |
3752 KB |
Output is correct |
22 |
Correct |
1657 ms |
3824 KB |
Output is correct |
23 |
Execution timed out |
2013 ms |
3780 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 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 |
772 KB |
Output is correct |
6 |
Correct |
7 ms |
860 KB |
Output is correct |
7 |
Correct |
7 ms |
604 KB |
Output is correct |
8 |
Correct |
37 ms |
2580 KB |
Output is correct |
9 |
Correct |
36 ms |
2620 KB |
Output is correct |
10 |
Correct |
33 ms |
2532 KB |
Output is correct |
11 |
Correct |
34 ms |
2388 KB |
Output is correct |
12 |
Correct |
18 ms |
1368 KB |
Output is correct |
13 |
Correct |
33 ms |
2160 KB |
Output is correct |
14 |
Correct |
29 ms |
1628 KB |
Output is correct |
15 |
Correct |
19 ms |
1372 KB |
Output is correct |
16 |
Correct |
27 ms |
1652 KB |
Output is correct |
17 |
Correct |
30 ms |
1792 KB |
Output is correct |
18 |
Correct |
17 ms |
1116 KB |
Output is correct |
19 |
Correct |
30 ms |
1872 KB |
Output is correct |
20 |
Correct |
1681 ms |
3964 KB |
Output is correct |
21 |
Correct |
1571 ms |
3752 KB |
Output is correct |
22 |
Correct |
1657 ms |
3824 KB |
Output is correct |
23 |
Execution timed out |
2013 ms |
3780 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |