HDU 5444
Test instructions: Give you a tree of the first sequence traversal, the default is 1...N, and then Q query, ask the root node to the path of the point (test instructions very difficult to understand, or I am too stupid)
Idea: This is a big water problem, but I'm still too stupid. 1000 Points of the tree, incredibly with the standard binary tree structure to save points,,, crouching trough what to think of something. You can use a one-dimensional array, left,right directly to the location, I use the list. For the read-in sequence, a binary sort tree is generated, and the path can be recorded at the same time, after forgetting to empty the path and WA a bit.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <queue>7#include <vector>8#include <Set>9#include <string>Ten #defineINF 0x3f3f3f3f One #defineLL Long Long A #defineMAXN 2005 - #defineIn Freopen ("In.txt", "R", stdin); - using namespacestd; the - structnode{ - intx; -Node *left, *Right ; +Node (intx =0): X (x) { -left =NULL; +right =NULL; A }; at }; - -vector<Char>PATH[MAXN]; - -Node *Root; - voidBst_insert (intx) { in //Node *t = root; - //return; toNode *t =Root; + //Node *s = &node (x); - while(t! =NULL) { the if(X > T->x) { *Path[x].push_back ('W'); $ if(T->right = =NULL) {Panax NotoginsengT->right =NewNode (x); - Break; the } + Else{ At = t->Right ; the } + } - Else{ $Path[x].push_back ('E'); $ if(T->left = =NULL) { -T->left =NewNode (x); - Break; the } - Else{Wuyit = t->Left ; the } - } Wu } - } About $ intMain () { - //in ; - //freopen ("OUT.txt", "w", stdout); - intt, N; Ascanf"%d", &t); + while(t--){ thescanf"%d", &n); - intx; $scanf"%d", &x); theRoot =NewNode (x); the for(inti =1; I <= N; i++){ the path[i].clear (); the } - for(inti =2; I <= N; i++) { inscanf"%d", &x); the Bst_insert (x); the } About //Find (1, n, 1); the intQ; thescanf"%d", &q); the //BFS (); + while(q--){ -scanf"%d", &x); the for(inti =0; I < path[x].size (); i++){Bayiprintf"%c", Path[x][i]); the } theprintf"\ n"); - } - } the return 0; the}
HDU 5444 Elven Postman two forks sorting tree