This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<limits.h>
#include<math.h>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<iomanip>
#include<cstring>
#include<random>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;
int n,m,q;
vector<int>rev[100000];
vector<pair<int,int>>farthest[100000]; // (dist,node)
const int CHUNK_SIZE=100;
int solve(int src,vector<int>blocked){
vector<int>res(n);
for(int i:blocked)
res[i]=-1e9;
for(int node=0;node<n;node++){
for(int ne:rev[node])
res[node]=max(res[node],res[ne]+1);
}
return max(-1,res[src]);
}
void setup(){
for(int node=0;node<n;node++){
//vector<int>it(rev[node].size());
/*while(farthest[node].size()<CHUNK_SIZE){
int nxt=-1;
for(int i=0;i<(int)it.size();i++){
if(it[i]==(int)farthest[rev[node][i]].size())
continue;
if(nxt==-1||farthest[rev[node][i]][it[i]].first>farthest[rev[node][nxt]][it[nxt]].first)
nxt=i;
}
if(nxt==-1)
break;
farthest[node].push_back(farthest[rev[node][nxt]][it[nxt]]);
farthest[node].back().first++;
it[nxt]++;
}*/
unordered_map<int,int>mp;
for(int ne:rev[node])
for(auto[d,i]:farthest[ne])
mp[i]=max(mp[i],d);
for(auto[i,d]:mp)
farthest[node].emplace_back(d,i);
sort(farthest[node].begin(),farthest[node].end());
reverse(farthest[node].begin(),farthest[node].end());
while(farthest[node].size()>CHUNK_SIZE)
farthest[node].pop_back();
for(auto&i:farthest[node])
i.first++;
if(farthest[node].size()<CHUNK_SIZE)
farthest[node].emplace_back(0,node);
}
}
void solve(){
cin>>n>>m>>q;
for(int i=0;i<m;i++){
int a,b;
cin>>a>>b;
a--;b--;
rev[b].push_back(a);
}
setup();
while(q--){
int src,k;
cin>>src>>k;
src--;
vector<int>blocked(k);
for(int&i:blocked){
cin>>i;
i--;
}
if(k>=CHUNK_SIZE){
cout<<solve(src,blocked)<<"\n";
continue;
}
set<int>s(blocked.begin(),blocked.end());
bool done=false;
for(auto[d,i]:farthest[src]){
if(s.count(i)==0){
cout<<d<<"\n";
done=true;
break;
}
}
if(!done)
cout<<"-1\n";
}
}
int main(){
ios::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);
//freopen("/Users/jakob/cp_testing/test.txt","r",stdin);//freopen("output.out","w",stdout);
int t=1;//cin>>t;
while(t--)solve();
return 0;
}
/*
5 6 3
1 2
2 4
3 4
1 3
3 5
4 5
4 1 1
5 2 2 3
2 3 1 4 5
1
3
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... |