Submission #899529

#TimeUsernameProblemLanguageResultExecution timeMemory
899529Ice_manCat Exercise (JOI23_ho_t4)C++14
100 / 100
304 ms58048 KiB
#include <iostream> #include <chrono> #include <vector> #define maxn 200005 #define maxlog 20 #define INF 1000000010 #define LINF 1000000000000000005 #define endl '\n' #define pb(x) push_back(x) #define X first #define Y second #define control cout<<"passed"<<endl; #pragma GCC optimize("O3" , "Ofast" , "unroll-loops" , "fast-math") #pragma GCC target("avx2") using namespace std; std::chrono::high_resolution_clock::time_point startT, currT; constexpr double TIME_MULT = 1; double timePassed() { using namespace std::chrono; currT = high_resolution_clock::now(); double time = duration_cast<duration<double>>(currT - startT).count(); return time * TIME_MULT; } long long n; long long h[maxn] , depth[maxn]; vector <long long> v[maxn]; void read() { cin >> n; for(long long i = 1; i <= n; i++) cin >> h[i]; long long from , to; for(long long i = 1; i < n; i++) { cin >> from >> to; v[h[from]].pb(h[to]); v[h[to]].pb(h[from]); } } long long bin_lift[maxn][maxlog]; void dfs(long long node , long long _parent) { for(long long nb : v[node]) { if(nb == _parent) continue; depth[nb] = depth[node] + 1; bin_lift[nb][0] = node; dfs(nb , node); } } void calc() { for(long long i = 1; i < maxlog; i++) for(long long j = 1; j <= n; j++) bin_lift[j][i] = bin_lift[bin_lift[j][i - 1]][i - 1]; } long long get_distance(long long a , long long b) { long long dist = 0; if(depth[b] > depth[a]) swap(a ,b); long long need = depth[a] - depth[b]; for(long long power = maxlog - 1; power > -1; power--) { if(need >= (1 << power)) { a = bin_lift[a][power]; dist += (1 << power); need -= (1 << power); } } if(a == b) return dist; for(long long power = maxlog - 1; power > -1; power--) { if(bin_lift[a][power] != bin_lift[b][power]) { a = bin_lift[a][power]; b = bin_lift[b][power]; dist += (1 << (power + 1)); } } return (dist + 2); } long long parent[maxn]; long long find_parent(long long node) { ///cout << node << endl; return parent[node] == -1? node : parent[node] = find_parent(parent[node]); } long long pom[maxn]; void solve() { for(long long i = 1; i <= n; i++) parent[i] = -1; for(long long node = 1; node <= n; node++) for(long long nb : v[node]) { nb = find_parent(nb); ///control if(nb <= node) { long long d = get_distance(node , nb); pom[node] = max(pom[node] , pom[nb] + d); parent[nb] = node; } } cout << pom[n] << endl; } int main() { /**#ifdef ONLINE_JUDGE freopen("taxi.in", "r", stdin); freopen("taxi.out", "w", stdout); #endif*/ /**ios_base::sync_with_stdio(false); cin.tie(nullptr);*/ startT = std::chrono::high_resolution_clock::now(); read(); ///control dfs(1 , -1); ///control calc(); ///control solve(); return 0; }
#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...