C - Chain of Infection Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 366

問題文

高橋君は N 台のコンピュータと 1 台の中央サーバからなるネットワークの管理者です。N 台のコンピュータには 1 から N までの番号が、中央サーバにはコンピュータ 0 という番号が付けられています。すなわち、ネットワーク全体はコンピュータ 0, 1, \ldots, N の合計 N + 1 台で構成されます。

各コンピュータ i1 \leq i \leq N)は、親コンピュータ P_i0 \leq P_i \leq N, P_i \neq i)に直接接続されています。これらの接続関係は、中央サーバ(コンピュータ 0)を根とする N + 1 頂点の木構造を成しています。この木構造において、コンピュータ v子コンピュータとは、P_j = v を満たすコンピュータ j のことを指します。

ある日、ネットワークにウイルスが侵入しました。各コンピュータ i1 \leq i \leq N)には脆弱性値 D_i が設定されています。

ウイルスの感染は、コンピュータ 1 から N のみを対象とします。中央サーバ(コンピュータ 0)は感染の対象外であり、感染することはありません。感染は以下のルールで連鎖的に広がります。

  1. 初期感染: 脆弱性値が正(D_i > 0)であるコンピュータは全て感染します。脆弱性値が 0 以下(D_i \leq 0)であるコンピュータは、この段階では感染しません。ただし、以下のルール 2 によって後から感染する場合があります。
  1. 感染の伝播(子→親方向): まだ感染していないコンピュータ v1 \leq v \leq N)について、v の子コンピュータのうち感染しているものの台数を a、感染していないものの台数を b とします。a > b を満たすならば、v を新たに感染させます。この判定はコンピュータ v 自身の脆弱性値 D_v の値によらず行われます。すなわち、D_v \leq 0 であっても、子コンピュータの感染状態の条件を満たせば v は感染します。なお、子コンピュータを 1 台も持たないコンピュータでは a = b = 0 となり、a > b を満たさないため、このルールでは感染しません。感染は子の感染状態に基づいて親が感染する方向にのみ起こります。親コンピュータが感染しても、そのことによって子コンピュータが新たに感染することはありません。
  1. 繰り返し: ルール 2 の判定と感染を、新たに感染するコンピュータが 1 台も発生しなくなるまで繰り返します。各回の判定は、その回の開始時点での感染状態に基づいて全コンピュータについて同時に行います。すなわち、ある回で新たに感染するコンピュータが複数ある場合、それらは全て同時に感染し、同じ回の途中で感染状態が変化することはありません。次の回の判定は、更新後の感染状態に基づいて行われます。

最終的に感染したコンピュータの台数を求めてください。中央サーバ(コンピュータ 0)は感染の対象外であるため、カウントに含めません。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq P_i \leq NP_i \neq i1 \leq i \leq N
  • 与えられる接続関係はコンピュータ 0 を根とする N + 1 頂点の木構造を成す
  • -10^9 \leq D_i \leq 10^91 \leq i \leq N
  • 入力はすべて整数である

入力

N
P_1 D_1
P_2 D_2
\vdots
P_N D_N
  • 1 行目には、コンピュータの台数を表す整数 N が与えられる。
  • 2 行目から N + 1 行目では、各コンピュータの情報が与えられる。
  • 1 + i 行目では、コンピュータ i の親コンピュータの番号 P_i と、コンピュータ i の脆弱性値 D_i がスペース区切りで与えられる。

出力

最終的に感染したコンピュータの台数を 1 行で出力せよ。


入力例 1

6
0 -1
1 1
1 2
1 0
2 -5
3 0

出力例 1

3

入力例 2

4
0 0
0 -3
0 -1
0 0

出力例 2

0

入力例 3

15
0 -5
1 -1
1 -1
1 0
2 1
2 1
2 0
3 -1
3 2
3 0
4 1
4 0
8 1
8 1
8 0

出力例 3

10

入力例 4

