POJ 2003 Hire and Fire (多重链表 树结构 好题)
来源:互联网 发布:yaw 算法 编辑:程序博客网 时间:2024/06/16 07:31
Hire and Fire
Time Limit: 1000MS Memory Limit: 30000KTotal 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:
![](http://poj.org/images/2003_1.jpg)
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:
![](http://poj.org/images/2003_2.jpg)
![](http://poj.org/images/2003_1.jpg)
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:
![](http://poj.org/images/2003_2.jpg)
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:
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.
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]
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:
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.
- 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
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
题目链接:http://poj.org/problem?id=2003
题目大意:看题面和输入输出很恐怖的样子,其实题意很简答,一棵树
A hires B 表示把B做为A的儿子
fire A 表示把A结点去掉,去掉以后其第一个儿子结点到它的位置,然后其第一个孙子结点到其第一个儿子结点出,以此类推。。。直到叶子
print 表示按先序遍历,遍历整棵树,+号个数表示当前点所在层的层数
题目分析:想到好的数据结构可以简化一大部分问题,像这种要对树上结点增删查的问题,我们采用多重链表来构树
多重链表结构体里有三个参数,点的名字,父指针,存储儿子指针的链表,还需要一个键值对存储作为子树根的结点对应的树指针,显然用map,具体操作看代码和注释吧
题目链接:http://poj.org/problem?id=2003
题目大意:看题面和输入输出很恐怖的样子,其实题意很简答,一棵树
A hires B 表示把B做为A的儿子
fire A 表示把A结点去掉,去掉以后其第一个儿子结点到它的位置,然后其第一个孙子结点到其第一个儿子结点出,以此类推。。。直到叶子
print 表示按先序遍历,遍历整棵树,+号个数表示当前点所在层的层数
题目分析:想到好的数据结构可以简化一大部分问题,像这种要对树上结点增删查的问题,我们采用多重链表来构树
多重链表结构体里有三个参数,点的名字,父指针,存储儿子指针的链表,还需要一个键值对存储作为子树根的结点对应的树指针,显然用map,具体操作看代码和注释吧
#include <cstdio>#include <string>#include <iostream>#include <list>#include <map>using namespace std;struct Tree{ string name; //结点名字 Tree *fa; //结点父指针 list <Tree *> son; //结点儿子指针链表 Tree() { fa == NULL; }};map <string, Tree *> mp; //结点与其树指针的键值对void Print(int dep, Tree *now) //先序递归输出{ 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]; //得到该点的树指针 while((int)s -> son.size() != 0) //遍历最左位置 { //下面三步相当于把当前结点的儿子上移 s -> name = s -> son.front() -> name; mp[s -> name] = s; s = s -> son.front(); } //此时的s到达最左的叶子处,可以删除 mp.erase(del); //释放以del为根的子树 s -> fa -> son.remove(s); //将其从其父亲的儿子指针链表中删除 delete s; //删除s}void Hire(string fir, string sec){ Tree *f = mp[fir]; //得到父指针 Tree *s = new Tree(); //新建一个指针域 s -> fa = f; //将其指向父指针 s -> name = sec; //命名 mp[sec] = s; //建立其与树指针的键值关系 f -> son.push_back(s); //将其加入父亲的儿子指针链表中 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); } }}
1 0
- POJ 2003 Hire and Fire (多重链表 树结构 好题)
- POJ 2004 Hire and Fire(树+好题)
- POJ 2003 Hire and Fire
- POJ---2003-Hire and Fire
- POJ 2003 Hire and Fire stl
- poj 2003 Hire and Fire list+hash+模拟
- 博弈搜索练习--POJ-2003 Hire and Fire
- POJ2003 Hire and Fire
- (使用数的便利求解层次性问题8.1.2)POJ 2003 Hire and Fire(元素的插入与删除)
- poj2003 Hire and Fire雇佣与解雇
- A - Hire and Fire(8.1.2)
- (并查集..含小总结)A - Hire and Fire(8.1.2)
- remotely work and hire
- poj 2288 Islands and Bridges(状压dp,好题)
- FZU 2150Fire Game (BFS_好题)
- Fire and Motion
- Fire and Forget模式
- POJ 2607 Fire Station
- 银行储蓄系统(C++课程实训博客电子报告)
- GCD—代码演练
- 01排序
- 安卓保存数据的三种方式--Rom、SD、sharepreference
- Linux 基础命令
- POJ 2003 Hire and Fire (多重链表 树结构 好题)
- Queue
- LeetCode 5 Longest Palindromic Substring
- c语言字符匹配(链桟)
- Objective-C中可变与不可变字符串的学习
- 完数
- android中加载大图片到内存中
- php判断假图片的方法
- java的this关键字