#include<bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define all(x) x.begin(), x.end()
#define int long long
#define pq priority_queue
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define pp pop_back
#define F first
#define S second
using namespace std;
vector<pair<int, int>> dp[100010];
vector<int> adj[100010];
bool nah[100010];
void merge(int t, int s){
auto cs = dp[s];
for (auto &i: cs) i.F++;
vector<pair<int, int>> v;
int l = 0, r = 0;
while (l < dp[t].size() && r < cs.size()) {
if (dp[t][l] > cs[r]) v.pb(dp[t][l++]);
else v.pb(cs[r++]);
}
while (l < dp[t].size()) v.pb(dp[t][l++]);
while (r < cs.size()) v.pb(cs[r++]);
dp[t].clear();
for (auto &i : v) {
if(!nah[i.S]) dp[t].pb(i);
nah[i.S] = 1;
}
for (auto &i: dp[t]) nah[i.S] = 0;
if (dp[t].size() > 318) dp[t].resize(318);
}
int sol1(int i) {
for(auto &i: dp[i]) if (!nah[i.S]) return i.F;
return -1;
}
int sol2(int t){
vector<int> dp(100010, -1e9);
for(int j = 1; j <= t; j++) {
if (!nah[j]) dp[j] = 0;
for (auto &i: adj[j]) dp[j] = max(dp[j], dp[i] + 1);
}
return dp[t] < 0 ? -1 : dp[t];
}
void solve () {
int n, m, q;
cin >> n >> m >> q;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
adj[b].pb(a);
}
for (int i = 1; i <= n; i++) {
dp[i] = {{0, i}};
for (auto j: adj[i]) merge(i, j);
}
while (q--) {
int t, a;
cin >> t >> a;
vector<int> c;
for (int i = 0; i < a; i++) {
int b; cin >> b;
nah[b] = 1;
c.pb(b);
}
if (a <= 318) cout << sol1(t) << '\n';
else cout << sol2(t) << '\n';
for (auto &i: c) nah[i] = 0;
}
}
signed main() {IOS solve(); 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... |