[Swift] LCP 5. Hair LeetCoin

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤ micro-channel public number: Shan Wing Chi ( let_us_code)
➤ bloggers domain: https://www.zengqiang.org
➤GitHub address: https://github.com/strengthen/LeetCode
➤ original address: https://www.cnblogs.com/strengthen/p/12151724 .html
➤ If the address is not a link blog Park Yong Shan Chi, it may be crawling author of the article.
➤ text has been modified update! Click strongly recommended that the original address read! Support authors! Support the original!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

The Brush problem management team can use a tree, he said:

Only one person in charge of the team, No. 1. In addition to the responsible person, each person has only one leader (not responsible leader);
absence of circulation management, such as management of A B, B management C, C Management A.
 

Stay button want to do the following three:

To a member of the team (can also be a person in charge) made a number of LeetCoin;
to a member of the team (can also be a person in charge), as well as his / her management of the owner (ie his / her subordinates, he / she a subsidiary of subordinates, ......), made a number of LeetCoin;
query one member (who may also be responsible for), and all those he / she is sent to the management of LeetCoin sum.
 

Input:

N represents the number of team members (numbered 1 ~ N, the person responsible. 1);
withleadership is of size (N - 1) * 2 two-dimensional array, wherein each element of [a, b] is b represents a subordinate ;
operations is a two-dimensional array of length Q, represent the following operations in time sequence, the format:
operations [I] [0] =. 1: represents a first operation, operations [i] [1] number of members representing, operations [i] [2] represents the number of LeetCoin;
operations [I] [0] = 2: representative of the second operation, operations [i] [1] of the representative member ID, operations [i] [2] of the representative LeetCoin number;
operations [I] [0] =. 3: number represents the third operation, operations [i] [1] Representative members;
output:

Returns an array, each array is the return value of the query (sent LeetCoin of operation does not require any return value). Because a lot of hair LeetCoin, please die each query result of 1e9 + 7 (1000000007).

 

Example 1:

Input: N = 6, leadership = [ [1, 2], [1, 6], [2, 3], [2, 5], [1, 4]], operations = [[1, 1, 500] , [2, 2, 50], [3, 1], [2, 6, 15], [3, 1]]
output: [650, 665]
explanation: team management relationship is shown below.
The first query, the number of each member LeetCoin obtained were (in numerical order): 500, 50, 50, 0, 50, 0;
when the second query, the number of each member LeetCoin were obtained (in numerical order): 500, 50, 50, 0, 50, 15.

limit:

1 <= N <= 50000
1 <= Q <= 50000
operations[i][0] != 3 时,1 <= operations[i][2] <= 5000

Guess you like

Origin www.cnblogs.com/strengthen/p/12151724.html