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 int long long
#define INF (int)1e18
#define f first
#define s second
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
const int N = 1e5 + 69, B = 1;
int n, m, q;
vector <int> adj[N];
pair <int, int> dp[N][B]; // {dist, who}
int ds[N];
void Solve()
{
cin >> n >> m >> q;
for (int i = 1; i <= m; i++){
int u, v; cin >> u >> v;
adj[v].push_back(u);
}
for (int i = 0; i < N; i++){
for (int j = 0; j < B; j++){
dp[i][j] = {-1, -1};
}
}
for (int i = 1; i <= n; i++){
vector <pair<int, int>> ok;
ok.push_back({0, i});
for (auto j :adj[i]){
for (int i1 = 0; i1 < B; i1++){
if (dp[j][i1].first == -1) break;
ok.push_back({dp[j][i1].first + 1, dp[j][i1].second});
}
}
sort(ok.begin(), ok.end(), greater<pair<int, int>>());
for (int j = 0; j < min(B, (int)ok.size()); j++){
dp[i][j] = ok[j];
}
}
while (q--){
int x, k; cin >> x >> k;
int kk = k;
set <int> st;
while (k--){
int y; cin >> y;
st.insert(y);
}
if (kk < B){
for (int i = 0; i < B; i++){
if (st.find(dp[x][i].second) == st.end()){
cout << dp[x][i].first << "\n";
break;
}
}
continue;
}
for (int i = 1; i <= n; i++) ds[i] = -INF;
ds[x] = 0;
for (int i = x; i >= 1; i--){
for (auto j : adj[i]){
ds[j] = max(ds[j], ds[i] + 1);
}
}
// for (int i = 1; i <= n; i++){
// cout << ds[i] << " ";
// }
// cout << "\n";
int mx = -1;
for (int i = 1; i <= n; i++) if (ds[i] != -INF && st.find(i) == st.end()) mx = max(mx, ds[i]);
cout << mx << "\n";
}
}
int32_t main()
{
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
//cout << "Case #" << i << ": ";
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
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... |