#include <bits/stdc++.h>
using namespace std;
const int rad = 333;
int N;
vector<vector<int>> adj;
vector<vector<pair<int, int>>> dp;
void crea(int a) {
unordered_set<int> pr;
int dc = adj[a].size();
vector<int> id(dc, 0);
bool ff = 1;
int h = 0;
while (h < rad) {
int best = -1, idb = -1;
for (int i = 0; i < dc; i++) {
while (id[i] < dp[adj[a][i]].size() && pr.count(dp[adj[a][i]][id[i]].second)) id[i]++;
if (id[i] >= dp[adj[a][i]].size()) continue;
if (dp[adj[a][i]][id[i]].first > best) {
idb = i;
best = dp[adj[a][i]][id[i]].first;
}
}
if (idb < 0) {
ff = 0; break;
}
pr.insert(dp[adj[a][idb]][id[idb]].second);
dp[a][h] = dp[adj[a][idb]][id[idb]];
dp[a][h++].first++;
id[idb]++;
}
if (h < rad) dp[a][h] = {a, 0};
}
int bfs(int a, bitset<100001> &bs) {
vector<int> dist(N, -1000000000);
for (int i = 1; i <= a; i++) {
if (!bs[i]) dist[i] = 0;
for (int x : adj[i]) dist[i] = max(dist[i], dist[x]+1);
}
return max(-1, dist[a]);
}
int prec(int a, bitset<100001> &bs) {
int m = -1;
for (int i = 0; i < dp[a].size(); i++) {
if (bs[dp[a][i].second]) continue;
m = max(m, dp[a][i].first);
}
return m;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL); cout.tie(NULL);
int M, Q; cin >> N >> M >> Q;
adj.resize(N+1); dp = vector<vector<pair<int, int>>> (N+1, vector<pair<int, int>> (rad, {-1, -1}));
for (int i = 0; i < M; i++) {
int s, t; cin >> s >> t;
adj[t].push_back(s);
}
for (int i = 1; i <= N; i++) crea(i);
bitset<100001> bs;
for (int i = 0; i < Q; i++) {
int a, y; cin >> a >> y;
vector<int> qc(y);
for (int j = 0; j < y; j++) {
cin >> qc[j];
bs[qc[j]] = 1;
}
if (y >= rad) cout << bfs(a, bs) << "\n";
else cout << prec(a, bs) << "\n";
for (int j = 0; j < y; j++) bs[qc[j]] = 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... |