/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 6.527 MiB
#2 Accepted 4ms 6.746 MiB
#3 Accepted 5ms 7.984 MiB
#4 Accepted 4ms 7.273 MiB
#5 Accepted 4ms 5.02 MiB
#6 Accepted 4ms 5.035 MiB
#7 Accepted 7ms 8.57 MiB
#8 Accepted 7ms 8.953 MiB
#9 Accepted 7ms 8.777 MiB
#10 Accepted 7ms 8.527 MiB
#11 Accepted 115ms 24.637 MiB
#12 Accepted 112ms 24.539 MiB
#13 Accepted 106ms 24.52 MiB
#14 Accepted 105ms 24.57 MiB
#15 Accepted 39ms 9.77 MiB
#16 Accepted 44ms 14.035 MiB
#17 Accepted 49ms 11.707 MiB
#18 Accepted 45ms 11.793 MiB
#19 Accepted 44ms 11.773 MiB
#20 Accepted 39ms 13.852 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-10-30 10:53:04
Judged By
Score
100
Total Time
115ms
Peak Memory
24.637 MiB