Submission #1150336

#TimeUsernameProblemLanguageResultExecution timeMemory
1150336WendidiaskCat Exercise (JOI23_ho_t4)C++20
100 / 100
240 ms71428 KiB
/* :---:--:--::::::--:-:----: ::-:::. ::-::: .* -===.. :--- .-+*##=-------====-: :--: :+##=.* -= :=+- .-=: .+++-. :- -=. .=+- .--. .++=- + -= :*- :-. +*+- :- -=. -*: :=: #*: * -=. +- .= =:%+ -: -=. +- -- -%: * -=. *: +. :%-.. =. -=. # -: :=# .. + -=. == -= ---+ . = -=. .# .+ -: == . = :------::------:.-=. % .= :- == .=. ---:. :--#+. .% :- = .* .+.. :=- :++ -+ =: + #. + -=: :*%-*: * := :# :=:= . :-----: =+-+ .+ * -* *=- . .- :::::::. :* .#+. + :- -+ :#. --::-----: :* ++= .- + :*. *. .:::::*%: ++ +.-=. + + -#**----: .= -*. + -= = * +.#=+: + . .=+: =. -=. = = -+ :++: + .. .=+= .= -=. - = %- :====-:. =. .:--===- * -=. - + +-- .:--==#==-:.= + -=. = * + * =. : .= -=. = + = * + = -=. + :- = .= =: = -=. .- * + =: * * -=. + :- .+ =- :- .+ -=. .+ * * := * := -= * = :- =: .- .=: -=. =: -: * .=- + .=- -= :- =: -: --: :- :--: -=. .= =:+ ----. * :---. -= .+ --=*----------------------=*+=--=+--=+*+------------------------------------------------%+:. :: := +:- -- :. .=. =. -= -= .=: .=. .=: .+: :-- :-: -- -=. :--. --: :--: .--: .-:-: ::--. :----. .:---- .:::::::::-::::::--::::::::. */ // Hello this is Tyx2019's clone #include <bits/stdc++.h> using namespace std; #define int long long #define debug(x) cerr << #x << " is " << x << "\n" #define hehe ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define rep(i, a, b) for (int i = a; i <= b; i++) #define repb(i, a, b) for (int i = b; i >= a; i--) #define pii pair<int, int> #define linebreak cout << "---------------------------------------------------------\n" #define f first #define s second #define pb push_back #pragma GCC optimize("O3,unroll-loops") #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") // good luck const int MS = 2e5+5, MS1 = 1e6+5, mod = 1e9+9, INF = 3e18, blk = 400; int n, p[MS], inv[MS], parent[MS], on[MS], macs[MS], twok[MS][20], depth[MS], dis[MS]; vector<int> adj[MS], adj1[MS]; int find(int x) { if (parent[x] == x) return x; return parent[x] = find(parent[x]); } bool same(int x, int y) { return find(x) == find(y); } void merge(int x, int y) { int rootx = find(x), rooty = find(y); parent[rootx] = rooty; macs[rooty] = max(macs[rooty], macs[rootx]); } void init(int x, int par) { for (int k = 0; k < 20; k++) { if (twok[x][k] == -1) break; twok[x][k+1] = twok[twok[x][k]][k]; } for (auto v : adj[x]) { if (v == par) continue; twok[v][0] = x; depth[v] = depth[x]+1; init(v, x); } } int kpar(int x, int k) { for (int j = 0; j < 20; j++) { if (x == -1) break; if (k&(1<<j)) x = twok[x][j]; } return x; } int lca(int a, int b) { if (depth[a] < depth[b]) swap(a, b); a = kpar(a, depth[a]-depth[b]); if (a == b) return a; for (int k = 19; k >= 0; k--) { if (twok[a][k] != twok[b][k]) { a = twok[a][k]; b = twok[b][k]; } } return twok[a][0]; } void dfs(int x, int par) { for (auto v : adj1[x]) { if (v == par) continue; dis[v] = dis[x] + depth[x] + depth[v] - 2*depth[lca(v, x)]; dfs(v, x); } } int32_t main() { cin >> n; for (int i = 1; i <= n; i++) {cin >> p[i]; inv[p[i]] = i; parent[i] = i; macs[i] = p[i]; } for (int i = 1; i < n; i++) { int a, b; cin >> a >> b; adj[a].pb(b); adj[b].pb(a); } memset(on, 0, sizeof(on)); for (int i = 1; i <= n; i++) { on[inv[i]] = 1; for (auto it : adj[inv[i]]) { if (on[it]) { int temp = find(it); adj1[inv[i]].pb(inv[macs[temp]]); adj1[inv[macs[temp]]].pb(inv[i]); merge(it, inv[i]); } } } depth[1] = 0; memset(twok, -1, sizeof(twok)); init(1, -1); dis[inv[n]] = 0; dfs(inv[n], -1); int ans = 0; for (int i = 1; i <= n; i++) ans = max(ans, dis[i]); cout << ans; }
#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...