40
0 -100
1 0
1 -1
1 -1
2 -1
2 -1
2 3
3 -2
3 -2
3 -2
4 1
4 0
4 1
5 0
5 1
5 1
6 -1
6 2
7 0
7 -5
8 1
8 0
8 1
9 -1
9 -1
10 1
10 1
10 0
17 1
17 0
17 1
19 1
19 0
20 -1
20 -1
24 1
24 1
25 0
25 1
34 1

出力例 4

28

入力例 5

1
0 -1000000000

出力例 5

0

Score : 366 pts

Problem Statement

Takahashi is the administrator of a network consisting of N computers and 1 central server. The N computers are numbered from 1 to N, and the central server is numbered as computer 0. That is, the entire network consists of a total of N + 1 computers: computers 0, 1, \ldots, N.

Each computer i (1 \leq i \leq N) is directly connected to a parent computer P_i (0 \leq P_i \leq N, P_i \neq i). These connections form a tree structure with N + 1 vertices rooted at the central server (computer 0). In this tree structure, a child computer of computer v refers to a computer j satisfying P_j = v.

One day, a virus infiltrated the network. Each computer i (1 \leq i \leq N) has a vulnerability value D_i assigned to it.

Virus infection targets only computers 1 through N. The central server (computer 0) is not subject to infection and will never become infected. Infection spreads in a chain reaction according to the following rules:

  1. Initial infection: All computers with a positive vulnerability value (D_i > 0) become infected. Computers with a vulnerability value of 0 or less (D_i \leq 0) are not infected at this stage. However, they may become infected later through Rule 2 below.
  1. Propagation of infection (child → parent direction): For a computer v (1 \leq v \leq N) that is not yet infected, let a be the number of infected child computers of v, and b be the number of non-infected child computers of v. If a > b is satisfied, then v becomes newly infected. This determination is made regardless of the vulnerability value D_v of computer v itself. That is, even if D_v \leq 0, v becomes infected if the condition on the infection status of its child computers is met. Note that for a computer with no child computers, a = b = 0, which does not satisfy a > b, so such a computer will not become infected by this rule. Infection only occurs in the direction where a parent becomes infected based on the infection status of its children. Even if a parent computer becomes infected, this does not cause its child computers to become newly infected.
  1. Repetition: The determination and infection in Rule 2 are repeated until no new computers become infected in a round. The determination in each round is performed simultaneously for all computers based on the infection status at the beginning of that round. That is, if multiple computers become newly infected in a given round, they all become infected simultaneously, and the infection status does not change partway through the same round. The determination in the next round is based on the updated infection status.

Determine the total number of computers that are ultimately infected. The central server (computer 0) is not subject to infection and should not be included in the count.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq P_i \leq N, P_i \neq i (1 \leq i \leq N)
  • The given connections form a tree structure with N + 1 vertices rooted at computer 0
  • -10^9 \leq D_i \leq 10^9 (1 \leq i \leq N)
  • All input values are integers

Input

N
P_1 D_1
P_2 D_2
\vdots
P_N D_N
  • The first line contains an integer N representing the number of computers.
  • Lines 2 through N + 1 give the information for each computer.
  • Line 1 + i contains the parent computer number P_i of computer i and the vulnerability value D_i of computer i, separated by a space.

Output

Output the total number of computers that are ultimately infected, in a single line.


Sample Input 1

6
0 -1
1 1
1 2
1 0
2 -5
3 0

Sample Output 1

3

Sample Input 2

4
0 0
0 -3
0 -1
0 0

Sample Output 2

0

Sample Input 3

15
0 -5
1 -1
1 -1
1 0
2 1
2 1
2 0
3 -1
3 2
3 0
4 1
4 0
8 1
8 1
8 0

Sample Output 3

10

Sample Input 4

40
0 -100
1 0
1 -1
1 -1
2 -1
2 -1
2 3
3 -2
3 -2
3 -2
4 1
4 0
4 1
5 0
5 1
5 1
6 -1
6 2
7 0
7 -5
8 1
8 0
8 1
9 -1
9 -1
10 1
10 1
10 0
17 1
17 0
17 1
19 1
19 0
20 -1
20 -1
24 1
24 1
25 0
25 1
34 1

Sample Output 4

28

Sample Input 5

1
0 -1000000000

Sample Output 5

0