#abc319f. F - Fighter Takahashi

F - Fighter Takahashi

Score : 550550 points

问题描述

存在一棵包含 NN 个顶点的树。第1个顶点为根节点,第 ii2iN2\leq i\leq N)个顶点的父节点为pip _ i1pi<i1\leq p _ i<i)。

每个非根顶点上有一个敌人或一种药剂。高桥想要击败所有敌人。初始时,他的力量值为 11,并且位于顶点 11。对于第 iii=2,,Ni=2,\ldots,N)个顶点的信息由一个包含三个整数的元组 (ti,si,gi)(t _ i,s _ i,g _ i) 表示,如下所示:

  • ti=1t _ i=1,则在第 ii 个顶点上有一个敌人。当高桥首次访问这个顶点时,如果他的力量值小于 sis _ i,他会被敌人击败并失败,之后无法移动到其他顶点。否则,他将击败敌人,并且他的力量值增加 gig _ i
  • ti=2t _ i=2,则在第 ii 个顶点上有一种药剂。当高桥首次访问这个顶点时,他会获取药剂,其力量值会乘以 gig _ i。(对于拥有药剂的顶点,si=0s _ i=0。)

最多有 1010 个顶点含有药剂。

高桥可以重复地移动到相邻顶点。判断他是否能够击败所有敌人。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There is a tree with NN vertices. The 11-st vertex is the root, and the parent of the ii-th vertex (2iN)(2\leq i\leq N) is pi (1pi<i)p _ i\ (1\leq p _ i\lt i).

Each non-root vertex has an enemy or a medicine on it. Takahashi wants to defeat all the enemies. Initially, his strength is 11, and he is at vertex 11. For i=2,,Ni=2,\ldots,N, the information of the ii-th vertex is represented by a triple of integers (ti,si,gi)(t _ i,s _ i,g _ i) as follows.

  • If ti=1t _ i=1, there is an enemy at the ii-th vertex. When Takahashi visits this vertex for the first time, if his strength is less than sis _ i, Takahashi is defeated by the enemy and loses, after which he cannot move to other vertices. Otherwise, he defeats the enemy, and his strength increases by gig _ i.
  • If ti=2t _ i=2, there is a medicine at the ii-th vertex. When Takahashi visits this vertex for the first time, he takes the medicine, and his strength is multiplied by gig _ i. (For a vertex with a medicine, si=0s _ i=0.)

There are at most 1010 vertices with a medicine.

Takahashi can repeatedly move to an adjacent vertex. Determine if he can defeat all the enemies.

Constraints

  • 2N5002\leq N\leq 500
  • 1pi<i (2iN)1\leq p _ i\lt i\ (2\leq i\leq N)
  • ti{1,2} (2iN)t _ i\in\lbrace1,2\rbrace\ (2\leq i\leq N)
  • $t _ i=1\implies1\leq s _ i\leq 10 ^ 9\ (2\leq i\leq N)$
  • ti=2    si=0 (2iN)t _ i=2\implies s _ i=0\ (2\leq i\leq N)
  • 1gi109 (2iN)1\leq g _ i\leq 10 ^ 9\ (2\leq i\leq N)
  • There are at most 1010 vertices with ti=2t _ i=2.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN

p2p _ 2 t2t _ 2 s2s _ 2 g2g _ 2

p3p _ 3 t3t _ 3 s3s _ 3 g3g _ 3

\vdots

pNp _ N tNt _ N sNs _ N gNg _ N

Output

Print the answer (Yes or No) in one line.

Sample Input 1

8
1 2 0 3
2 1 3 3
1 2 0 4
4 1 2 2
1 2 0 5
6 1 5 5
5 1 140 1

Sample Output 1

Yes

Initially, the tree looks like this:

Takahashi can defeat all the enemies by moving from vertex 11 to 2,3,2,1,6,7,6,1,4,5,82,3,2,1,6,7,6,1,4,5,8 in this order. Here, his position and strength change as shown in the following figure (movements to vertices that have already been visited are omitted).

On the other hand, if he moves from vertex 11 to 4,5,84,5,8 in this order, for example, his strength when visiting vertex 88 will be less than s8=140s _ 8=140, so he will lose without defeating all the enemies.

Sample Input 2

12
1 1 166 619
1 1 17 592
2 1 222 983
2 1 729 338
5 1 747 62
3 1 452 815
3 2 0 1
4 2 0 40
4 1 306 520
6 1 317 591
1 1 507 946

Sample Output 2

No

Sample Input 3

12
1 1 1 791
2 2 0 410
2 1 724 790
2 1 828 599
5 2 0 13
3 1 550 803
1 1 802 506
5 1 261 587
6 1 663 329
8 1 11 955
9 1 148 917

Sample Output 3

Yes

Sample Input 4

12
1 2 0 1000000000
2 2 0 1000000000
3 2 0 1000000000
4 2 0 1000000000
5 2 0 1000000000
6 2 0 1000000000
7 2 0 1000000000
8 2 0 1000000000
9 2 0 1000000000
10 2 0 1000000000
11 1 1 1

Sample Output 4

Yes

update @ 2024/3/10 09:08:06