Submit solution
Points:
7
Time limit:
2.0s
Python
3.0s
Memory limit:
64M
Python
128M
Author:
Problem type
You are given a directed acyclic graph of nodes numbered from
to
and you need to perform
queries on it. Node
has a value of
, and every other node has a value of
. Every node only has 1 child. Find the sum of values of a node and all of its children.
Input Specification
The first line will contain the integer , the number of nodes and the number of queries.
The next lines describes which nodes are connected. The
line contains the integers
, where
is the
node's child.
The next lines contains an integer
, the node to query.
Output Specification
For every query, print the sum of values of the node and all of its children.
Sample Input
5 5
1 0
2 0
4 1
8 2
16 3
1
2
3
4
5
Sample Output
1
2
5
10
21
Subtasks
Subtask 1 [30%]
Subtask 2 [70%]
No further constraints.
Comments