#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
#include <set>
using namespace std;
using ll = long long;
using vl = vector<ll>;
istream &operator>>(istream &os, vl &a) {
for (auto &x: a) cin >> x;
return os;
}
constexpr int N = 200200;
constexpr int B = 100;
vector<int> g[N], ig[N];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, q;
cin >> n >> m >> q;
for (int i = 0; i < m; i++) {
int s, e;
cin >> s >> e;
g[s].push_back(e);
ig[e].push_back(s);
}
std::vector<std::vector<pair<int, int> > > least(n + 1, std::vector<pair<int, int> >());
std::vector<int> used(n + 1, -1);
least[1] = {{0, 1}};
for (int i = 2; i <= n; i++) {
vector<int> have;
for (auto &d: ig[i]) {
for (auto &[x, y]: least[d]) {
if (used[y] < 0) have.push_back(y);
used[y] = max(used[y], x + 1);
}
}
least[i].emplace_back(0, i);
for (auto &x: have) {
least[i].emplace_back(used[x], x);
}
sort(least[i].begin(), least[i].end());
reverse(least[i].begin(), least[i].end());
while (least[i].size() > B) {
least[i].pop_back();
}
for (auto &d: have) used[d] = -1;
}
for (int i = 0; i < q; i++) {
int t, y;
cin >> t >> y;
vector<int> blocked(y);
std::set<int> bl;
for (int j = 0; j < y; j++) {
cin >> blocked[j];
bl.insert(blocked[j]);
}
if (y >= B) {
std::vector<int> dp(n + 1, -1);
dp[t] = 0;
for (int j = t; j >= 1; j--) {
if (dp[j] == -1) continue;
for (const auto &d: ig[j]) {
dp[d] = max(dp[d], dp[j] + 1);
}
}
int value = -1;
for (int g = 1; g <= t; g++) {
if (!bl.contains(g)) value = max(value, dp[g]);
}
std::cout << value << "\n";
} else {
int value = -1;
for (const auto &d: least[t]) {
if (!bl.contains(d.second)) value = max(value, d.first);
}
std::cout << value << "\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... |