Submission #626952

# Submission time Handle Problem Language Result Execution time Memory
626952 2022-08-12T02:50:02 Z Glory Paths (RMI21_paths) C++17
12 / 100
471 ms 32684 KB
#include<bits/stdc++.h>
using namespace std;
int n,k,res=0,l[100005];
long long d[100005];
int parent[100005];
int sparse_table[100005][25];
vector<pair<int,int>> gr[100005];
pair<long long,int> a;
map<pair<int,int>,long long> mp;
set<pair<int,int>> path;
void dfs(int u,int par,long long dh,int lh) {
    parent[u]=par;
    sparse_table[u][0] = par;
    l[u] = lh;
    d[u] = dh;
    for (auto [v,w]: gr[u]){
        if(v != par){
            dfs(v,u,w+dh,lh+1);
        }
    }
}
 
void dfs_1(int u,int par,long long dh) {
    a = max(a, {dh,u});
    for(auto [v,w]: gr[u]){
        if(v != par){
            dfs_1(v, u, w+dh);
        }
    }
}

void preprocess_lca() {
    
 
    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];
}


void sub_5(){
    a = {-100,-100};
    dfs_1(1,-1,0);
    int pa = a.second;
    
    a = {-100,-100};
    dfs_1(pa,-1,0);
    int pb = a.second;
    
    
    dfs(1,-1,0ll,0);
    preprocess_lca();
    
    for(int i = 1;i <= n; i++){
       cout<<max(d[i]+d[pa]-d[lca(i,pa)]*2, d[i]+d[pb]-d[lca(i,pb)]*2)<<endl;
    }
}


void sub_1(){
    
}
    
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});
        mp[{u,v}] = w;
        mp[{v,u}] = w;
    }
    
    if(k == 1)sub_5();
    else sub_1();
}


# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 469 ms 31344 KB Output is correct
2 Correct 469 ms 32684 KB Output is correct
3 Correct 387 ms 31052 KB Output is correct
4 Correct 417 ms 31300 KB Output is correct
5 Correct 471 ms 32152 KB Output is correct
6 Correct 401 ms 31208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -