Centroid Decomposition
Authors: Siyong Huang, Benjamin Qi
Prerequisites
Decomposing a tree to faciliate path computations.
Introduction
Focus Problem – read through this problem before continuing!
Centroid Decomposition is a divide and conquer technique for trees. The centroid of a tree is a node which, if rooted, results in no other nodes having a subtree of size greater than . Centroid Decomposition works by repeated splitting the tree and each of the resulting subgraphs at the centroid, producing layers of subgraphs.
Resources | |||
---|---|---|---|
Carpanese | how to solve above problem | ||
CF | blog + video for above problem. LCA isn't necessary though. | ||
GFG |
Implementation
General centroid code is shown below.
This section is not complete.
Ben - this is not easy to understand :/
C++
1bool r[MN];//removed2int s[MN];//subtree size3int dfs(int n, int p = 0)4{5 s[n] = 1;6 for(int x : a[n])7 if(x != p && !r[x])8 s[n] += dfs(x, n);9 return s[n];10}
Java
1boolean[] r = new boolean[MN];//removed2int[] s = new int[MN];//subtree size3public int dfs(int n, int p)4{5 s[n] = 1;6 for(int x : a[n])7 if(x != p && !r[x])8 s[n] += dfs(x, n);9 return s[n];10}
Solution - Xenia & Tree
1#include <cstdio>2#include <cstring>3#include <vector>45template<typename T> bool ckmin(T& a, const T& b){return b<a?a=b,1:0;}67const int MN = 1e5+10, INF = 0x3f3f3f3f;89int N, M, s[MN], m[MN][2], t, b, d;10bool r[MN], red[MN];
Problems
Status | Source | Problem Name | Difficulty | Tags | Solution | URL |
---|---|---|---|---|---|---|
CF | Easy | Show TagsCentroid | Check CF | |||
CF | Easy | Show TagsCentroid | Check CF | |||
Plat | Normal | Show TagsCentroid | External Sol | |||
CF | Normal | Show TagsCentroid | Check CF | |||
CF | Normal | Show TagsCentroid, NT | Check CF | |||
CF | Normal | Show TagsCentroid, DP | Check CF | |||
JOI | Normal | Show TagsCentroid | External Sol | |||
DMOJ | Hard | Show TagsCentroid | ||||
DMOJ | Hard | Show TagsCentroid | ||||
JOI | Hard | Show TagsCentroid, Small to Large | ||||
Plat | Very Hard | Show TagsCentroid | External Sol |
Module Progress:
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!