#P8710. [蓝桥杯 2020 省 AB1] 网络分析

[蓝桥杯 2020 省 AB1] 网络分析

Problem Description

Xiaoming is doing a network experiment.

He sets up nn computers, called nodes, used to send, receive, and store data.

Initially, all nodes are independent, and there are no connections.

Xiaoming can use network cables to connect two nodes. After they are connected, the two nodes can communicate with each other. If there is a cable connection between two nodes, they are called adjacent.

Sometimes Xiaoming tests the current network. He sends a message from a certain node. The message is sent to each adjacent node, and then those nodes forward it to their own adjacent nodes, until all nodes that are directly or indirectly adjacent have received the message. All nodes that send or receive the message will store it. Each message is stored only once.

Given Xiaoming’s process of connecting and testing, compute the total size of messages stored on each node.

Input Format

The first line contains two integers nn and mm, representing the number of nodes and the number of operations. Nodes are numbered from 11 to nn.

The next mm lines each contain three integers, describing an operation.

If an operation is 1 a b, it means connecting node aa and node bb with a network cable. When a=ba = b, it means adding a self-loop, which has no real effect on the network.

If an operation is 2 p t, it means sending a message of size tt from node pp.

Output Format

Output one line containing nn integers, separated by one space, representing the total size of messages stored on nodes 11 to nn after all operations are completed.

4 8
1 1 2
2 1 10
2 3 5
1 4 1
2 2 2
1 1 2
1 2 4
2 2 1
13 13 5 3

Hint

For 30%30\% of the test cases, 1n201 \le n \le 20 and 1m1001 \le m \le 100.

For 50%50\% of the test cases, 1n1001 \le n \le 100 and 1m10001 \le m \le 1000.

For 70%70\% of the test cases, 1n10001 \le n \le 1000 and 1m100001 \le m \le 10000.

For all test cases, 1n100001 \le n \le 10000, 1m1051 \le m \le 10^5, and 1t1001 \le t \le 100.

Lanqiao Cup 2020, First Round of the Provincial Contest, Group A Problem J (Group B Problem J).

Translated by ChatGPT 5