/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 5ms 5.023 MiB
#2 Accepted 5ms 4.781 MiB
#3 Accepted 5ms 4.973 MiB
#4 Accepted 7ms 8.004 MiB
#5 Accepted 5ms 5.086 MiB
#6 Accepted 5ms 6.777 MiB
#7 Accepted 13ms 7.965 MiB
#8 Accepted 11ms 8.945 MiB
#9 Accepted 10ms 5.391 MiB
#10 Accepted 11ms 8.605 MiB
#11 Accepted 172ms 24.586 MiB
#12 Accepted 190ms 24.668 MiB
#13 Accepted 188ms 24.672 MiB
#14 Accepted 160ms 24.535 MiB
#15 Accepted 89ms 13.867 MiB
#16 Accepted 65ms 9.727 MiB
#17 Accepted 71ms 11.754 MiB
#18 Accepted 71ms 13.754 MiB
#19 Accepted 74ms 11.73 MiB
#20 Accepted 95ms 9.613 MiB

Code

/*
 *   Copyright (c) 2024 Emon Thakur
 *   All rights reserved.
 */
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using minheap = priority_queue<long long, vector<long long>, greater<long long>>;
typedef tree<int, null_type, greater_equal<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key

#define ll long long
#define ld long double
#define MOD 1000000007
#define pie 2*(acos(0.0))
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define pb push_back
#define endl '\n'
#define lcm(a,b) (a*b)/(__gcd<ll>(a,b))
#define print(v) for(auto e:v) cout<<e<<" "; cout<<endl;
#define printp(v) for(auto e:v) cout<<e.first<<" "<<e.second<<endl;
#define srt(v) sort(v.begin(),v.end())
#define rsrt(v) sort(v.rbegin(),v.rend())
#define life_is_a_race ios::sync_with_stdio(false); cin.tie(nullptr);
const int N = 1e5 + 10;
vector<pair<int,int>>g[N];
int table[N + 1][22];
int level[N];
int tin[N],tout[N];
int maxLen[N + 1][22];
int n, lg, Time = 0, INF = 1e9 + 10;

void dfs(int v, int par = -1, int dep = 0,int mx = -1){
    // cout << v << endl;
    tin[v] = ++Time;
    table[v][0] = par;
    level[v] = dep;
    maxLen[v][0] = mx;
    for(int i = 1; i <= lg; i++){
        if(table[v][i - 1] != -1){
            table[v][i] = table[table[v][i - 1]][i - 1];
            maxLen[v][i] = max(maxLen[v][i - 1], maxLen[table[v][i - 1]][i - 1]);
        }
    }

    for(auto &child : g[v]){
        if(child.first == par)continue;

        dfs(child.first, v, dep + 1, child.second);
    }
    tout[v] = ++Time;
}

int lca_query(int a, int b){

    if(level[a] < level[b])swap(a, b);

    int dis = level[a] - level[b];
    int mx = 0;
    while(dis) {
        int i = log2l(dis);
        mx = max(mx, maxLen[a][i]);
        a = table[a][i], dis -= (1 << i);
    }
    if(a == b) return mx;

    for(int i = lg; i >= 0; i--){
        if(table[a][i] != -1  && table[a][i] != table[b][i]){
            mx = max(mx, maxLen[a][i]);
            mx = max(mx, maxLen[b][i]);
            a = table[a][i], b = table[b][i];
        }
    }
    mx = max(mx, maxLen[a][0]);
    mx = max(mx, maxLen[b][0]);
    return mx;
}
void solve(int tc)
{
    //cout<<"Case "<<tc<<": ";
    int n, q;
    cin >> n >> q;
    lg = log2l(n) + 1;
    for(int i = 0; i <= n; i++){
        for(int j = 0; j <= lg; j++){
            table[i][j] = -1;
            maxLen[i][j] = -1;
        }
    }
    for(int i = 1; i < n; i++){
        int u, v, w;
        cin >> u >> v >> w;
        g[u].push_back({v, w});
        g[v].push_back({u, w});
    }
    dfs(1);
    while(q--){
        int a, b;
        cin >> a >> b;
        cout << lca_query(a, b) << endl;
    }
    
}
int main()
{
    life_is_a_race
    int t=1; 
    // cin>>t;
    for(int i=1;i<=t;i++) solve(i);
}

Information

Submit By
Type
Submission
Problem
P1019 Graph Stuff
Contest
Sylhet ICPC 2024 Collaborative Challenge: Episode 2
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-30 10:53:04
Judged At
2024-11-11 02:35:03
Judged By
Score
100
Total Time
190ms
Peak Memory
24.672 MiB