Title:
Git is a distributed code management tool that uses a tree to record a file's change history, such as: Base ' <--base<--a<--a ' ^ | ---b<--b ' Xiaomi engineers often need to look for two branches of the nearest split point, that is, base. Assuming that the git tree is a multi-fork tree, implement an algorithm that calculates the nearest split point for any two points on the git tree. (assuming that the number of Git tree nodes is n, the git tree is represented in the form of an adjacency matrix: the string array matrix contains n strings, each consisting of the characters ' 0 ' or ' 1 ', with a length of N.) matrix[i][j]== ' 1 ' when and only if Git tree species are connected to the first and the J nodes. Node 0 is the root node of the git tree. )
Input Example:
[01011,10100,01000,10000,10000],1,2
Output Example:
1
Ideas:
Git just introduces a background to the topic, in fact the problem is: give a multi-fork tree, looking for any two points of the nearest common parent node of the multi-fork tree. A multi-fork tree is represented by an adjacency matrix, and node 0 represents the root node.
Method: To find the nearest common parent node between two points, it is necessary to start from two nodes, and continue to backtrack until the nearest common parent node is found. Then we need to record the parent node information for each point, which can be traced up at a node to find the parent node.
How is the parent node recorded? The topic gives the adjacency matrix, you can turn the adjacency matrix to the adjacency list, so from the root node, that is, the root node into the stack, and then out of the stack, first find the node adjacent to the root, namely the tree layer (the root node is the No. 0 layer), record the depth of the second layer node (that is, root node + 1 Then the second layer of the node into the stack, sequentially out of the stack, repeating the above process. This gives you the depth and parent information for all nodes.
Code:
classSolution { Public: /** * returns the nearest split point of two points on the git tree * * @param matrix adjacent, representing Git tree, matrix[i][j] = = ' 1 ' When and only if the Git tree is connected to the I and J nodes, node 0 is the node of the Git tree * @param index * of indexa Node A @param index * @return integer of indexb Node B*/ intGetsplitnode (vector<string> Matrix,intIndexa,intIndexb) { intn=matrix.size (); Vector<vector<int> >adjlist (n); Vector<int> Parent (n,-1); Vector<int> Depth (n,0); for(intI=0; i<n;i++){ for(intj=i+1; j<n;j++){ if(matrix[i][j]=='1') {adjlist[i].push_back (j); Adjlist[j].push_back (i); }}} vector<int>Stk; Stk.push_back (0); intCount=0;//depth[0]=0; while(!stk.empty () && count<N) { intNode=Stk.back (); Stk.pop_back (); for(intk=0; K<adjlist[node].size (); k++){ intv=Adjlist[node][k]; if(parent[v]!=-1) Continue; PARENT[V]=node; DEPTH[V]=depth[node]+1; Stk.push_back (v); Count++; } } intA=Indexa; intb=Indexb; while(depth[a]>Depth[b]) a=Parent[a]; while(depth[a]<Depth[b]) b=Parent[b]; while(a!=b) {a=Parent[a]; b=Parent[b]; } returnA; }};
(written question) Xiaomi git