topics from the March 2015 CCF Computer professional qualification exam
Problem DescriptionGiven a company's network, it consists of n-switches and M-terminal computers, which use a network connection between the switch and the switch, the switch, and the computer. Switches are set hierarchically and the switch numbered 1 is the root switch with a level of 1. The other switches are connected to a switch that is higher than the previous layer, and the level of the corresponding switch is 1. All terminal computers are connected directly to the switch.
When information is passed between a computer and a switch, each step can only be passed to another computer or switch to which it is connected. Ask, how many steps are required to pass messages between computers and computers, or to pass messages between computers and switches, or to pass messages between switches and switches.
Input FormatThe first line of the input contains two integers n, m, respectively, indicating the number of switches and the number of computers on the terminal.
The second line contains n-1 integers that represent the number of switches connected to the 2nd, 3 、......、 n switches that are higher than their previous level. The switch number on the previous layer connected to the I switch must be smaller than its own number.
The third line contains m integers representing the number of switches connected to the 1th, 2 、......、 m terminal computers.
output FormatOutputs an integer that represents the maximum number of steps required for message delivery.
Sample Input4 2
1 1 3
2 1
Sample Output4
Sample DescriptionThe network connection mode for the sample is as follows, where the circle represents the switch and the box represents the computer:
The message delivery between PC 1 and switch 4 takes the longest time, which is 4 units of time.
Sample Input4 4
1 2 2
3 4 4 4
Sample Output4
Sample DescriptionThe network connection mode for the sample is as follows:
The message delivery between PC 1 and PC 4 takes the longest time, which is 4 units of time. The evaluation use case scale and the 30% evaluation use case satisfies: n≤5, m≤5.
The first 50% evaluation cases meet: N≤20, m≤20.
The first 70% evaluation cases meet: n≤100, m≤100.
All evaluation cases meet: 1≤n≤10000,1≤m≤10000.
Problem Analysislook at the problem, think to build a diagram, need to use the high-end graph algorithm. But according to the structure of the graph, can actually degenerate into a tree, the number of tree subtree is uncertain. the nodes of the tree do not have to save the data, you can use an array to represent the tree structure. the whole problem can also be well decomposed into sub-problems. That is, consider the tree of the root node, which indicates that the maximum number of steps required for message delivery is equal to the sum of the two subtree's depth and +2 (which needs to pass through the root node) and the maximum number of steps required by the Max subtree (no need to pass through the root node). this sentence in the title description "The switch number of the previous layer connected to the first switch must be smaller than its own number. "Let the traversal sequence simply traverse backwards." In fact, there is no need to use a graph or tree algorithm, the code is very concise.
#include"iostream"#include"algorithm"using namespacestd;intparent[10001];intdepth[10001][2];//The maximum depth of both subtree + 1intSmallidx (intx) {if(depth[x][0] < depth[x][1])return 0; Else return 1;}intBigidx (intx) {if(depth[x][0] < depth[x][1])return 1; Else return 0;}intMain () {intM, n, maxDepth =0; CIN>> m >>N; for(intI=2; i<=m; i++) {cin>>Parent[i]; } for(inti=m+1; i<=m+n; i++) { intp; CIN>>p; DEPTH[P][SMALLIDX (p)]=1; } for(intI=m; i>=1; i--) {depth[parent[i]][smallidx (Parent[i])]= Max (Depth[i][bigidx (i)] +1, Depth[parent[i]][smallidx (Parent[i])); MaxDepth= Max (depth[i][0]+depth[i][1], maxDepth); } cout<<maxDepth;}
CCF 201503-4 Network Latency