Hire and Fire
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 2316   Accepted: 655

Description

In this problem, you are asked to keep track of the hierarchical structure of an organization's changing staff. As the first event in the life of an organization, the Chief Executive Officer (CEO) is
named. Subsequently, any number of hires and fires can occur. Any member of the organization (including the CEO) can hire any number of direct subordinates, and any member of the organization (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 two direct subordinates: Tanenbaum and Dijkstra. Members of the organization who are direct subordinates of the same member are ranked by their respective seniority. In the diagram, the seniority of
such members decrease from left to right. For example Tanenbaum has higher seniority than Dijkstra.




When a member hires a new direct subordinate, the newly hired subordinate has lower seniority than any other direct subordinates of the same member. For example, if VonNeumann (in Figure 1) hires Shannon, then VonNeumann's direct subordinates are Tanenbaum,
Dijkstra, and Shannon in order of decreasing seniority.



When a member of the organization gets fired, there are two possible scenarios. If the victim (the person who gets fired) had no subordinates, then he/she will be simply dropped from the organization's hierarchy. If the victim had any subordinates, then his/her
highest ranking (by seniority) direct subordinate will be promoted to fill the resulting vacancy. The promoted person will also inherit the victim's seniority. Now, if the promoted person also had some subordinates then his/her highest ranking direct subordinate
will similarly be promoted, and the promotions will cascade down the hierarchy until a person having no subordinates has been promoted. In Figure 1, if Tanenbaum gets fired, then Stallings will be promoted to Tanenbaum's position and seniority, and Knuth will
be 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 20 characters, which may be upper or lower case letters, apostrophes,
and hyphens. (In particular, no blank spaces.) Each name contains at least one upper case and at least one lower case letter.






The first line will be followed by one or more additional lines. The format of each of these lines will be determined by one of the following three rules of syntax:

  • [existing member] hires [new member]
  • fire [existing member]
  • print

Here [existing member] is the name of any individual who is already a member of the organization, [new member] is the name of an individual who is 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 that at any time there is at least one member (who is the CEO) and no more than 1000 members in the organization.

Output

For each print command, print the current hierarchy of the organization, assuming all hires and fires since the beginning of the input have been processed as explained above. Tree diagrams (such as
those in Figures 1 and 2) are translated into textual format according to the following rules:

  • Each line in the textual representation of the tree will contain exactly one name.
  • The first line will contain the CEO's name, starting in column 1.
  • The entire tree, or any sub-tree, having the form




    will be represented in textual form as:

The output resulting from each print command in the input will be terminated by one line consisting of exactly 60 hyphens. There will not be any blank lines in the output.

Sample Input

  1. VonNeumann
  2. VonNeumann hires Tanenbaum
  3. VonNeumann hires Dijkstra
  4. Tanenbaum hires Stallings
  5. Tanenbaum hires Silberschatz
  6. Stallings hires Knuth
  7. Stallings hires Hamming
  8. Stallings hires Huffman
  9. print
  10. VonNeumann hires Shannon
  11. fire Tanenbaum
  12. print
  13. fire Silberschatz
  14. fire VonNeumann
  15. print

Sample Output

  1. VonNeumann
  2. +Tanenbaum
  3. ++Stallings
  4. +++Knuth
  5. +++Hamming
  6. +++Huffman
  7. ++Silberschatz
  8. +Dijkstra
  9. ------------------------------------------------------------
  10. VonNeumann
  11. +Stallings
  12. ++Knuth
  13. +++Hamming
  14. +++Huffman
  15. ++Silberschatz
  16. +Dijkstra
  17. +Shannon
  18. ------------------------------------------------------------
  19. Stallings
  20. +Knuth
  21. ++Hamming
  22. +++Huffman
  23. +Dijkstra
  24. +Shannon
  25. ------------------------------------------------------------

Source

Rocky Mountain 2004



题目链接:http://poj.org/problem?

id=2003



题目大意:看题面和输入输出非常恐怖的样子。事实上题意非常简答。一棵树

A hires B 表示把B做为A的儿子

fire A 表示把A结点去掉。去掉以后其第一个儿子结点到它的位置,然后其第一个孙子结点到其第一个儿子结点出,以此类推。。

。直到叶子

print 表示按先序遍历,遍历整棵树,+号个数表示当前点所在层的层数



题目分析:想到好的数据结构能够简化一大部分问题,像这样的要对树上结点增删查的问题,我们採用多重链表来构树

多重链表结构体里有三个參数,点的名字,父指针,存储儿子指针的链表,还须要一个键值对存储作为子树根的结点相应的树指针。显然用map,详细操作看代码和凝视吧

  1. #include <cstdio>
  2. #include <string>
  3. #include <iostream>
  4. #include <list>
  5. #include <map>
  6. using namespace std;
  7.  
  8. struct Tree
  9. {
  10. string name; //结点名字
  11. Tree *fa; //结点父指针
  12. list <Tree *> son; //结点儿子指针链表
  13. Tree()
  14. {
  15. fa == NULL;
  16. }
  17. };
  18.  
  19. map <string, Tree *> mp; //结点与其树指针的键值对
  20.  
  21. void Print(int dep, Tree *now) //先序递归输出
  22. {
  23. if(!now)
  24. return;
  25. for(int i = 0; i < dep; i++)
  26. printf("+");
  27. cout << now -> name << endl;
  28. for(list <Tree *> :: iterator it = now -> son.begin(); it != now -> son.end(); it++)
  29. Print(dep + 1, *it);
  30. return;
  31. }
  32.  
  33. void Fire(string del)
  34. {
  35. Tree *s = mp[del]; //得到该点的树指针
  36. while((int)s -> son.size() != 0) //遍历最左位置
  37. {
  38. //以下三步相当于把当前结点的儿子上移
  39. s -> name = s -> son.front() -> name;
  40. mp[s -> name] = s;
  41. s = s -> son.front();
  42. }
  43. //此时的s到达最左的叶子处,能够删除
  44. mp.erase(del); //释放以del为根的子树
  45. s -> fa -> son.remove(s); //将其从其父亲的儿子指针链表中删除
  46. delete s; //删除s
  47. }
  48.  
  49. void Hire(string fir, string sec)
  50. {
  51. Tree *f = mp[fir]; //得到父指针
  52. Tree *s = new Tree(); //新建一个指针域
  53. s -> fa = f; //将其指向父指针
  54. s -> name = sec; //命名
  55. mp[sec] = s; //建立其与树指针的键值关系
  56. f -> son.push_back(s); //将其增加父亲的儿子指针链表中
  57. return;
  58. }
  59.  
  60. int main()
  61. {
  62. string rt, fir, sec;
  63. cin >> rt;
  64. Tree *root = new Tree();
  65. mp[rt] = root;
  66. root -> name = rt;
  67. while(cin >> fir)
  68. {
  69. if(fir == "print")
  70. {
  71. Print(0, root);
  72. printf("------------------------------------------------------------\n");
  73. }
  74. else if(fir == "fire")
  75. {
  76. cin >> sec;
  77. Fire(sec);
  78. }
  79. else
  80. {
  81. string tmp;
  82. cin >> tmp >> sec;
  83. Hire(fir, sec);
  84. }
  85. }
  86. }

POJ 2003 Hire and Fire (多重链表 树结构 好题)的更多相关文章

  1. POJ 2003 Hire and Fire (Tree)

    题目:Hire and Fire 题目翻译成数据结构就是:建树,加结点,删除结点,打印结点.只有删除结点稍微复杂点,因为删除设计掉树的调整. 首先要考虑树怎么存储才能使解题更顺手. 1.我们要存储每个 ...

  2. POJ 2392 Space Elevator(贪心+多重背包)

    POJ 2392 Space Elevator(贪心+多重背包) http://poj.org/problem?id=2392 题意: 题意:给定n种积木.每种积木都有一个高度h[i],一个数量num ...

  3. POJ 3260 The Fewest Coins(多重背包+全然背包)

    POJ 3260 The Fewest Coins(多重背包+全然背包) http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币 ...

  4. POJ 2887 Big String(块状链表)

    题目大意 给一个字符串,长度不超过 106,有两种操作: 1. 在第 i 个字符的前面添加一个字符 ch 2. 查询第 k 个位置是什么字符 操作的总数不超过 2000 做法分析 好多不同的做法都可以 ...

  5. Poj 3189 Steady Cow Assignment (多重匹配)

    题目链接: Poj 3189 Steady Cow Assignment 题目描述: 有n头奶牛,m个棚,每个奶牛对每个棚都有一个喜爱程度.当然啦,棚子也是有脾气的,并不是奶牛想住进来就住进来,超出棚 ...

  6. poj 1276 Cash Machine(多重背包)

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33444   Accepted: 12106 De ...

  7. POJ 2584 T-Shirt Gumbo (二分图多重最大匹配)

    题意 现在要将5种型号的衣服分发给n个参赛者,然后给出每个参赛者所需要的衣服的尺码的大小范围,在该尺码范围内的衣服该选手可以接受,再给出这5种型号衣服各自的数量,问是否存在一种分配方案使得每个选手都能 ...

  8. poj 3046 Ant Counting (DP多重背包变形)

    题目:http://poj.org/problem?id=3046 思路: dp [i] [j] :=前i种 构成个数为j的方法数. #include <cstdio> #include ...

  9. Running Median POJ - 3784 (对顶堆/优先队列 | 链表)

    For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After ...

随机推荐

  1. 在SqlServer 中解析JSON数据 [parseJSON] 函数 数据库中 解析JSON

    使用如下: SELECT * FROM parseJSON('{ "联系人": { "姓名": "huang", "网名" ...

  2. JavaScript:RegExp 对象

    ylbtech-JavaScript:RegExp 对象 1.返回顶部 RegExp 对象 RegExp 对象表示正则表达式,它是对字符串执行模式匹配的强大工具. 直接量语法 /pattern/att ...

  3. quantum theory

    the principles of quantum mechanics by p.a.m.dirac.

  4. [leetcode]Spiral Matrix II @ Python

    原题地址:https://oj.leetcode.com/problems/spiral-matrix-ii/ 题意: Given an integer n, generate a square ma ...

  5. iOS开发-Instruments性能调优

    性能是苹果审核的一个很重要的部分,CPU,内存,图形绘制,存储空间和网络性能都是应用的重要的评估和组成部分.不管是作为个人应用开发者还是企业的开发人员,都需要遵循的一个原则是站在用户的角度去思考问题, ...

  6. Recover Binary Search Tree leetcode java

    题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...

  7. Openfire XMPP Smack RTC IM 即时通讯 聊天 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  8. python3 CERTIFICATE_VERIFY_FAILED错误 certificate verify failed

    在response = request.urlopen(url)打开一个https连接时报如下错误: urllib.error.URLError: <urlopen error [SSL: CE ...

  9. F分布

    定义:设X1服从自由度为m的χ2分布,X2服从自由度为n的χ2分布,且X1.X2相互独立,则称变量F=(X1/m)/(X2/n)所服从的分布为F分布,其中第一自由度为m,第二自由度为n.[1] F分布 ...

  10. JPA(五):映射关联关系------映射单向多对一的关联关系

    映射单向多对一的关联关系 新建Customer.java: package com.dx.jpa.singlemanytoone; import java.util.Date; import java ...