Submission #1277235

#TimeUsernameProblemLanguageResultExecution timeMemory
1277235minggaCat Exercise (JOI23_ho_t4)C++20
54 / 100
657 ms41296 KiB
// Author: caption_mingle #include "bits/stdc++.h" using namespace std; #define ln "\n" #define pb push_back #define fi first #define se second #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) #define ll long long const int mod = 1e9 + 7; const int inf = 2e9; const int N = 2e5 + 7; int n, a[N], up[N][20], h[N]; int par[N], sz[N], mx[N], f[N]; vector<int> g[N]; void dfs(int u, int p) { for(int i = 1; i < 20; i++) { up[u][i] = up[up[u][i - 1]][i - 1]; } for(int v : g[u]) { if(v == p) continue; h[v] = h[u] + 1; up[v][0] = u; dfs(v, u); } } int lca(int u, int v) { if(h[u] < h[v]) swap(u, v); int k = h[u] - h[v]; for(int i = 0; (1 << i) <= k; i++) { if(k >> i & 1) u = up[u][i]; } if(u == v) return u; for(int i = 19; i >= 0; i--) { if(up[u][i] != up[v][i]) { u = up[u][i]; v = up[v][i]; } } return up[u][0]; } int dist(int u, int v) { return h[u] + h[v] - 2 * h[lca(u, v)]; } int find(int u) { return u == par[u] ? u : par[u] = find(par[u]); } void join(int u, int v) { u = find(u), v = find(v); if(u == v) return; if(sz[u] < sz[v]) swap(u, v); sz[u] += sz[v]; par[v] = u; mx[u] = max(mx[u], mx[v]); } signed main() { cin.tie(0) -> sync_with_stdio(0); #define task "" if(fopen(task ".INP", "r")) { freopen(task ".INP", "r", stdin); freopen(task ".OUT", "w", stdout); } cin >> n; for(int i = 1; i <= n; i++) cin >> a[i]; for(int i = 1; i < n; i++) { int u, v; cin >> u >> v; u = a[u], v = a[v]; g[u].pb(v); g[v].pb(u); } dfs(1, 0); for(int i = 1; i <= n; i++) { sz[i] = 1; par[i] = mx[i] = i; } for(int i = 1; i <= n; i++) { for(int v : g[i]) { v = mx[find(v)]; if(v < i) { f[i] = max(f[i], f[v] + dist(i, mx[v])); join(i, v); } } cerr << i << ' ' << f[i] << ln; } cout << f[n] << ln; cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC; }

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:70:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |                 freopen(task ".INP", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:71:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |                 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...