Problem Statement
PROBLEM STATEMENT
To avoid the potential problem of network messages (packets) looping around
forever inside a network, each message includes a Time To Live (TTL) field.
This field contains the number of nodes (stations, computers, etc.) that can
retransmit the message, forwarding it along toward its destination, before the
message is unceremoniously dropped. Each time a station receives a message it
decrements the TTL field by 1. If the destination of the message is the current
station, then the TTL field's value is ignored. However, if the message must be
forwarded, and the decremented TTL field contains zero, then the message is not
forwarded.
In this problem you are given the description of a number of networks, and for
each network you are asked to determine the number of nodes that are not
reachable given an initial node and TTL field value. Consider the following
example network:
10 -- 15 -- 20 -- 25
| | | |
30 35 -- 40 45
| | | |
47 | | |
| | | |
50 -- 55 -- 60 -- 65
If a message with a TTL field of 2 was sent from node 35 it could reach nodes
15, 10, 55, 50, 40, 20 and 60. It could not reach nodes 30, 47, 25, 45 or 65,
since the TTL field would have been set to zero on arrival of the message at
nodes 10, 20, 50 and 60. If we increase the TTL field's initial value to 3,
starting from node 35 a message could reach all nodes except node 45.
DEFINITION
Class name: Network
Method name: reachable
Parameters: String[], int, int
Return type: int
Method signature: int reachable(String[] nodes, int start, int ttl); make sure
your method is public
nodes will contain strings that are pairs of integers. These will be the
interconnections between nodes, for example "50 55" would indicate a connection
between node 50 and node 55. start will be the node that messages start from.
ttl will be the Time To Live values for the messages. There will be no more
than one (direct) connection between any pair of nodes, and no network will
contain more than 30 nodes.
Your method will test to see which nodes are not reachable from the start node
given a specific TTL value. Your method will return the number of nodes that
are not reachable from the starting point with the given TTL.
TopCoder will ensure that:
- nodes will have between 1 and 30 elements inclusive.
- ttl will be between 0 and 30 inclusive.
- each node will be between 0 and 1000 inclusive.
- each element in nodes will be formatted as follows "<node1> <node2>".
Double quotes are just for clarity, they will not be included in the input.
<node1> is the first node, then a single space, then <node2>.
- a node will not be directly connected to itself.
- there will be at most 1 connection between a pair of nodes.
- start will be in nodes.
NOTES
EXAMPLES
1. Given the following node connections: {"10 15", "15 20", "20 25", "10 30",
"30 47", "47 50", "25 45", "45 65", "15 35", "35 55", "20 40", "50 55", "35
40", "55 60", "40 60", "60 65"}, the starting point: 35, and the TTL: 2, there
are 5 nodes not reachable.
2. Given the following node connections: {"10 15", "15 20", "20 25", "10 30",
"30 47", "47 50", "25 45", "45 65", "15 35", "35 55", "20 40", "50 55", "35
40", "55 60", "40 60", "60 65"}, the starting point: 35, and the TTL: 3, there
is 1 node not reachable.
2. Given the following node connections: {"1 2", "2 7", "1 3", "3 4", "3 5", "5
10", "5 11", "4 6", "7 6", "7 8", "7 9", "8 9", "8 6", "6 11"}, the starting
point: 1, and the TTL: 1, there are 8 nodes not reachable.
Definition
- Class:
- Network
- Method:
- reachable
- Parameters:
- String[], int, int
- Returns:
- int
- Method signature:
- int reachable(String[] param0, int param1, int param2)
- (be sure your method is public)
Constraints
Examples
No public sample cases were explicitly marked for this problem in the original data.