#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, q; cin >> n >> m >> q;
int bl = 33;
vector<vector<int>> to(n);
for (int i = 0; i < m; ++i) {
int a, b; cin >> a >> b;
to[--a].push_back(--b);
}
vector<set<pair<int, int>>> best(n);
vector<map<int, int>> ex(n);
auto add = [&](int idx, pair<int, int> v) -> void {
if (ex[idx].find(v.second) != ex[idx].end()) {
int val = ex[idx][v.second];
if (val >= v.first) return;
best[idx].erase({val, v.second});
ex[idx].erase(v.second);
}
best[idx].insert(v);
ex[idx][v.second] = v.first;
if (best[idx].size() > bl) {
auto it = best[idx].begin();
ex[idx].erase(it->second);
best[idx].erase(it);
}
};
for (int i = 0; i < n; ++i) {
add(i, {0, i});
for (int j : to[i]) {
for (pair<int, int> k : best[i]) {
add(j, {k.first + 1, k.second});
}
}
}
vector<bool> nouse(n);
vector<int> mx(n);
vector<int> c(n);
while (q--) {
int t, y; cin >> t >> y;
for (int i = 0; i < y; ++i) {
cin >> c[i];
--c[i];
nouse[c[i]] = true;
}
--t;
if (y >= bl) {
int ans = -1;
for (int j = t; j >= 0; --j) {
mx[j] = (j == t ? 0 : -1);
for (int k : to[j]) {
if (k <= t && mx[k] != -1) mx[j] = max(mx[j], mx[k] + 1);
}
if (!nouse[j]) ans = max(ans, mx[j]);
}
cout << ans << '\n';
} else {
auto it = prev(best[t].end());
while (it != best[t].begin() && nouse[it->second]) {
--it;
}
if (nouse[it->second]) {
cout << "-1\n";
} else {
cout << it->first << '\n';
}
}
for (int i = 0; i < y; ++i) {
nouse[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... |