Submission #1051854

#TimeUsernameProblemLanguageResultExecution timeMemory
1051854peraFactories (JOI14_factories)C++17
100 / 100
2779 ms374480 KiB
#include<bits/stdc++.h> #define pli pair<int , long long> #include "factories.h" using namespace std; const int MAX = 5e5 + 1 , LOG = 21; const long long oo = 1e16; int cen , dead[MAX] , sz[MAX]; long long e[MAX]; vector<int> g[MAX]; vector<long long> w[MAX]; vector<pli> ups[MAX]; int dfs(int u , int p = -1){ sz[u] = 1; for(int x = 0;x < (int)g[u].size();x ++){ if(g[u][x] != p && !dead[g[u][x]]){ dfs(g[u][x] , u); sz[u] += sz[g[u][x]]; } } return sz[u]; } int centroid(int u , int e , int p = -1){ for(int x = 0;x < (int)g[u].size();x ++){ if(g[u][x] != p && !dead[g[u][x]] && sz[g[u][x]] > e / 2){ return centroid(g[u][x] , e , u); } } return u; } void build(int u , int p , long long D){ ups[u].push_back({cen , D}); for(int x = 0;x < (int)g[u].size();x ++){ if(g[u][x] != p && !dead[g[u][x]]){ build(g[u][x] , u , D + w[u][x]); } } } void solve(int u){ u = centroid(u , dfs(u)); cen = u; dead[u] = 1; for(int x = 0;x < (int)g[u].size();x ++){ if(!dead[g[u][x]]){ build(g[u][x] , u , w[u][x]); } } for(int x = 0;x < (int)g[u].size();x ++){ if(!dead[g[u][x]]){ solve(g[u][x]); } } } void Init(int N , int A[] , int B[] , int D[]){ for(int i = 0;i < N - 1;i ++){ ++A[i] , ++B[i]; g[A[i]].push_back(B[i]); w[A[i]].push_back(D[i]); g[B[i]].push_back(A[i]); w[B[i]].push_back(D[i]); } for(int i = 1;i <= N;i ++){ dead[i] = 0; e[i] = oo; } solve(1); } long long Query(int S , int X[] , int T , int Y[]){ long long ans = oo; for(int i = 0;i < T;i ++){ ++Y[i]; e[Y[i]] = 0; for(auto [u , D] : ups[Y[i]]){ e[u] = min(e[u] , D); } } for(int i = 0;i < S;i ++){ ++X[i]; ans = min(ans , e[X[i]]); for(auto [u , D] : ups[X[i]]){ ans = min(ans , e[u] + D); } } for(int i = 0;i < T;i ++){ e[Y[i]] = oo; for(auto [u , D] : ups[Y[i]]){ e[u] = oo; } } return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...