"topic Link: J-heap" 1734: Heap Time limit:1 Sec Memory limit:128 MB
submit:239 solved:113
Submitstatusweb Board Description
Input
Output
Sample Input31Ten3Ten 5 31 21 351 2 3) 4 53121242 5Sample OutputYesNoYesIdeas
Heap: The most important property of the heap is that the value of the son must not be less than the father's value. In addition, the nodes of the tree are arranged in a compact order from top to bottom, from left to right.
The heap of the subject is a data structure called a two-fork heap.
"Challenge Program Design" P71
For this problem, first of all to simulate the structure of the two-fork tree, according to test instructions through value[] to save the weight of each node (value)
Then, if there is an edge connection between each of the two nodes, save as tree[][], by entering the two node, a, B, that is, save tree[a][b] = 1
1. In the next deep search, just search for the node of tree[][] = = 1
2. At the same time, the nature of the minimum heap (heap) needs to be met in the search, i.e. value[node] <= value[child node]
If the above 1.2 two conditions are met, continue to the deep (child node) search
That is, the nature of DFS (depth-first search): Lists all possible, each of which may be searched until the boundary, or is not qualified. (Do not hit the south wall does not look back)
Still, need an array to save, whether the current node has been searched, that is see[] = 1
1#include <iostream>2#include <cstring>3 #defineMax (A > B) (A:B)4 #defineMin (A < b) (A:B)5 Const intMAXN =410;6 intvalue[maxn],tree[maxn][maxn],see[maxn],ac,m;7 voidDfsintk) {8SEE[K] =1;//indicates that a search has been made9 for(intj =1; J <= m;j++){Ten if(Tree[k][j] &&!see[j]) {//no search, and there are edges between two nodes One if(Value[k] <= value[j])//the current node weight value is less than or equal to its child node weights A Dfs (j); - Else -AC =1; the } - } - } - intMain () { + intN; -scanf"%d",&n); + while(n--){ Amemset (Tree,0,sizeof(tree)); atmemset (See,0,sizeof(see)); - inti; -scanf"%d",&m); - for(i =1; I <= m;i++){ -scanf"%d",&value[i]); - } in for(i =1; I < m;i++){ - intb; toscanf"%d%d",&a,&b); +TREE[A][B] =1; - } theAC =0; *Dfs1);//start from the root node $printf (ac?"no\n":"yes\n") ; Panax Notoginseng } - return 0; the}
"Dfs+ heap of two fork tree structure" 15 Light college-j-Heap