#include <bits/stdc++.h>
using namespace std;
const int rad = 400;
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].push_back(dp[adj[a][idb]][id[idb]]);
dp[a][h++].first++;
id[idb]++;
}
if (h < rad) dp[a].push_back({0, a});
}
int bfs(int a, unordered_set<int> &skip) {
vector<int> dist(N, -1);
for (int i = 1; i <= a; i++) {
if (!skip.count(i)) dist[i] = 0;
for (int x : adj[i]) {
if (skip.count(x)) continue;
dist[i] = max(dist[i], dist[x]+1);
}
}
return dist[a];
}
int prec(int a, unordered_set<int> s) {
int m = -1;
for (int i = 0; i < dp[a].size(); i++) {
if (s.count(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.resize(N+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);
for (int i = 0; i < Q; i++) {
unordered_set<int> s;
int a, y; cin >> a >> y;
for (int j = 0; j < y; j++) {int aus; cin >> aus; s.insert(aus);}
if (y >= rad) cout << bfs(a, s) << "\n";
else cout << prec(a, s) << "\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... |