Metro

Time limit: 2.5s
Memory limit: 512MB
Input: metro.in
Output: metro.out

In recent years the Compulsive Disorder Research Institute became increasingly busy with discovering and classifying all sorts of peculiar behaviors. You have been dispatched to investigate one such strange compulsion reported amongst the innocent everyday metro travelers.

The underground metro consists of NN stations connected by N1N-1 bidirectional paths and MM metros with linear routes, each having a unique number. The issue concerns the displays located within the stations that show the numbers of the metros passing through their respective station in sorted order. One of your subjects proclaimed that whenever he saw a list of integers C0,C1,,CLC_0, C_1, \dots, C_L he couldn't resist calculating the sum of those even-indexed S=C0+C2+C4++C2K+S = C_0 + C_2 + C_4 + \dots + C_{2K} + \cdots. One such list can be seen on each of the displays located inside the metro stations.

Before any assumptions can be made about those afflicted, the sum for each of the NN displays needs to be calculated. Unfortunately if you manually count those numbers there is a definite chance you might actually end up suffering from this specific disorder. As such you are to write a program that computes them for you, thus protecting your mind from potential insanity.

Task

Given the number of stations, NN, the way in which they are connected, as well as the MM metros, compute the necessary sum for each of the NN stations.

Input data

The file metro.in contains two integers on the first line, NN and MM. The following N1N-1 lines each contain two integers xix_i, yiy_i meaning there is a direct link between stations xix_i and yiy_i.

The next MM lines describe the metros using three integers aia_i, bib_i and noino_i meaning that the route of the metro numbered noino_i goes from station aia_i to station bib_i.

Output data

The file metro.out must contain NN lines. Line ii must contain a single number, the sum for the display in the station with number ii. If there are no trains at all passing through station ii, write the number 00 on line ii.

Constraints and clarifications

  • 1N200 0001 \leq N \leq 200 \ 000
  • 1M200 0001 \leq M \leq 200 \ 000
  • 1noiM1 \leq no_i \leq M for all 1iM1 \leq i \leq M
  • It is guaranteed that all stations are reachable from each other.
# Points Constraints
1 10 N100N \le 100, M100M \le 100
2 10 N5 000N \le 5\ 000, M5 000M \le 5\ 000
3 15 N100 000N \le 100\ 000, M1 000M \le 1\ 000
4 15 N1 000N \le 1\ 000, M100 000M \le 100\ 000
5 20 N100 000N \le 100\ 000, M100 000M \le 100\ 000
6 30 No additional constraints

Example

metro.in

6 4
1 2
2 4
2 6
1 3
5 2
5 6 3
4 5 1
4 3 2
1 2 4

metro.out

2
4
2
1
1
3

Explanation

The displays are showing the following numbers:

Station 11: 2,42, 4
Station 22: 1,2,3,41, 2, 3, 4
Station 33: 22
Station 44: 1,21, 2
Station 55: 1,31, 3
Station 66: 33

Problem info

ID: 2594

Editor: moldovan_robert_lol

Author:

Source: RMI 2016: Day 1 Problem 3

Tags:

RMI 2016

Log in or sign up to be able to send submissions!