Skip to content

Commit

Permalink
update centroid_decomposition.md
Browse files Browse the repository at this point in the history
refactor cf_321_c
  • Loading branch information
idat50me committed Jul 16, 2024
1 parent ca50165 commit 845c564
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
5 changes: 4 additions & 1 deletion graph/docs/centroid_decomposition.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ documentation_of: ../centroid_decomposition.cpp
- `get(x, tsize)`:$O(m)$
- `del(x)`:$O(1)$

## 備考
## 重心分解の再帰について
重心分解後の各部分木について,`get()` の返り値に含まれる頂点とサイズの情報を用いることで再帰的に重心分解を繰り返すことができる.

1レイヤーの重心分解につき部分木の最大サイズは半分以下となるため,頂点数1の木になるまで重心分解したときのレイヤー数は $O(\log n)$ である.
また,`del(x)` で重心を論理削除すれば `E` および `subsizes` を再初期化する必要はないため,1つのインスタンスを使い回すことができる.
これは全体 $O(n \log n)$ で実行することができる.

実装例については `atc_abc291_ex``cf_321_c` を参照されたい.


# 参考
- https://qiita.com/drken/items/4b4c3f1824339b090202
32 changes: 11 additions & 21 deletions test/cf_321_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using namespace std;
#endif

#include "structure/2d_array.cpp"
#include "graph/centroid_decomposition.cpp"

int main() {
Expand All @@ -26,30 +25,21 @@ int main() {
}

vector<char> ans(N);
char c = 'A';
queue<int> q, qs;
queue<tuple<int, int, char>> q;
auto cd = centroid_decomposition(E);
q.push(0);
qs.push(N);
q.emplace(0, N, 'A');
while(not q.empty()) {
queue<int> q2, qs2;
while(not q.empty()) {
int x = q.front(), xs = qs.front();
q.pop(), qs.pop();
auto [x, xs, c] = q.front();
q.pop();

auto res = cd.get(x, xs);
int centroid = res.first;
auto v = res.second;
ans[centroid] = c;
cd.del(centroid);
for(int i = 0; i < v.size(); i++) {
q2.push(v[i].first);
qs2.push(v[i].second);
}
auto res = cd.get(x, xs);
int centroid = res.first;
auto v = res.second;
ans[centroid] = c;
cd.del(centroid);
for(int i = 0; i < v.size(); i++) {
q.emplace(v[i].first, v[i].second, c + 1);
}
swap(q, q2);
swap(qs, qs2);
c++;
}

for(int i = 0; i < N; i++) cout << ans[i] << (i < N - 1 ? ' ' : '\n');
Expand Down
2 changes: 1 addition & 1 deletion test/docs/cf_321_c.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ documentation_of: ../cf_321_c.cpp
---

## Submission page
- [Submission #270800426 - Codeforces](https://codeforces.com/contest/321/submission/270800426)
- [Submission #270844198 - Codeforces](https://codeforces.com/contest/321/submission/270844198)

0 comments on commit 845c564

Please sign in to comment.