POJ 2003 Hire and Fire (多重链表 树结构 好题)
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 2316 | Accepted: 655 |
Description
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
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]
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
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
VonNeumann
VonNeumann hires Tanenbaum
VonNeumann hires Dijkstra
Tanenbaum hires Stallings
Tanenbaum hires Silberschatz
Stallings hires Knuth
Stallings hires Hamming
Stallings hires Huffman
VonNeumann hires Shannon
fire Tanenbaum
fire Silberschatz
fire VonNeumann
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
题目链接: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);
}
}
}
POJ 2003 Hire and Fire (多重链表 树结构 好题)的更多相关文章
- POJ 2003 Hire and Fire (Tree)
题目:Hire and Fire 题目翻译成数据结构就是:建树,加结点,删除结点,打印结点.只有删除结点稍微复杂点,因为删除设计掉树的调整. 首先要考虑树怎么存储才能使解题更顺手. 1.我们要存储每个 ...
- POJ 2392 Space Elevator(贪心+多重背包)
POJ 2392 Space Elevator(贪心+多重背包) http://poj.org/problem?id=2392 题意: 题意:给定n种积木.每种积木都有一个高度h[i],一个数量num ...
- POJ 3260 The Fewest Coins(多重背包+全然背包)
POJ 3260 The Fewest Coins(多重背包+全然背包) http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币 ...
- POJ 2887 Big String(块状链表)
题目大意 给一个字符串,长度不超过 106,有两种操作: 1. 在第 i 个字符的前面添加一个字符 ch 2. 查询第 k 个位置是什么字符 操作的总数不超过 2000 做法分析 好多不同的做法都可以 ...
- Poj 3189 Steady Cow Assignment (多重匹配)
题目链接: Poj 3189 Steady Cow Assignment 题目描述: 有n头奶牛,m个棚,每个奶牛对每个棚都有一个喜爱程度.当然啦,棚子也是有脾气的,并不是奶牛想住进来就住进来,超出棚 ...
- poj 1276 Cash Machine(多重背包)
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33444 Accepted: 12106 De ...
- POJ 2584 T-Shirt Gumbo (二分图多重最大匹配)
题意 现在要将5种型号的衣服分发给n个参赛者,然后给出每个参赛者所需要的衣服的尺码的大小范围,在该尺码范围内的衣服该选手可以接受,再给出这5种型号衣服各自的数量,问是否存在一种分配方案使得每个选手都能 ...
- poj 3046 Ant Counting (DP多重背包变形)
题目:http://poj.org/problem?id=3046 思路: dp [i] [j] :=前i种 构成个数为j的方法数. #include <cstdio> #include ...
- Running Median POJ - 3784 (对顶堆/优先队列 | 链表)
For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After ...
随机推荐
- SQLite中的事务操作
关于SQLite事务可以解决一些问题,比如你要插入两个数据,可以将两个数据作为同一个事务进行插入,这样如果第二个数据错误了,便自动执行回滚操作,第一个数据也不会插入成功,保证了数据的同步! 一.实际的 ...
- 从源码角度一步一步来修改PreferenceActivity界面
PreferenceActivity给我们封装好了一个数据存储对象,我们只需要在xml文件中写上控件即可完成简单的设置界面.但是系统提供的设置界面十分的简陋,要想做的好看必须要自己来进行修改 ...
- .Net Excel操作之NPOI(一)简介
一.NPOI简介 NPOI是一个开源项目,可以读/写xls,doc,ppt文件,有着广泛的应用. 使用NPOI能够帮助开发者在没有安装微软Office的情况下读写Office 97-2003的文件,支 ...
- [转]php cli命令 自定义参数传递
FROM :http://www.cnblogs.com/zcy_soft/archive/2011/12/10/2283437.html 所有的PHP发行版,不论是编译自源代码的版本还是预创建的版本 ...
- Useful JVM Flags – Part 8 (GC Logging)
The last part of this series is about garbage collection logging and associated flags. The GC log is ...
- NVelocity语法常用指令
对变量的引用:$ [ ! ][ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ][ } ]. 在NVelocity中,对变量的引用都是以$开头加上变量名称.当使用 ...
- matlib实现梯度下降法(序一)
数据来源:http://archive.ics.uci.edu/ml/datasets/Combined+Cycle+Power+Plant 数据描述: 有四个输入特征,这些数据来自电厂,这四个特征和 ...
- QT中文乱码与国际化支持
QT国际化支持 Qt内部采用的全Unicode编码,这从根本上保证了多国语界面实现的正确性和便捷性.Qt本身提供的linguist工具,用来实现翻译过程十分方便.MFC中利用资源DLL切换资源,或 ...
- Linux下简单线程池的实现
大多数的网络服务器,包括Web服务器都具有一个特点,就是单位时间内必须处理数目巨大的连接请求,但是处理时间却是比较短的.在传统的多线程服务器模型中是这样实现的:一旦有个服务请求到达,就创建一个新的服务 ...
- VC++深入详解-第四章学习心得
这一章节主要讲解了 简单的绘图 主要是通过一些小的例子让我们学会了VC++的一些基本操作 void CDrawView::OnLButtonDown(UINT nFlags, CPoint point ...