#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];
ve<int> restr[MXN];
int blck[MXN];
int ans[MXN];
set<pair<int, int>> madst[MXN+1];
const int BLCK = 100;
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;
}
ll populate(ll x){
ll cnt = 0;
priority_queue<pair<int, int>> g;
g.push({0, x});
map<int, int> mxdst;
while(!g.empty()){
cnt++;
auto top = g.top();
if(!mxdst.count(top.second)){
mxdst[top.second] = -1e9;
}
g.pop();
if(madst[x].size() == BLCK && top.first < (*madst[x].begin()).first){
continue;
}
if(top.first > mxdst[top.second]){
madst[x].insert({top.first, top.second});
while(madst[x].size() > BLCK){
madst[x].erase(madst[x].begin());
}
mxdst[top.second] = top.first;
for(int i : adj[top.second]){
g.push({1+top.first, i});
}
}
}
return cnt;
}
int main(){
fill(ans, ans+MXN, -1);
fill(blck, blck+MXN, -1);
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);
}
for(int i = 1; i <= n; i++){
populate(i);
}
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].pb(y);
blck[y] = i;
}
if(sz >= BLCK){
ans[i] = ansgy(t, i);
}
else{
for(auto [u,v]: madst[t]){
if(blck[v] != i){
ans[i] = max(ans[i], u);
}
}
}
}
for(int i = 0; i < q; i++){
cout << ans[i] << 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... |