#include <bits/stdc++.h>
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define int long long
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define ff first
#define ss second
#define pb push_back
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
//#define double long double
template<class T, class U> inline bool chmin(T& a, const U& b) { if (a > b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmax(T& a, const U& b) { if (a < b) { a = b; return true; } return false; }
const int N = 1000002;
//const int inf = 1e18;
//const int mx = 1e13;
//const int mod = 1e9 + 7;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//#define rnd(l, r) uniform_int_distribution <int> (l, r)(rng)
void solve(){
int n, m, q;
cin >> n >> m >> q;
vector<vector<int>> g(n+1), ig(n+1);
for(int i = 0; i < m; i++){
int u, v;
cin >> u >> v;
g[u].pb(v);
ig[v].pb(u);
}
vector<vector<array<int,2>> > lst(n+1);
vector<int> us(n+1, -1);
lst[1] = {{0,1}};
for(int i = 2; i <= n; i++){
vector<int> have;
for(auto to:ig[i]){
for(auto [cnt, a] : lst[to]){
if(us[a] < 0)have.pb(a);
chmax(us[a], cnt+1);
}
}
lst[i].pb({0, i});
for(auto x:have)lst[i].pb({us[x], x});
sort(rall(lst[i]));
while(lst[i].size() > 100)lst[i].pop_back();
for(auto x:have)us[x] = -1;
}
//for(int i = 1; i <= n; i++){
//cout << i << '\n';
//for(auto [a, cn]:lst[i])cout << " : " << a << ' ' << cn << '\n';
//}
while(q--){
int x;
cin >> x;
int nm;cin >> nm;
set<int> st;
for(int i = 0; i < nm; i++){
int v;
cin >> v;
st.insert(v);
}
int ans = -1;
if(nm >= 100){
vector<int> dp(x + 1, -n);
dp[x] = 0;
for(int i = x; i > 0; i--){
for(auto to : ig[i])
chmax(dp[to], dp[i] + 1);
}
for(int i = 1; i <= x; i++)
if(!st.count(i))chmax(ans, dp[i]);
}else{
for(auto [cn,a]:lst[x])
if(!st.count(a))
chmax(ans, cn);
}
cout << ans << '\n';
};
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
int tt = 1;
//cin >> tt;
while(tt--){
solve();
};
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |