Submission #626932

#TimeUsernameProblemLanguageResultExecution timeMemory
626932GloryPaths (RMI21_paths)C++17
0 / 100
348 ms32872 KiB
#include<bits/stdc++.h> #define int long long using namespace std; int n,k,res=0,l[100005],parent[100005], passed[100005]; int sparse_table[100005][25]; vector<pair<int,int>> gr[100005]; pair<int,int> a,b; void dfs(int u){ for (auto [v,w] : gr[u]){ if(parent[v] == 0){ parent[v] = u; l[v] = l[u] + w; dfs(v); } } } void dfs_1(int u,int d){ a = max(a, make_pair(d,u)); for(auto [v,w] : gr[u]){ if (!passed[v]){ passed[v] = 1; dfs_1(v, w+d); } } } void preprocess_lca() { for (int i = 1; i <= n; i++){ sparse_table[i][0] = parent[i]; } for (int k = 1; k <= 20; k++){ for (int i = 1; i <= n; i++){ sparse_table[i][k] = sparse_table[sparse_table[i][k - 1]][k - 1]; } } } int lca(int p, int q) { for (int i = 20; i >= 0; i--){ if (l[sparse_table[p][i]] >= l[q]){ p = sparse_table[p][i]; } } for (int i = 20; i >= 0; i--){ if (l[sparse_table[q][i]] >= l[p]){ q = sparse_table[q][i]; } } if (p == q){ return p; } for (int i = 20; i >= 0; i--){ if (sparse_table[p][i] != sparse_table[q][i]) { p = sparse_table[p][i]; q = sparse_table[q][i]; } } return parent[p]; } int32_t main(){ cin>>n>>k; for(int i = 1;i < n; i++){ int u,v,w; cin>>u>>v>>w; gr[u].push_back({v,w}); gr[v].push_back({u,w}); } a = {-1,-1}; dfs(1); preprocess_lca(); dfs_1(1,0); for(int i = 1;i <= n; i++){ cout<<l[i]+l[a.second]-l[lca(a.second,i)]*2<<endl; } }
#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...