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<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define task "code"
const int ar = 1e5 + 5;
const ll mod = 1e9 + 7;
const int block = 100;
int n, m, q;
vector<int> ad[ar];
vector<pair<int, int>> lis[ar];
int x[ar];
bool check[ar];
int dp[ar];
int mx[ar];
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    if (fopen(task".inp", "r"))
    {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    cin >> n >> m >> q;
    for (int i = 1; i <= m; i++)
    {
        int s, e;
        cin >> s >> e;
        ad[e].push_back(s);
    }
    for (int i = 1; i <= n; i++)
    {
        lis[i] = {{0, i}};
        for (auto j : ad[i])
        {
            mx[j] = max(mx[j], 1);
            for (auto [w, u] : lis[j])
            {
                mx[u] = max(mx[u], w + 1);
            }
        }
        for (auto j : ad[i])
        {
            if (!check[j])
            {
                lis[i].push_back({mx[j], j});
                check[j] = true;
            }
            for (auto [w, u] : lis[j])
            {
                if (!check[u])
                {
                    lis[i].push_back({mx[u], u});
                    check[u] = true;
                }
            }
        }
        for (auto j : ad[i])
        {
            check[j] = false;
            mx[j] = 0;
            for (auto [w, u] : lis[j])
            {
                check[u] = false;
                mx[u] = 0;
            }
        }
        sort(lis[i].begin(), lis[i].end(), greater<pair<int, int>>());
        while(lis[i].size() > block) lis[i].pop_back();
    }
    while(q--)
    {
        int t, y;
        cin >> t >> y;
        for (int i = 1; i <= y; i++)
        {
            cin >> x[i];
            check[x[i]] = true;
        }
        if (y >= block)
        {
            for (int i = 1; i <= t; i++)
            {
                dp[i] = 0;
                for (auto j : ad[i])
                {
                    if (check[j])
                    {
                        if (dp[j] > 0) dp[i] = max(dp[i], dp[j] + 1);
                    }
                    else dp[i] = max(dp[i], dp[j] + 1);
                }
            }
            cout << ((dp[t] == 0 && check[t]) ? -1 : dp[t]) << '\n';
        }
        else
        {
            int ans = -1;
            for (auto [w, u] : lis[t])
            {
                if (!check[u])
                {
                    ans = w;
                    break;
                }
            }
            cout << ans << '\n';
        }
        for (int i = 1; i <= y; i++)
        {
            check[x[i]] = false;
        }
    }
}
Compilation message (stderr)
bitaro.cpp: In function 'int main()':
bitaro.cpp:26:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   26 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bitaro.cpp:27:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |