HDU 5444 Elven Postman two forks sorting tree

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.