Hire and Fire
Time Limit: 1000MS |
|
Memory Limit: 30000K |
Total Submissions: 2316 |
|
Accepted: 655 |
Description
In this problem, you is asked to keep track of the hierarchical structure of a organization ' s changing staff. As the first event in the life of a organization, the chief Executive Officer (CEO) is named. Subsequently, any number of hires and fires can occur. Any member of the Organization (including the chief executive) can hire any number of direct subordinates, and any member of the organ ization (including the CEO) can be fired. The organization ' s hierarchical structure can be represented by a tree. Consider the example shown by Figure 1:
Vonneumann is the CEO of this organization. Vonneumann has both direct Subordinates:tanenbaum and Dijkstra. Members of the organization who is direct subordinates of the same member is ranked by their respective seniority. In the diagram, the seniority of such members decrease from left to right. For example Tanenbaum have higher seniority than Dijkstra.
When a member hires a new direct subordinate, the newly hired subordinate have lower seniority than any other direct Subord Inates of the same member. For example, if Vonneumann (in Figure 1) hires Shannon, then Vonneumann ' s direct subordinates is Tanenbaum, Dijkstra, and Shannon in order of decreasing seniority.
When a member of the organization gets fired, there is and possible scenarios. If the victim (the person who gets fired) had no subordinates and then he/she'll be simply dropped from the organization ' s Hierarchy. If the victim had any subordinates, then his/her highest ranking (by seniority) direct subordinate would be promoted to Fil L the resulting vacancy. The promoted person would also inherit the victim ' s seniority. Now, if the promoted person also had some subordinates then his/her highest ranking direct subordinate would similarly be p Romoted, and the promotions would cascade down the hierarchy until a person have no subordinates has been promoted. In Figure 1, if Tanenbaum gets fired, then Stallings'll be is promoted to Tanenbaum ' s position and seniority, and Knuth wil L is promoted to Stallings ' previous position and seniority.
Figure 2 shows the hierarchy resulting from Figure 1 after (1) Vonneumann hires Shannon and (2) Tanenbaum gets fired:
Input
The first line of the "input contains only" the name of the person who is initially the CEO. All names in the input file consist of 2 to characters, which may is upper or lower case letters, apostrophes, and hyph Ens. (in particular, no blank spaces.) Each name contains at least one upper case and at least one lower case letter.
The first line is being followed by one or more additional lines. The format of each of these lines is determined by one of the following three rules of syntax:
- [Existing member] hires [new member]
- Fire [Existing member]
- Print
Here [existing member] was the name of any individual who was already a member of the organization, [new member] is the name Of an individual who are not a member of the organization as yet. The three types of lines (hires, fire, and print) can appear in any order, any number of times.
You may assume it at any time there be at least one member (who's the CEO) and no more than the Organiz ation.
Output
For each print command, print the current hierarchy of the organization, assuming all hires and fires since the beginning Of the input has been processed as explained above. Tree diagrams (such as those in Figures 1 and 2) is translated into textual format according to the following rules:
- Each of the textual representation of the tree would contain exactly one name.
- The first line would contain the CEO ' s name, starting in column 1.
- The entire tree, or any sub-tree, has the form
would be represented in textual form as:
The output resulting from all Print command in the input would be terminated by a line consisting of exactly hyphens. There won't is any blank lines in the output.
Sample Input
Vonneumannvonneumann hires Tanenbaumvonneumann hires Dijkstratanenbaum hires Stallingstanenbaum hires Silberschatzstallings hires Knuthstallings hires Hammingstallings hires Huffmanprintvonneumann hires Shannonfire Tanenbaumprintfire Silberschatzfire Vonneumannprint
Sample Output
vonneumann+tanenbaum++stallings+++knuth+++hamming+++huffman++silberschatz+ Dijkstra------------------------------------------------------------vonneumann+stallings++knuth+++hamming+++ Huffman++silberschatz+dijkstra+shannon------------------------------------------------------------stallings+ Knuth++hamming+++huffman+dijkstra+shannon------------------------------------------------------------
Source
Rocky Mountain 2004
Title Link: http://poj.org/problem?id=2003
The main topic: look at the surface and input and output very scary look, actually test instructions very simple answer, a tree
A hires B means the son of B as a.
Fire a means to remove the a node, remove its first son node to its position, and then its first grandson node to the first son node, and so on ... Until the leaves
Print indicates the number of layers in the layer at which the current point is traversed by traversing the entire tree, with the number of digits in the first order.
Problem analysis: Think of a good data structure can simplify a large part of the problem, like this to the tree node additions and deletions, we use the multi-linked list to construct the tree
There are three parameters in the multi-link list structure, the name of the point, the parent pointer, the linked list that stores the pointer of the son, and a key-value pair that stores the tree pointer corresponding to the node of the subtree, obviously using a map, see the code and the comments in detail.
#include <cstdio> #include <string> #include <iostream> #include <list> #include <map> Using namespace std;struct tree{string name; Knot name Tree *fa; Node parent pointer list <tree *> son; Node son pointer list Tree () {FA = = NULL; }};map <string, Tree *> MP; The key value of the node and its tree pointer to void Print (int dep, tree *now)//pre-order recursive output {if (!now) return; for (int i = 0; i < dep; i++) printf ("+"); cout << Now, name << Endl; For (List <tree *>:: Iterator it = now-son.begin (); it = Now son.end (); it++) Print (dep + 1, *it ); return;} void Fire (String del) {Tree *s = Mp[del]; Get the point of the tree pointer while ((int) s, son.size ()! = 0)//traverse the leftmost position {//The following three steps are equivalent to moving the son of the current node up S-c name = -Son.front (), S-and name; Mp[s, name] = s; s = S-Son.front (); }//At this time s to reach the leftmost leaves, you can delete Mp.erase (DEL); Release a subtree with a del root Son.remove (s), FA-S; Remove the delete s from the list of his father's son pointer; Delete s}void Hire (String fir, string sec) {Tree *f = Mp[fir]; Get parent pointer to tree *s = new Tree (); Create a new pointer field s, FA = f; Point it to the parent pointer s, name = sec; Named mp[sec] = s; Establishes its key-value relationship with the tree pointer, F-son.push_back (s); Add it to the father's son in the list of pointers to return;} int main () {string rt, fir, sec; CIN >> RT; Tree *root = new Tree (); MP[RT] = root; Root-name = RT; while (cin >> fir) {if (fir = = "Print") {print (0, root); printf ("------------------------------------------------------------\ n"); } else if (fir = = "Fire") {cin >> sec; Fire (SEC); } else {string tmp; CIN >> tmp >> sec; Hire (Fir, sec); } }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 2003 Hire and Fire (multi-link table tree structure good problem)