teamnote default
This commit is contained in:
40
source/Tree/HLD.cpp
Normal file
40
source/Tree/HLD.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
BOJ 트리와 쿼리 1
|
||||
1 i c: i번 간선의 비용을 c로 바꾼다.
|
||||
2 u v: u에서 v로 가는 단순 경로에 존재하는 비용 중에서 가장 큰 것을 출력한다.
|
||||
|
||||
vi adj[N]; int par[N]; int sz[N]; int d[N];
|
||||
void dfs1(int s) {
|
||||
sz[s] = 1;
|
||||
for(auto it=adj[s].begin(); it!=adj[s].end(); it++)
|
||||
if(*it == par[s]) {adj[s].erase(it); break;}
|
||||
for(auto &i : adj[s]) {
|
||||
par[i] = s; d[i] = d[s] + 1;
|
||||
dfs1(i); sz[s] += sz[i];
|
||||
if(sz[i] > sz[adj[s][0]]) swap(adj[s][0], i);
|
||||
}
|
||||
}
|
||||
int in[N], c; int top[N];
|
||||
void dfs2(int s) {
|
||||
in[s] = ++c;
|
||||
for(auto i : adj[s]) {
|
||||
if(i == adj[s].front()) top[i] = top[s];
|
||||
else top[i] = i;
|
||||
dfs2(i);
|
||||
}
|
||||
}
|
||||
|
||||
int query(int a,int b) {
|
||||
int ans = 0;
|
||||
while(top[a] != top[b]) {
|
||||
if(d[top[a]] > d[top[b]]) swap(a, b);
|
||||
ans = max(ans, query(root, in[top[b]], in[b]));
|
||||
b = par[top[b]];
|
||||
}
|
||||
if(d[a] > d[b]) swap(a, b);
|
||||
return max(ans, query(root, in[a]+1, in[b]));
|
||||
}
|
||||
|
||||
// dfs1(1); dfs2(1);
|
||||
// forr(i, n) arr[in[i]] = m[{par[i], i}]; init(root, arr);
|
||||
// q == 1: update(root, in[a], c); // in[a]를 c로 대체
|
||||
// q == 2: query(a, b);
|
||||
Reference in New Issue
Block a user