Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Hints: Graph Cutting (Three Vertices, Any Size)

Triplet version of E0. You know how side branches work for a path core; here the mandatory core is the Steiner tree on $\{a,b,c\}$.

On the star ($1$ connected to $2,3,4$), list connected sets containing $1,2,3$. How many? What about sets containing $2,3,4$?
Answer to Hint 1: For $\{1,2,3\}$: $\{1,2,3\}$ and $\{1,2,3,4\}$ — two sets. For $\{2,3,4\}$: only the whole tree $\{1,2,3,4\}$ — one set.
For any three vertices in a tree, what is the smallest connected set containing them? Is it unique? How is it built from the three pairwise paths?
Answer to Hint 3: The Steiner tree $T(a,b,c)$ is unique: take the union of paths $P(a,b)$, $P(b,c)$, and $P(a,c)$. Either the three lie on one path (then $T$ is a path), or $T$ is a “tripod” — one center vertex with three disjoint branches to $a,b,c$.
If $S$ is connected and contains $a,b,c$, what is the relation between $T(a,b,c)$ and $S$?
Answer to Hint 5: $T(a,b,c)\subseteq S$, exactly as $P(a,b)\subseteq S$ in E0. Extra vertices can only come from side branches hanging off $T(a,b,c)$.
Mark all vertices on $T(a,b,c)$ (mark each of the three pairwise paths). At a vertex $v\in T$, a neighbor $x\notin T$ starts a side branch. How many connected choices in that branch, if we must include $x$ when we use the branch at all?
Answer to Hint 7: Same as E0: $\mathrm{ways}(x)=\prod_{y}(1+\mathrm{ways}(y))$ on the side subtree. Each branch independently contributes factor $(1+\mathrm{ways}(x))$.
Write the full answer as a product over vertices of $T(a,b,c)$ and their side neighbors — mirror the E0 formula, replacing $P(a,b)$ by $T(a,b,c)$.

Answer to Hint 9:

$$\text{answer}=\prod_{\substack{v\in T(a,b,c)\\ x\in N(v)\setminus T}}\bigl(1+\mathrm{ways}(x)\bigr).$$

Mark $T$ by union of three paths; run side DP on non-$T$ neighbors. $O(n)$ per test case. Next: track sizes in E4.