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 ll = long long;
int const nmax = 100000;
int dp[1 + nmax], c[1 + nmax];
std::vector<int> g[1 + nmax];
std::vector<std::pair<int, int>> sol[1 + nmax];
bool viz[1 + nmax], b[1 + nmax];
int main() {
std::ios_base::sync_with_stdio(0);
std::cin.tie(0);
int n, m, q;
std::cin >> n >> m >> q;
for(int i = 1;i <= m; i++) {
int u, v;
std::cin >> u >> v;
g[u].push_back(v);
}
for(int i = 1;i <= n; i++)
sol[i].push_back({0, i});
for(int u = 1;u <= n; u++) {
std::sort(sol[u].rbegin(), sol[u].rend());
for(auto p : sol[u])
viz[p.second] = false;
std::vector<std::pair<int, int>> aux;
for(auto v : sol[u]) {
if(aux.size() >= 120)
break;
if(!viz[v.second]) {
aux.push_back(v);
viz[v.second] = 1;
}
}
std::swap(sol[u], aux);
for(int v : g[u]) {
for(auto p : sol[u]) {
if(sol[v].size() < 120)
sol[v].push_back({p.first + 1, p.second});
}
}
}
while(q--) {
int to, y;
std::cin >> to >> y;
for(int i = 1;i <= y; i++) {
int v;
std::cin >> v;
c[i] = v;
b[v] = true;
}
if(y >= 120) {
for(int i = 1;i <= n; i++) {
if(b[i])
dp[i] = -1;
else
dp[i] = 0;
}
for(int i = 1;i <= n; i++) {
if(dp[i] == -1)
continue;
for(int u : g[i])
dp[u] = std::max(dp[u], dp[i] + 1);
}
std::cout << dp[to] << "\n";
} else {
int ans = -1;
for(auto p : sol[to]) {
if(!b[p.second])
ans = std::max(ans, p.first);
}
std::cout << ans << "\n";
}
for(int i = 1;i <= y; i++)
b[c[i]] = false;
}
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... |