#include <bits/stdc++.h>
#define ve vector
#define ar array
#define pb push_back
#define ins insert
#define endl '\n'
#define ll long long
using namespace std;
const int MXN = 1e5+1;
ve<int> adj[MXN];
set<int> restr[MXN];
ll ansgy(ll x, ll i){
priority_queue<pair<int, int>> g;
ll dst[MXN];
fill(dst, dst+MXN, -1);
g.push({0, x});
while(!g.empty()){
auto top = g.top();
g.pop();
if(top.first > dst[top.second]){
dst[top.second] = top.first;
for(int i : adj[top.second]){
g.push({1+top.first, i});
}
}
}
int ans = -1;
for(int j : restr[i]){
dst[j] = -1;
}
for(int j : dst){
ans = max(ans, j);
}
return ans;
}
const int BLCK = 330;
int main(){
int n, m, q;
cin >> n >> m >> q;
for(int i = 0; i < m; i++){
int a, b; cin >> a >> b;
adj[b].pb(a);
}
int ans[q];
map<int, ve<int>> qs;
for(int i = 0; i < q; i++){
int t; cin >> t;
int sz; cin >> sz;
for(int j = 0; j < sz; j++){
int y;
cin >> y;
restr[i].insert(y);
}
if(sz >= BLCK-1){
ans[i] = ansgy(t, i);
}
else{
qs[t].pb(i);
}
}
set<pair<int, int>> madst[n+1];
for(int i = 1; i <= n; i++){
// store BLCK max distance
madst[i].insert({0, i});
for(int j : adj[i]){
for(auto k : madst[j]){
madst[i].insert({k.first+1, k.second});
while(madst[i].size() > BLCK){
madst[i].erase(madst[i].begin());
}
}
}
assert(madst[i].size() < BLCK);
for(int j : qs[i]){
int as = -1;
for(auto [u, v] : madst[i]){
if(!restr[j].count(v)){
as = max(as, u);
}
}
ans[j] = as;
}
}
for(int j : ans){
cout << j << endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |