Submission #737316

#TimeUsernameProblemLanguageResultExecution timeMemory
737316nguyentunglamCat Exercise (JOI23_ho_t4)C++17
100 / 100
376 ms44844 KiB
#include<bits/stdc++.h> #define fi first #define se second #define endl "\n" #define ii pair<int, int> using namespace std; const int N = 2e5 + 10; int par[20][N], h[N], p[N], pos[N], lab[N]; long long f[N]; vector<int> adj[N]; void dfs(int u) { for(int j = 1; j <= 18; j++) par[j][u] = par[j - 1][par[j - 1][u]]; for(int &v : adj[u]) if (v != par[0][u]) { par[0][v] = u; h[v] = h[u] + 1; dfs(v); } } int lca(int u, int v) { if (h[u] < h[v]) swap(u, v); int delta = h[u] - h[v]; for(int j = 18; j >= 0; j--) if (delta >> j & 1) u = par[j][u]; if (u == v) return u; for(int j = 18; j >= 0; j--) if (par[j][u] != par[j][v]) { u = par[j][u]; v = par[j][v]; } return par[0][u]; } int dist(int u, int v) { return h[u] + h[v] - 2 * h[lca(u, v)]; } int root(int v) { return lab[v] == v ? v : lab[v] = root(lab[v]); } int main() { #define task "" cin.tie(0) -> sync_with_stdio(0); if (fopen ("task.inp", "r")) { freopen ("task.inp", "r", stdin); freopen ("task.out", "w", stdout); } if (fopen (task".inp", "r")) { freopen (task".inp", "r", stdin); freopen (task".out", "w", stdout); } int n; cin >> n; for(int i = 1; i <= n; i++) cin >> p[i], pos[p[i]] = i; for(int i = 1; i < n; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } dfs(1); for(int i = 1; i <= n; i++) lab[i] = i; for(int i = 1; i <= n; i++) { int u = pos[i]; for(int &v : adj[u]) if (p[v] < i) { int ver = root(v); f[u] = max(f[u], f[ver] + dist(u, ver)); } for(int &v : adj[u]) if (p[v] < i) { int ver = root(v); lab[ver] = u; } } cout << f[pos[n]]; }

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:44:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   44 |         freopen ("task.inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:45:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   45 |         freopen ("task.out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:48:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |         freopen (task".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:49:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |         freopen (task".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...