This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 1e5 + 2;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m, q;
cin >> n >> m >> q;
vector<vector<int>> g(n);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u -= 1; v -= 1;
g[v].push_back(u);
}
const int M = 350;
vector<vector<pair<int, int>>> di(n);
vector<int> lst(n, -1), dist(n);
for (int i = 0; i < n; i++) {
vector<int> v;
for (int j : g[i]) {
for (auto [u, d] : di[j]) {
if (lst[u] != i) {
dist[u] = d + 1;
lst[u] = i;
v.push_back(u);
}
else smax(dist[u], d + 1);
}
}
v.push_back(i);
sort(v.begin(), v.end(), [&] (int x, int y) {
return dist[x] > dist[y];
});
for (int j = 0; j < min((int) v.size(), M); j++) di[i].push_back({v[j], dist[v[j]]});
}
vector<bool> busy(n);
while (q--) {
int c; cin >> c; c--;
int sz; cin >> sz;
vector<int> b(sz);
for (int i = 0; i < sz; i++) {
cin >> b[i]; b[i] -= 1;
busy[b[i]] = 1;
}
if (sz >= M) {
vector<int> dp(n, -1e9);
for (int i = 0; i < n; i++) {
if (!busy[i]) dp[i] = 0;
for (int j : g[i]) {
if (dp[j] != -1e9) smax(dp[i], dp[j] + 1);
}
}
if (dp[c] == -1e9) dp[c] = -1;
cout << dp[c] << en;
}
else {
int ans = -1e9;
for (auto [i, j] : di[c]) {
if (!busy[i]) smax(ans, j);
}
if (ans == -1e9) ans = -1;
cout << ans << en;
}
for (int i : b) busy[i] = 0;
}
return 0;
}
Compilation message (stderr)
bitaro.cpp: In function 'int main()':
bitaro.cpp:29:23: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
29 | for (auto [u, d] : di[j]) {
| ^
bitaro.cpp:66:23: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
66 | for (auto [i, j] : di[c]) {
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |