/**
* In the name of Allah
* We are nothing and you're everything
* Ya Muhammad!
**/
#include <bits/stdc++.h>
#ifdef LOCAL
#define debug(x) cerr << #x << "= " << x << endl;
#else
#define debug(x)
#endif
using namespace std;
using ll = long long;
using ull = uint64_t;
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define mp(x, y) make_pair(x, y)
#define pii pair<int, int>
//#define int long long
const char nl = '\n';
const int N = 1e5+5;
const int inf = 1e9;
vector<int> g[N], ind(N);
vector<pii> v[N];
vector<bool> vis(N);
const int bl = 100;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, q; cin >> n >> m >> q;
for (int i = 0; i < m; ++i) {
int a, b; cin >> a >> b;
g[b].push_back(a);
}
for (int i = 1; i <= n; ++i) {
priority_queue<pii> pq;
for (auto j: g[i])
pq.push(mp(v[j][0].first, j));
while (!pq.empty() && sz(v[i])<bl) {
auto [d, x] = pq.top(); pq.pop();
int y = v[x][ind[x]].second;
if (!vis[y]) {
vis[y] = true;
v[i].push_back(mp(d+1, y));
}
ind[x] += 1;
if (ind[x] < sz(v[x]))pq.push(mp(v[x][ind[x]].first, x));
}
v[i].push_back(mp(0, i));
for (auto j: v[i])vis[j.second] = 0;
for (auto j: g[i])ind[j] = 0;
}
vector<bool> was(n+1);
for (; q>0; --q) {
int t, y; cin >> t >> y;
vector<int> c(y);
for (auto &i: c) {
cin >> i;
was[i] = true;
}
bool ok = false;
for (auto j: v[t])
if (!was[j.second]) {
cout << j.first << nl;
ok = true; break;
}
if (!ok) {
vector<int> dp(n+1);
for (int i = 1; i <= t; ++i) {
dp[i] = (was[i]?-inf:0);
for (auto j: g[i])dp[i] = max(dp[i], dp[j]+1);
}
cout << (dp[t]<0?-1:dp[t]) << nl;
}
for (auto i: c)was[i] = 0;
}
return 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... |