#include<bits/stdc++.h>
using namespace std;
const int bs = 320;
vector<int> ad[100005];
vector<pair<int, int>> option[100005];
bool used[100005];
void combine(vector<pair<int, int>> &a, vector<pair<int, int>> &b)
{
vector<pair<int, int>> ans;
int j = 0;
for(int i = 0; i < a.size(); i++) if(ans.size() < bs){
while(j < b.size() && b[j].first > a[i].first && ans.size() < bs){
if(used[b[j].second] == 0){
used[b[j].second] = 1;
ans.push_back(b[j]);
}
j++;
}
if(ans.size() == bs) break;
if(used[a[i].second] == 0){
ans.push_back(a[i]); used[a[i].second] = 1;
}
}
while(j < b.size() && ans.size() < bs){
if(used[b[j].second] == 0){
used[b[j].second] = 1;
ans.push_back(b[j]);
}
j++;
}
a = ans;
for(pair<int, int> &i : ans) used[i.second] = 0;
}
int dp[100005];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m, q; cin>>n>>m>>q;
for(int i = 1; i <= m; i++){
int u, v; cin>>u>>v;
ad[v].push_back(u);
}
//Now calculate sqrt(n) best option
for(int i = 1; i <= n; i++){
for(int v : ad[i]) combine(option[i], option[v]);
if(option[i].size() < bs) option[i].push_back({-1, i});
for(auto &j : option[i]) j.first++;
//cerr<<"A"<<i<<endl;
//for(auto j : option[i]) cerr<<j.first<<" "<<j.second<<endl;
}
for(int test = 0; test < q; test++){
int u, nn; cin>>u>>nn;
if(nn >= bs){
//Brute solve
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= nn; i++){
int x; cin>>x; dp[x] = -1e9;
}
for(int i = 1; i <= u; i++) for(int v : ad[i]) dp[i] = max(dp[i], dp[v] + 1);
cout<<max(dp[u], -1)<<'\n';
}
else{
vector<int> a(nn);
for(int i = 0; i < nn; i++) cin>>a[i];
for(int i : a) used[i] = 1;
int ans = -1;
for(auto &i : option[u]) if(used[i.second] == 0){ans = i.first; break;}
for(int i : a) used[i] = 0;
cout<<ans<<'\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... |