Banner Image

Familystrokes | 338.

cout << internalCnt + horizontalCnt << '\n'; return 0;

1 if childCnt(v) = 1 2 if childCnt(v) ≥ 2 0 if childCnt(v) = 0 Proof. Directly from Lemma 2 (vertical) and Lemma 3 (horizontal). ∎ answer = internalCnt + horizontalCnt computed by the algorithm equals the minimum number of strokes needed to draw the whole tree.

Proof. If childCnt ≥ 2 : the children occupy at least two columns on the next row, so a horizontal line is needed to connect the leftmost to the rightmost child (rule 2). 338. FamilyStrokes

import sys sys.setrecursionlimit(200000)

Both bounds comfortably meet the limits for N ≤ 10⁵ . Below are clean, self‑contained implementations in C++17 and Python 3 that follow the algorithm exactly. 6.1 C++17 #include <bits/stdc++.h> using namespace std; Below are clean

internalCnt ← 0 // |I| horizontalCnt ← 0 // # childCount(v) ≥ 2

int main() long long horizontalCnt = 0; // # v using namespace std

if childCnt > 0: // v has at least one child → internal internalCnt += 1 if childCnt >= 2: horizontalCnt += 1

root = 1 stack = [(root, 0)] # (node, parent) internal = 0 horizontal = 0



icon-Google Play
icon-instagramicon-xicon-facebookicon-tiktok