Submission #1241521

#TimeUsernameProblemLanguageResultExecution timeMemory
1241521yoshi_33550336Cat Exercise (JOI23_ho_t4)C++20
21 / 100
2096 ms10568 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #ifndef yoshi_likes_e4 #define endl '\n' #endif #define problem "" #define multitest 0 #define debug(x) cerr << #x << " = " << x << endl; void init() { } mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); vector<int> adj[100000]; int depth[100000]; int binlift[17][100000]; int in_queue[100000]; int d[100000]; deque<int> qu; void dfs(int i, int par) { binlift[0][i] = par; for (auto j : adj[i]) { if (j != par) { depth[j] = depth[i] + 1; dfs(j, i); } } } int lca(int l, int r) { if (depth[l] > depth[r]) swap(l, r); int x = depth[r] - depth[l]; for (int R = __lg(x); R >= 0; R--) { if ((x >> R) & 1) { r = binlift[R][r]; x -= 1 << R; } } if (l == r) return l; else { for (int R = __lg(depth[l]); R >= 0; R--) { if (binlift[R][r] != binlift[R][l]) { l = binlift[R][l]; r = binlift[R][r]; } } return binlift[0][l]; } } int dist(int u, int v) { return depth[u] + depth[v] - 2 * depth[lca(u, v)]; } int par[200000]; int find_(int x) { while (par[x] != x) x = par[x]; return x; } void Yoshi() { int n; cin >> n; vector<int> p(n), invp(n); for (auto &i : p) cin >> i, i--; for (int i = 0; i < n; i++) invp[p[i]] = i, par[i] = i; for (int i = 1; i < n; i++) { int a, b; cin >> a >> b; a--, b--; adj[a].push_back(b); adj[b].push_back(a); } for (auto &i : adj) shuffle(i.begin(), i.end(), rng); dfs(0, -1); for (int i = 1; i < 17; i++) for (int j = 0; j < n; j++) { if (binlift[i - 1][j] == -1) binlift[i][j] = -1; else binlift[i][j] = binlift[i - 1][binlift[i - 1][j]]; } vector<int> DP(n); for (int i = 0; i < n; i++) { for (auto &k : adj[invp[i]]) if (p[k] < i) DP[invp[i]] = max(DP[invp[i]], DP[find_(k)] + dist(find_(k), invp[i])); for (auto &k : adj[invp[i]]) if (p[k] < i) par[find_(k)] = invp[i]; } cout << DP[invp[n - 1]] << endl; } signed main() { #ifndef yoshi_likes_e4 ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); if (fopen(problem ".inp", "r")) { freopen(problem ".inp", "r", stdin); freopen(problem ".out", "w", stdout); } #endif init(); int t = 1; #if multitest cin >> t; #endif while (t--) Yoshi(); }

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:119:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  119 |         freopen(problem ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:120:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  120 |         freopen(problem ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...