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;
const int N = 1e5+1, INF = 1e9+1;
int n, m, q, bsz = 300;
vector<int> g[N];
vector<pair<int,int> > dp[N];
bool blocked[N];
int f[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> q;
for(int i = 0, s, e; i < m; i++) {
cin >> s >> e;
--s, --e;
g[e].push_back(s);
}
dp[0].emplace_back(0, 0);
for(int i = 1; i < n; i++) {
dp[i].emplace_back(0, i);
for(int j: g[i]) for(auto x: dp[j]) dp[i].emplace_back(x.first + 1, x.second);
nth_element(dp[i].begin(), dp[i].begin() + min(bsz, (int) dp[i].size()), dp[i].end());
dp[i].resize(min(bsz, (int) dp[i].size()));
}
while(q--) {
int targ, Y;
cin >> targ >> Y;
--targ;
vector<int> C(Y);
for(int i = 0; i < Y; i++) {
cin >> C[i];
--C[i];
blocked[C[i]] = true;
}
int res = -INF;
if(Y < bsz) {
for(auto p: dp[targ]) if(!blocked[p.second]) res = max(p.first, res);
} else {
fill(f, f+targ+1, 0);
for(int i: C) f[i] = -INF;
for(int i = 0; i <= targ; i++) if(f[i] != -INF) for(int j: g[i]) {
f[i] = max(f[j] + 1, f[i]);
}
}
if(res == -INF) cout << -1;
else cout << res;
cout << '\n';
for(int i = 0; i < Y; i++) blocked[C[i]] = false;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |