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> adj[maxn], adjr[maxn];
bool blocked[maxn];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m, q;
cin >> n >> m >> q;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(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});
vector <int> curr, dis(n+1);
vector <bool> vis(n+1);
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;
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 + 1, longest_paths[i].end());
}
while (q--) {
int t, y;
cin >> t >> y;
fill(blocked, blocked + n + 1, false);
for (int i = 0; i < y; i++) {
int x;
cin >> x;
blocked[x] = true;
}
if (y < sqn) {
int ans = -1;
for (auto i : longest_paths[t]) {
if (!blocked[i.second])
ans = max(ans, i.first);
}
cout << ans << '\n';
} else {
vector <int> dp(n+1, -1);
for (int i = 1; i <= n; i++) {
if (!blocked[i])
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... |