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 <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 1e5 + 1;
const int sqn = 320;
vector <int> adjr[maxn];
vector <int> dis, curr, vis, dp, blocked;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m, Q;
cin >> n >> m >> Q;
blocked = dis = vis = dp = vector <int> (n+1);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
adjr[b].push_back(a);
}
vector <vector <pair <int, int>>> longest_paths(n+1);
for (int i = 1; i <= n; i++) {
longest_paths[i].push_back({0, i});
curr.clear();
for (auto j : adjr[i]) {
for (auto x : longest_paths[j]) {
if (vis[x.second] == i)
dis[x.second] = max(dis[x.second], x.first + 1);
else {
dis[x.second] = x.first + 1;
vis[x.second] = i;
curr.push_back(x.second);
}
}
}
for (auto j : curr)
longest_paths[i].push_back({dis[j], j});
sort(longest_paths[i].begin(), longest_paths[i].end(), greater <pair <int, int>> ());
if (longest_paths[i].size() > sqn)
longest_paths[i].erase(longest_paths[i].begin() + sqn, longest_paths[i].end());
}
for (int q = 1; q <= Q; q++) {
int t, y;
cin >> t >> y;
for (int i = 0; i < y; i++) {
int x;
cin >> x;
blocked[x] = q;
}
if (y < sqn) {
int ans = -1;
for (auto i : longest_paths[t]) {
if (blocked[i.second] != q) {
ans = i.first;
break;
}
}
cout << ans << '\n';
} else {
fill(dp.begin(), dp.end(), -1);
for (int i = 1; i <= n; i++) {
if (blocked[i] != q)
dp[i] = 0;
for (auto j : adjr[i]) {
if (dp[j] != -1)
dp[i] = max(dp[i], dp[j] + 1);
}
}
cout << dp[t] << '\n';
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |