ICPC Miền Nam 2026-2027 - Bài G: Black Start
Đề bài
G. Black Start
Standard I/O | Time Limit: 4s | Memory Limit: 1GB
(Python on oj.vietnamson.com: 10s, 1GB)
A storm has knocked out the regional power grid, and every substation is dark. The grid consists of $n$ substations, numbered from $1$ to $n$, and $n - 1$ transmission lines. Each line connects two different substations and has a positive integer length. Every substation can be reached from every other one along the lines, so the lines form a tree. The distance between two substations is the total length of the lines on the unique route between them.
Substation $i$ is equipped with a booster of range $r_i$. As soon as substation $i$ is energized, it energizes every substation whose distance from substation $i$ is at most $r_i$. The newly energized substations energize further substations by the same rule, and so on, until no new substation can be energized. In particular, a substation with range $0$ energizes no other substation.
To restart the grid, the operators switch on the emergency generators of some substations, which energizes these substations at the very beginning. Find the minimum number of substations whose generators must be switched on so that, in the end, every substation is energized.
Input
- The first line contains a single integer $n$ $(1 \le n \le 3 \cdot 10^5)$ – the number of substations.
- The second line contains $n$ integers $r_1, r_2, \ldots, r_n$ $(0 \le r_i \le 10^9)$ – the ranges of the boosters.
- Each of the next $n - 1$ lines contains three integers $u$, $v$ and $\ell$ $(1 \le u, v \le n, u \ne v, 1 \le \ell \le 10^9)$, describing a transmission line of length $\ell$ between substations $u$ and $v$. It is guaranteed that the lines form a tree.
Output
Print a single integer – the minimum number of substations whose generators must be switched on.
Notes
In the first example, switch on the generators of substations $1$ and $7$. Substation $7$ energizes substation $6$ (at distance $1$) and substation $4$ (at distance exactly $7$). Substation $4$ energizes substations $2$ and $5$, and substation $2$ energizes substation $3$. No substation can energize substation $1$ or substation $7$; for example, substation $2$ is at distance $4$ from substation $1$, but its range is only $3$. So both generators are necessary.
In the second example, substations $1$ and $2$ energize each other, so a generator at either of them energizes substations $1$, $2$ and $3$. Substation $4$ is at distance $1$ from substation $3$, but the range of substation $3$ is $0$, so substation $4$ needs its own generator.
Note (VIETNAMSON)
- Problem of The 2026 ICPC Vietnam Southern Provincial Programming Contest. The statement and samples are kept verbatim from the original PDF.
- Python on oj.vietnamson.com: time limit 10 s, memory limit 1 GB.
- Pure Python cannot pass this time limit (as in the real contest); the Python code is for reference - use C++ in contests.
Input
7
4 3 0 5 2 0 7
1 2 4
2 3 3
2 4 5
4 5 2
4 6 6
6 7 1
Output
2Input
4
10 10 0 0
1 2 10
2 3 10
3 4 1
Output
2Ý tưởng và lời giải chi tiết
Phần phân tích ý tưởng, các cách giải từ cơ bản đến tối ưu kèm code C++/Python và chấm thử trên VIETNAMSON OJ dành cho học viên đã đăng ký gói.
- Không biết học gì trước, học gì sau, học lan man nên kiến thức bị hổng.
- Bí bài không ai gỡ, ngồi cả buổi vẫn không ra rồi bỏ cuộc.
- Code chạy được nhưng không đủ điểm, không biết sai ở đâu, chậm ở đâu.
- Không có đề thi thử và người chấm, nên không biết mình đang ở mức nào.
- Dễ nản giữa chừng khi không có ai theo sát.
- Lộ trình 111 buổi video sắp sẵn từ dòng code đầu tiên đến đề HSG thật của các tỉnh.
- Thầy trực tiếp gỡ bí, chữa bài cho từng em qua nhóm Zalo riêng.
- Hệ thống chấm bài tự động 24/7 và thi thử HSG hằng tuần để biết chính xác mình đang ở đâu.
- Đóng học phí một lần, thầy kèm lâu dài: từ thi chuyên Tin đến HSG quốc gia và cả khi lên đại học.
