#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000 + 10;
const int SQ = 320;
int n, m, q;
vector<int> adj[MAX_N];
vector<pair<int, int>> fars[MAX_N];
bool block[MAX_N];
int main() {
cin >> n >> m >> q;
for (int i = 1, s, t; i <= m; ++i) {
cin >> s >> t;
adj[t].push_back(s);
}
for (int i = 1; i <= m; ++i) {
fars[i].push_back({0, i});
for (int j : adj[i]) {
vector<pair<int, int>> new_fars;
for (int pi = 0, pj = 0; new_fars.size() < min(SQ, int(fars[i].size() + fars[j].size()));) {
if (pi < fars[i].size() && (pj == fars[j].size() || fars[i][pi].first >= fars[j][pj].first + 1)) {
new_fars.push_back(fars[i][pi++]);
} else {
new_fars.push_back(fars[j][pj++]);
++new_fars.back().first;
}
}
fars[i] = new_fars;
}
}
while (q--) {
int t, y;
cin >> t >> y;
vector<int> blocks;
while (y--) {
int c;
cin >> c;
block[c] = true;
blocks.push_back(c);
}
int res = -1;
if (y < SQ) {
for (auto [d, u] : fars[t]) {
if (!block[u]) {
res = d;
break;
}
}
} else {
int d[t + 1];
for (int i = 1; i <= t; ++i) {
if (block[i]) {
d[i] = -1;
} else {
d[i] = 0;
for (int j : adj[i]) {
d[i] = max(d[i], d[j] + 1);
}
}
}
res = d[t];
}
cout << res << '\n';
for (int c : blocks) {
block[c] = false;
}
}
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |