This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O2")
#pragma GCC target("avx2,sse")
#include <bits/stdc++.h>
#ifdef DEBUG
#include "debug.hpp"
#endif
using namespace std;
#define all(c)              (c).begin(), (c).end()
#define rall(c)             (c).rbegin(), (c).rend()
#define traverse(c, it)     for(auto it = (c).begin(); it != (c).end(); ++it)
#define rep(i, N)           for(int i = 0; i < (N); ++i)
#define rrep(i, N)          for(int i = (N) - 1; i >= 0; --i)
#define rep1(i, N)          for(int i = 1; i <= (N); ++i)
#define rep2(i, s, e)       for(int i = (s); i <= (e); ++i)
#define rep3(i, s, e, d)    for(int i = (s); (d) >= 0 ? i <= (e) : i >= (e); i += (d))
#ifdef DEBUG
#define debug(x...)         { \
                            ++dbg::depth; \
                            string dbg_vals = dbg::to_string(x); \
                            --dbg::depth; \
                            dbg::fprint(__func__, __LINE__, #x, dbg_vals); \
                            }
#define light_debug(x)      { \
                            dbg::light = true; \
                            dbg::dout << __func__ << ":" << __LINE__; \
                            dbg::dout << "  " << #x << " = " << x << endl; \
                            dbg::light = false; \
                            }
#else
#define debug(x...)         42
#define light_debug(x)      42
#endif
using ll = long long;
template<typename T>
inline T& ckmin(T& a, T b) { return a = a > b ? b : a; }
template<typename T>
inline T& ckmax(T& a, T b) { return a = a < b ? b : a; }
constexpr int BLOCK{320};
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    #ifdef DEBUG
    freopen("debug", "w", stderr);
    #endif
    int N, M, Q; cin >> N >> M >> Q;
    vector<vector<int>> adj(N);
    rep(_, M) {
        int S, E; cin >> S >> E; --S, --E;
        adj[E].push_back(S);
    }
    vector<vector<pair<int, int>>> far_nodes(N);
    vector<bool> taken(N);
    auto get_far_nodes = [&](const int u) {
        using T = pair<int, int>;
        const int k{int(adj[u].size())};
        vector<vector<T>::iterator> elems(k);
        far_nodes[u].reserve(BLOCK);
        rep(i, k)
            elems[i] = far_nodes[adj[u][i]].begin();
        rep(_, BLOCK) {
            int j{-1};
            rep(i, k) {
                while(elems[i] != far_nodes[adj[u][i]].end() && taken[elems[i]->second])
                    ++elems[i];
                if(elems[i] == far_nodes[adj[u][i]].end())
                    continue;
                if(j == -1 || *elems[i] > *elems[j])
                    j = i;
            }
            if(j == -1) break;
            else {
                far_nodes[u].push_back(*elems[j]);
                taken[elems[j]->second] = true;
                ++elems[j];
            }
        }
        if(int(far_nodes[u].size()) < BLOCK)
            far_nodes[u].push_back({-1, u});
        for(auto& [d, v] : far_nodes[u]) ++d, taken[v] = false;
    };
    debug(far_nodes); 
    vector<bool> bad(N);
    auto brute_force = [&](const int u) {
        vector<int> dist(u + 1, -M); dist[u] = 0;
        rrep(v, u + 1) 
            for(int w : adj[v])
                ckmax(dist[w], dist[v] + 1);
        int ans{-1};
        rep(i, u + 1)
            if(!bad[i]) ckmax(ans, dist[i]);
        return ans;
    };
    rep(u, N) get_far_nodes(u);
    debug(far_nodes);
    rep(_, Q) {
        int T, Y; cin >> T >> Y; --T;
        vector<int> C(Y);
        for(int& x : C) cin >> x, bad[--x] = true;
        if(Y < BLOCK) {
            int ans{-1};
            for(const auto& [d, u] : far_nodes[T])
                if(!bad[u]) { ans = d; break; }
            cout << ans << '\n';
        } else 
            cout << brute_force(T) << '\n';
        for(int& x : C) bad[x] = false;
    }
    #ifdef DEBUG
    dbg::dout << "\nExecution time: " << clock() * 1000 / CLOCKS_PER_SEC  << "ms" << endl;
    #endif
    return 0;
}
Compilation message (stderr)
bitaro.cpp: In function 'int main()':
bitaro.cpp:36:29: warning: statement has no effect [-Wunused-value]
   36 | #define debug(x...)         42
      |                             ^~
bitaro.cpp:99:5: note: in expansion of macro 'debug'
   99 |     debug(far_nodes);
      |     ^~~~~
bitaro.cpp:36:29: warning: statement has no effect [-Wunused-value]
   36 | #define debug(x...)         42
      |                             ^~
bitaro.cpp:114:5: note: in expansion of macro 'debug'
  114 |     debug(far_nodes);
      |     ^~~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |