Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ret2home/Library

:warning: structure/HLD.cpp

Depends on

Code

#pragma once
#include "../template/template.cpp"

struct HLD{
    vector<vector<int>>G;
    vector<int>sz,in,out,top,par,depth;
    void dfs_sz(int x,int p){
        sz[x]=1;
        int mx=0;
        for(int &i:G[x])if(i!=p){
            dfs_sz(i,x);
            sz[x]+=sz[i];
            if(chmax(mx,sz[i]))swap(G[x][0],i);
        }
    }
    int t=0;
    void dfs_hld(int x,int p){
        in[x]=t++;
        for(int i:G[x])if(i!=p){
            if(i!=G[x][0]){
                par[i]=x;
                top[i]=i;
                depth[i]=depth[x]+1;
            }else {
                par[i]=par[x];
                top[i]=top[x];
                depth[i]=depth[x];
            }
            dfs_hld(i,x);
        }
        out[x]=t;
    }
    HLD(vector<vector<int>>&G):G(G){
        int N=len(G);
        sz.resize(N);
        in.resize(N);out.resize(N);
        par.resize(N);top.resize(N);depth.resize(N);
        dfs_sz(0,0);dfs_hld(0,0);
    }
};
#line 2 "template/template.cpp"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define REV(i, n) for (int i = n - 1; i > 0; i--)
#define all(v) v.begin(), v.end()
#define PL pair<ll, ll>
#define PI pair<int, int>
#define pi acos(-1)
#define len(s) (int)s.size()
#define compress(v) \
    sort(all(v));   \
    v.erase(unique(all(v)), v.end());
#define comid(v, x) lower_bound(all(v), x) - v.begin()

template<class T>
using prique=priority_queue<T,vector<T>,greater<>>;

template <class T, class U>
inline bool chmin(T &a, U b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T, class U>
inline bool chmax(T &a, U b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
constexpr ll inf = 3e18;
#line 3 "structure/HLD.cpp"

struct HLD{
    vector<vector<int>>G;
    vector<int>sz,in,out,top,par,depth;
    void dfs_sz(int x,int p){
        sz[x]=1;
        int mx=0;
        for(int &i:G[x])if(i!=p){
            dfs_sz(i,x);
            sz[x]+=sz[i];
            if(chmax(mx,sz[i]))swap(G[x][0],i);
        }
    }
    int t=0;
    void dfs_hld(int x,int p){
        in[x]=t++;
        for(int i:G[x])if(i!=p){
            if(i!=G[x][0]){
                par[i]=x;
                top[i]=i;
                depth[i]=depth[x]+1;
            }else {
                par[i]=par[x];
                top[i]=top[x];
                depth[i]=depth[x];
            }
            dfs_hld(i,x);
        }
        out[x]=t;
    }
    HLD(vector<vector<int>>&G):G(G){
        int N=len(G);
        sz.resize(N);
        in.resize(N);out.resize(N);
        par.resize(N);top.resize(N);depth.resize(N);
        dfs_sz(0,0);dfs_hld(0,0);
    }
};
Back to top page