Submission #1241566

#TimeUsernameProblemLanguageResultExecution timeMemory
1241566yoshi_33550336Cat Exercise (JOI23_ho_t4)C++20
0 / 100
2 ms5184 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[200000]; int depth[200000]; int binlift[18][200000]; int in_queue[200000]; int d[200000]; 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 n; int find_(int x) { int counter = 0; while (par[x] != x) counter++; par[x] = x, assert(counter < n); } void Yoshi() { 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); } dfs(0, -1); for (int i = 1; i < 18; 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) { int u = find_(k); DP[invp[i]] = max(DP[invp[i]], DP[u] + dist(u, 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 'long long int find_(long long int)':
Main.cpp:72:1: warning: control reaches end of non-void function [-Wreturn-type]
   72 | }
      | ^
Main.cpp: In function 'int main()':
Main.cpp:122:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  122 |         freopen(problem ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:123:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  123 |         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...