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 = 50;
int n, m, q;
vector <int> adj[N];
pair <int, int> dp[N][B]; // {dist, who}
bool pp[N];
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>>());
set <int> bruh;
vector <pair<int, int>> nok;
for (auto [x, y] : ok){
if (bruh.find(y) == bruh.end()){
nok.push_back({x, y});
bruh.insert(y);
}
}
ok = nok;
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;
vector <int> st;
while (k--){
int y; cin >> y;
pp[y] = true;
st.push_back(y);
}
if (kk < B){
for (int i = 0; i < B; i++){
if (!pp[dp[x][i].second]){
cout << dp[x][i].first << "\n";
break;
}
}
for (auto x : st) pp[x] = false;
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);
}
}
int mx = -1;
for (int i = 1; i <= n; i++) if (ds[i] != -INF && !pp[i]) mx = max(mx, ds[i]);
for (auto x : st) pp[x] = false;
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... |