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>
using namespace std;
int main() {
const int B = 400;
ios_base::sync_with_stdio(0);
cin.tie(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[v].push_back(u);
// REVERSED EDGES
}
vector<vector<pair<int, int>>> far(n); // Store furthest B cities, pair of vertex and distance (sorted by distance)
vector<int> done(n);
auto merge = [&](vector<pair<int, int>>& a, vector<pair<int, int>>& b) {
vector<pair<int, int>> res;
int ia = 0;
int ib = 0;
int sa = (int) a.size();
int sb = (int) b.size();
for (auto x : a)
done[x.second] = 0;
for (auto x : b)
done[x.second] = 0;
while (ia < sa || ib < sb) {
if (ia == sa) {
if (!done[b[ib].second])
res.emplace_back(b[ib].first + 1, b[ib].second);
done[b[ib].second] = 1;
ib++;
} else if (ib == sb) {
if (!done[a[ia].second])
res.push_back(a[ia]);
done[a[ia].second] = 1;
ia++;
} else if(a[ia].first > b[ib].first + 1) {
if (!done[a[ia].second])
res.push_back(a[ia]);
done[a[ia].second] = 1;
ia++;
} else {
if (!done[b[ib].second])
res.emplace_back(b[ib].first + 1, b[ib].second);
done[b[ib].second] = 1;
ib++;
}
}
a = res;
while ((int) a.size() > B)
a.pop_back();
};
for (int i = 0; i < n; i++) {
auto& cur = far[i];
cur = {{0, i}};
for (int j : adj[i]) {
merge(cur, far[j]);
}
}
vector<int> big(n);
auto compute_dist = [&](int src) -> int {
vector<int> dist(n, -1);
dist[src] = 0;
for (int i = src; i >= 0; i--) {
if (dist[i] == -1)
continue;
for (int j : adj[i]) {
dist[j] = max(dist[j], dist[i] + 1);
}
}
int mx = -1;
for (int i = 0; i < n; i++) {
if (big[i])
continue;
mx = max(mx, dist[i]);
}
return mx;
};
while (q--) {
int t, y;
cin >> t >> y;
t--;
vector<int> bad(y);
for (int i = 0; i < y; i++)
cin >> bad[i], bad[i]--;
for (int x : bad)
big[x] = 1;
if (y >= B) {
int ans = compute_dist(t);
cout << ans << '\n';
} else {
// We store the sqrt furthest paths away from every vertex
// To do that we can
int ans = -1;
for (auto x : far[t]) {
if (big[x.second])
continue;
ans = max(ans, x.first);
}
cout << ans << '\n';
}
for (int x : bad)
big[x] = 0;
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |