/
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
N 個の放送局があり、1 から N までの番号が付いています。はじめ、すべての放送局は営業しています。
放送素材の提供関係が M 件あります。提供関係 j は整数 u_j,v_j,W_j で表され、毎朝、放送局 u_j が営業していれば、放送局 v_j に素材を W_j 個提供します。自分自身への提供や、同じ局の組に対する複数の提供関係もあり得ます。それぞれの提供関係の素材は別々に数えます。
放送局 i が営業を続けるには、毎日、その朝に受け取った素材が合計 B_i 個以上必要です。1 日目から毎晩、その朝に受け取った素材の合計が B_i 個未満だった営業中の放送局が、すべて同時に営業を停止します。一度停止した局が再び営業することはありません。素材を翌日へ持ち越すことはできません。
各放送局について、何日目の夜に営業を停止するか求めてください。いつまでも営業を続ける局については 0 としてください。
制約
- 1 \leq N \leq 200000
- 0 \leq M \leq 200000
- 0 \leq B_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq u_j,v_j \leq N (1 \leq j \leq M)
- 1 \leq W_j \leq 10^9 (1 \leq j \leq M)
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられます。
N M B_1 B_2 \ldots B_N u_1 v_1 W_1 u_2 v_2 W_2 \vdots u_M v_M W_M
- 1 行目には、放送局の個数 N と提供関係の件数 M が与えられる。
- 2 行目には、各放送局に必要な素材の個数 B_1,B_2,\ldots,B_N が与えられる。
- 続く M 行のうち j 行目には、提供関係 j を表す u_j,v_j,W_j が与えられる。M=0 の場合、これらの行は存在しない。
出力
放送局 1,2,\ldots,N の停止日を、この順に空白区切りで 1 行に出力してください。いつまでも営業を続ける局の停止日は 0 としてください。
入力例 1
3 2 1 2 1 1 2 2 2 3 1
出力例 1
1 2 3
入力例 2
2 2 3 3 1 2 3 2 1 3
出力例 2
0 0
入力例 3
5 5 1 0 4 5 1 1 3 2 2 3 3 3 4 4 4 5 1 5 4 2
出力例 3
1 0 2 3 4
入力例 4
8 9 1 1 0 6 5 3 1 1 1 4 2 2 4 3 3 4 5 4 5 4 5 6 2 6 5 2 7 8 1 8 7 1 6 6 1
出力例 4
1 1 0 2 3 4 0 0
入力例 5
1 0 0
出力例 5
0
Score : 400 pts
Problem Statement
There are N broadcasting stations, numbered 1 to N. Initially, all broadcasting stations are in operation.
There are M material supply relationships. The j-th supply relationship is represented by integers u_j, v_j, W_j: every morning, if broadcasting station u_j is in operation, it provides W_j units of material to broadcasting station v_j. A station may supply material to itself, and there may be multiple supply relationships between the same pair of stations. Materials from each supply relationship are counted separately.
For broadcasting station i to remain in operation, it requires a total of at least B_i units of material received that morning each day. Starting from Day 1, every night, all operating broadcasting stations that received strictly less than B_i units of material in total that morning stop operating simultaneously. Once a station stops operating, it will never resume operation. Materials cannot be carried over to the next day.
For each broadcasting station, determine on the night of which day it stops operating. If a station continues to operate indefinitely, output 0 for it.
Constraints
- 1 \leq N \leq 200000
- 0 \leq M \leq 200000
- 0 \leq B_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq u_j,v_j \leq N (1 \leq j \leq M)
- 1 \leq W_j \leq 10^9 (1 \leq j \leq M)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M B_1 B_2 \ldots B_N u_1 v_1 W_1 u_2 v_2 W_2 \vdots u_M v_M W_M
- The first line contains the number of broadcasting stations N and the number of supply relationships M.
- The second line contains the number of units of material required by each broadcasting station, B_1, B_2, \ldots, B_N.
- The following M lines describe the supply relationships; the j-th line contains u_j, v_j, W_j representing the j-th supply relationship. If M=0, these lines do not exist.
Output
Print the day on which broadcasting stations 1, 2, \ldots, N stop operating, in this order, separated by spaces on a single line. For stations that continue operating indefinitely, print 0.
Sample Input 1
3 2 1 2 1 1 2 2 2 3 1
Sample Output 1
1 2 3
Sample Input 2
2 2 3 3 1 2 3 2 1 3
Sample Output 2
0 0
Sample Input 3
5 5 1 0 4 5 1 1 3 2 2 3 3 3 4 4 4 5 1 5 4 2
Sample Output 3
1 0 2 3 4
Sample Input 4
8 9 1 1 0 6 5 3 1 1 1 4 2 2 4 3 3 4 5 4 5 4 5 6 2 6 5 2 7 8 1 8 7 1 6 6 1
Sample Output 4
1 1 0 2 3 4 0 0
Sample Input 5
1 0 0
Sample Output 5
0