Submission #626938

# Submission time Handle Problem Language Result Execution time Memory
626938 2022-08-12T02:23:06 Z Glory Paths (RMI21_paths) C++17
0 / 100
417 ms 30776 KB
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,k,res=0,l[100005],parent[100005], passed[100005], dist[100005];
int sparse_table[100005][25];
vector<pair<int,int>> gr[100005];
pair<int,int> a;
 
void dfs(int x,int par,int d,int lh) {
    sparse_table[x][0] = par;
    l[x] = lh;
    dist[x] = d;
    for (auto v: gr[x]){
        if (v.first != par){
            dfs(v.first, x, d+v.second,lh+1);
        }
    }
}
 
void dfs_1(int x,int par,int d) {
    a = max(a, {d,x});
    for(auto v: gr[x]){
        if(v.first != par){
            dfs_1(v.first, x, d+v.second);
        }
    }
}

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});
    }
    
    memset(passed,0,sizeof(passed));
    memset(parent,0,sizeof(parent));
    
    a = {-100,-100};
    dfs_1(1,-1,0);
    int pa = a.second;
    
    memset(passed,0,sizeof(passed));
    
    a = {-100,-100};
    dfs_1(pa,-1,0);
    int pb = a.second;
    
    
    dfs(1,-1,0,0);
    preprocess_lca();
    
    for(int i = 1;i <= n; i++){
       cout<<max(l[i]+l[pa]-l[lca(i,pa)]*2, l[i]+l[pb]-l[lca(i,pb)]*2)<<endl;
    }
}
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 4180 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 4180 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 4180 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 4180 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 417 ms 30776 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 4180 KB Output isn't correct
2 Halted 0 ms 0 KB -