In the world of Teradata, each country has one central bank and its own payment gateway associated with it. They all are connected to each other using tree topology. If a customer is trying to transfer funds from country to then the funds have to flow through all intermediate banks connecting and .
Tree topology is structured like a tree, where it has a root node, intermediate nodes and leaves. Root node is the head node of the structure, and the leaves are the last nodes, which has no further child nodes. This structure is arranged in a hierarchical form, each node can have any number of the child nodes.
Let's say there are central banks in the Teradata world and all of them are uniquely numbered between . Root bank will be represented by . All edges are bidirectional in nature, i.e., a funds can flow in any direction. It is guaranteed that there will be exactly one path between each pair of banks. There will be connections which will be used to connect the banks.
Funds can be transferred between 2 banks if the number of banks (aka nodes) on the path (including end-points) will not exceed a certain threshold value. Otherwise transfer operation can't be initiated.
Above figure represents a sample bank network structure which is connected via tree topology.
- Nodes between bank #4 and #2 .
- Nodes between bank #5 and #1 .
- Nodes between bank #9 and #10 .
- Nodes between bank #7 and #10 .
Reference Input
Configuration Data: You will be provided network configuration as training.txt
file. First line of it contains an integer, , representing total number of banks in the topology. Then follows lines. Each line will contain two comma integers, , which represents that is the parent of .
Use this link to know how to read from file: https://www.hackerrank.com/environment, then choose tab Writing state information to a file.
10
1,2
2,4
2,5
1,6
6,9
6,10
1,3
3,7
3,8
Constraints
Client Request: A client will send multiple requests to the server. Each of these query contains three comma separated integers as string, , where and represents the source and destination banks. And is the maximum number of nodes which are allowed in the path. End of queries from a client will be represented by string END
. After receiving this you have to reply back the same (END
) and then disconnect the client.
4,2,2
5,1,2
2,4,1
9,10,5
7,10,100
1,5,8
9,10,2
7,10,3
END
Constraints
Server Response: For each query, send YES
to client if funds can be successfully transmitted else NO
.
YES
NO
NO
YES
YES
YES
NO
NO
Note
- Each pair of banks has exactly one path to reach each other.
- Parent bank will always have smaller number.
Explanation
Query #1: Nodes between #2 and #4 is while threshold is . So connection can be established.
Query #2: Nodes between #1 and #5 is while threshold is . So connection can't be established.
Query #3: Nodes between #2 and #4 is while threshold is . So connection can't be established.
Query #4: Nodes between #9 and #10 is while threshold is . So connection can be established.
Query #5: Nodes between #7 and #10 is while threshold is . So connection can be established.
Query #6: Nodes between #1 and #5 is while threshold is . So connection can be established.
Query #7: Nodes between #9 and #10 is while threshold is . So connection can't be established.
Query #8: Nodes between #7 and #10 is while threshold is . So connection can't be established.