Skip to content

Commit af57156

Browse files
committed
rename existing class as preorder_tree_dfs
1 parent 01d9f22 commit af57156

File tree

5 files changed

+102
-40
lines changed

5 files changed

+102
-40
lines changed

tree/eulertour.hpp

Lines changed: 0 additions & 30 deletions
This file was deleted.

tree/preorder_tree_dfs.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#pragma once
2+
#include <cassert>
3+
#include <vector>
4+
5+
// Preorder tree DFS
6+
// 木を DFS して行きがけ順に頂点を保持する.
7+
// 木を区間に変換する.部分木を構成する頂点は連続して現れるので,部分木の頂点クエリ等に有用.
8+
// heavy_light_decomposition が上位互換
9+
struct preorder_tree_dfs {
10+
int n; // # of vertices of tree
11+
int root;
12+
std::vector<int> subtree_begin, subtree_end;
13+
std::vector<int> vis_order;
14+
15+
void _build_dfs(int now, const std::vector<std::vector<int>> &to) {
16+
subtree_begin[now] = vis_order.size();
17+
vis_order.push_back(now);
18+
for (auto nxt : to[now]) {
19+
if (subtree_begin[nxt] == -1) _build_dfs(nxt, to);
20+
}
21+
subtree_end[now] = vis_order.size();
22+
}
23+
24+
preorder_tree_dfs() = default;
25+
26+
preorder_tree_dfs(const std::vector<std::vector<int>> &to, int root)
27+
: n(to.size()), root(root), subtree_begin(n, -1), subtree_end(n, -1) {
28+
_build_dfs(root, to);
29+
}
30+
};

tree/preorder_tree_dfs.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
title: Preorder tree DFS (木の行きがけ順 DFS)
3+
documentation_of: ./preorder_tree_dfs.hpp
4+
---
5+
6+
与えられた木に対して,行きがけ順に頂点を列挙した列を構築する.部分木を構成する頂点は連続して現れるので,部分木の頂点クエリ等に有用.
7+
8+
## 使用方法
9+
10+
```cpp
11+
int N;
12+
vector<vector<int>> to(N);
13+
int root;
14+
15+
preorder_tree_dfs tour(to, root);
16+
17+
// 頂点 v の部分木を構成する頂点は半開区間 [tour.subtree_begin[v], tour.subtree_end[v]) に現れる
18+
```
19+
20+
## 問題例
21+
22+
- [Library Checker: Vertex Add Subtree Sum](https://judge.yosupo.jp/problem/vertex_add_subtree_sum)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#define PROBLEM "https://judge.yosupo.jp/problem/vertex_add_subtree_sum"
2+
#include "../preorder_tree_dfs.hpp"
3+
#include <cassert>
4+
#include <iostream>
5+
#include <atcoder/fenwicktree>
6+
using namespace std;
7+
8+
int main() {
9+
int N, Q;
10+
cin >> N >> Q;
11+
vector<long long> A(N);
12+
vector<vector<int>> to(N);
13+
for (auto &a : A) cin >> a;
14+
for (int i = 1; i < N; i++) {
15+
int p;
16+
cin >> p;
17+
to.at(p).push_back(i);
18+
to.at(i).push_back(p);
19+
}
20+
21+
preorder_tree_dfs tour(to, 0);
22+
23+
atcoder::fenwick_tree<long long> ft(N);
24+
for (int i = 0; i < N; i++) ft.add(tour.subtree_begin.at(i), A.at(i));
25+
26+
while (Q--) {
27+
int q;
28+
cin >> q;
29+
if (q) {
30+
int u;
31+
cin >> u;
32+
printf("%lld\n", ft.sum(tour.subtree_begin[u], tour.subtree_end[u]));
33+
} else {
34+
int u, x;
35+
cin >> u >> x;
36+
ft.add(tour.subtree_begin[u], x);
37+
}
38+
}
39+
}
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
1-
#include "../../segmenttree/point-update-range-get_nonrecursive.hpp"
2-
#include "../eulertour.hpp"
31
#define PROBLEM "https://judge.yosupo.jp/problem/vertex_add_subtree_sum"
2+
#include "../heavy_light_decomposition.hpp"
43
#include <cassert>
54
#include <iostream>
5+
#include <atcoder/fenwicktree>
66
using namespace std;
77

88
int main() {
99
int N, Q;
1010
cin >> N >> Q;
1111
vector<long long> A(N);
12-
vector<vector<int>> to(N);
12+
vector<pair<int, int>> edges;
1313
for (auto &a : A) cin >> a;
1414
for (int i = 1; i < N; i++) {
1515
int p;
1616
cin >> p;
17-
to[p].push_back(i);
18-
to[i].push_back(p);
17+
edges.emplace_back(p, i);
1918
}
2019

21-
PreorderEulerTour tour(to, 0);
20+
heavy_light_decomposition tour(N, edges);
21+
tour.build();
22+
2223
vector<long long> v;
2324
for (auto i : tour.vis_order) v.push_back(A[i]);
2425
assert(int(v.size()) == N);
25-
PointUpdateRangeSum<long long> rsq(v, 0);
26+
atcoder::fenwick_tree<long long> ft(N);
27+
for (int i = 0; i < N; i++) ft.add(i, v[i]);
2628

2729
while (Q--) {
2830
int q;
2931
cin >> q;
3032
if (q) {
3133
int u;
3234
cin >> u;
33-
printf("%lld\n", rsq.get(tour.subtree_begin[u], tour.subtree_end[u]));
35+
printf("%lld\n", ft.sum(tour.subtree_begin[u], tour.subtree_end[u]));
3436
} else {
3537
int u, x;
3638
cin >> u >> x;
37-
A[u] += x;
38-
rsq.update(tour.subtree_begin[u], A[u]);
39+
ft.add(tour.subtree_begin[u], x);
3940
}
4041
}
4142
}

0 commit comments

Comments
 (0)