POJ 2003 Hire and Fire (Tree)
题目:Hire and Fire
题目翻译成数据结构就是:建树,加结点,删除结点,打印结点。只有删除结点稍微复杂点,因为删除设计掉树的调整。
首先要考虑树怎么存储才能使解题更顺手。
1.我们要存储每个结点的孩子,父亲和名字。存储孩子是因为第一个孩子可能会“升级”,存储父亲是因为要打印,名字肯定也是要打印的;
2.我们要知道每个结点的子树,删除结点会涉及调整;
3.我们要知道根节点,存放CEO。
所以我们的结点出来了,如下:
struct Tman
{
string name;
Tman *father;
list<Tman *> sons;
};
思路都在注释里。
代码如下:
#include<iostream>
#include<map>
#include<list>
#include<string>
using namespace std; //存储名字,父亲,孩子就行
struct Tman
{
string name;
Tman *father;
list<Tman *> sons;
};
//key是结点名字,value是名字的子树
map<string, Tman *> hash; //根结点,指向CEO,打印是要用
Tman *root; void print(long dep, Tman *now)
{
if(now == NULL) return;
for(long i = ; i <= dep; ++i)
cout<<"+";
cout<<now->name<<endl;;
//递归打印每个孩子结点
for(list<Tman *>::iterator j = now->sons.begin(); j != now->sons.end(); ++j)
print(dep + , *j);
} void hires(string n1, string n2)
{
Tman *boss = hash[n1];//父亲的子树指针
Tman *employee = new Tman();//新建一个Tman结构体,用于存储新加入的结点
employee->name = n2;//新加入结点的名字
employee->father = boss;//n1是n2的父亲
boss->sons.push_back(employee);//把n2放入n1的孩子list中
hash[n2] = employee;//新加入的结点也要有子树
} void fire(string n1)
{
Tman *p = hash[n1];//指向n1结点的指针
hash.erase(n1);
while(p->sons.size() > )//如果要删的结点有孩子
{
p->name = p->sons.front()->name;//第一个孩子取代父亲的地位
hash[p->name] = p;//父亲的子树交给第一个孩子
p = p->sons.front();//p往下移动,始终指向孩子队列的第一个
}
p->father->sons.remove(p);//最后一个没有孩子的结点“删除”,实际上是上移了
delete p;//防止野指针
} void solve()
{
string str1,str2;
long i;
cin>>str1;
root = new Tman();
hash[str1] = root;
root->name = str1;
while(cin>>str1)
{
if(str1 == "print")
{
print(,root);
cout<<"------------------------------------------------------------"<<endl;
}
else if(str1 == "fire")
{
cin>>str2;
fire(str2);
}
else
{
cin>>str2;
cin>>str2;
hires(str1, str2);
}
}
} int main()
{
solve();
return ;
}
POJ 2003 Hire and Fire (Tree)的更多相关文章
- POJ 2003 Hire and Fire (多重链表 树结构 好题)
Hire and Fire Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 2316 Accepted: 655 Desc ...
- HDU 1325,POJ 1308 Is It A Tree
HDU认为1>2,3>2不是树,POJ认为是,而Virtual Judge上引用的是POJ数据这就是唯一的区别....(因为这个瞎折腾了半天) 此题因为是为了熟悉并查集而刷,其实想了下其实 ...
- hdu 1325 && poj 1308 Is It A Tree?(并查集)
Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a ...
- POJ 1308 Is It A Tree?和HDU 1272 小希的迷宫
POJ题目网址:http://poj.org/problem?id=1308 HDU题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1272 并查集的运用 ...
- POJ 1308 Is It A Tree?
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18778 Accepted: 6395 De ...
- HDU ACM 1325 / POJ 1308 Is It A Tree?
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- POJ 1308 Is It A Tree? (并查集)
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24237 Accepted: 8311 De ...
- POJ 1308 Is It A Tree? (并查集)
Is It A Tree? 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/M Description A tree is a w ...
- POJ 1308 Is It A Tree?--题解报告
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 31092 Accepted: 10549 D ...
随机推荐
- Linux内核3.0移植并基于Initramfs根文件系统启动
Linux内核移植与启动 Target borad:FL2440 Bootloader:U-boot-2010.09 交叉编译器:buildroot-2012.08 1.linux内核基础知识 首先, ...
- C++ volatile的作用
volatile的作用 2006-10-23 13:44:21 大 中 小 关键在于两个地方: 1. 编译器的优化 (请高手帮我看看下面的理解) 在本次线程内, 当读取一个变量时,为提 ...
- UVa 673 (括号配对) Parentheses Balance
本来是当做水题来做的,后来发现这道题略坑. 首先输入的字符串可能是空串,所以我用了gets函数,紧接着就被scanf("%d", &n)后面的换行符坑掉了. 于是乎再加一句 ...
- [swustoj 404] 最小代价树
最小代价树(0404) 问题描述 以下方法称为最小代价的字母树:给定一正整数序列,例如:4,1,2,3,在不改变数的位置的条件下把它们相加,并且用括号来标记每一次加法所得到的和. 例如:((4+1)+ ...
- js返回上一页并刷新的多种方法
js返回上一页并刷新的几种方法.参考链接:http://www.jbxue.com/article/11230.html <a href="javascript:history.go( ...
- Struts2 教程
一.Struts2是什么 Struts2是在WebWork2基础发展而来的.和Struts1一样, Struts2也是基于MVC的web层框架. 那么既然有了Struts1,为何还要Struts2? ...
- C# 随机读写入文件
先来代码再解释 public Worker(string path) { FileStream fs = new FileStream( path, FileMode.OpenOrCreate, Fi ...
- MySQL中间层 Atlas
Atlas是由 Qihoo 360, Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目.它在MySQL官方推出的MySQL-Proxy 0.8.2版本的基础上,修改了大量bu ...
- NGINX(三)HASH表
前言 nginx的hash表有几种不同的种类, 不过都是以ngx_hash_t为基础的, ngx_hash_t是最普通的hash表, 冲突采用的是链地址法, 不过这里冲突的元素不是一个链表, 而是一个 ...
- 寒假训练第九场 Brocard Point of a Triangle
题意:求布洛卡点坐标 思路:直接利用布洛卡点的性质.http://pan.baidu.com/s/1eQiP76E #include<cstdio> #include<cstring ...