🏆 Luyện bài tập & THI THỬ MIỄN PHÍ HÀNG TUẦN tại oj.vietnamson.com — HSG Tin 9 · Tuyển sinh 10 chuyên Tin · HSG THPT · Quốc gia · Tin học trẻ · chấm tự động, có bảng xếp hạng & lời giải → Vào thi ngay! 🏆 THI THỬ TIN HỌC MIỄN PHÍ MỖI TUẦN tại oj.vietnamson.com → Tuyển sinh 🎓 Mình nhận đào tạo lập trình giải thuật thi đấu: thi vào lớp 10 chuyên tin · thi học sinh giỏi tin học cấp tỉnh/thành · thi vào đội tuyển quốc gia · Olympic tin học sinh viên / ICPC · phỏng vấn thuật toán. Bấm vào đây để xem thông tin khóa học → Xem khóa học

VIETNAMSON OJ · THI THỬ HÀNG TUẦN

Tuần này có kỳ thi thử

Thi như thật, chấm tự động, có lời giải chi tiết ngay sau giờ thi. Miễn phí cho mọi người. Đăng ký trước, rủ bạn bè cùng vào thử sức nhé!

Các đạo hữu, cùng nhập trận nào!
Chủ đề mới: Tu TiênMỗi đề là một chặng tu luyện: trận pháp, đan dược, bí cảnh, thiên thê…
Độ khó đã được giảmTheo góp ý của các bạn, đề vừa sức hơn so với 3 tuần trước
Tính điểm và xếp hạngKỳ thi nào cũng có bảng xếp hạng: biết ngay mình ở cảnh giới nào
Đăng ký trước từ bây giờ: đến giờ chỉ việc vào làm bài!Chưa biết cách tham gia? Xem hướng dẫn

ICPC Miền Nam 2026-2027 - Bài G: Black Start

Nguồn: The 2026 ICPC Vietnam Southern Provincial Programming Contest (ICPC Việt Nam vòng miền Nam 2026) – Problem GĐộ khó: Khó

Đề 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.
Ví dụ / Sample 1
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
2
Ví dụ / Sample 2
Input
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.

Em đã mua gói thì đăng nhập để xem. Chưa có gói thì bấm Xem giới thiệu gói để biết gói gồm những đề nào, bao nhiêu bài, học phí và ưu đãi; muốn đăng ký thì nhắn Zalo thầy Sơn.
Tự học thuật toán: khó ở đâu?
Đọc xong một bài giải là bước đầu. Phần lớn học sinh tự học sẽ gặp những trở ngại sau:
  • 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.
Học cùng thầy Nam Sơn, các trở ngại đó được gỡ bỏ:
  • 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.