Uva - 506 - System Dependencies
模拟题,注意显示安装和隐式安装,显示安装的必须显示显示删除。把名字转化为整数维护。其他注意都注释了。输入稍微多一下,题目不是很麻烦。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <bitset>
#include <cassert>
using namespace std;
const int maxn = 10000;
int cnt = 0;
map<string, int> name2id; // 把名字转化为整数,方便处理
string name[maxn];
vector<int> depend[maxn]; // 组件x所以来的组件列表
vector<int> depended[maxn]; // 依赖于x的组件列表
int status[maxn]; // 0表示组件x未安装,1表示显示安装,2表示隐式安装
vector<int> installed; // 存放安装过的组件,安装过的就不要再安装了
// 把名字转化为整数维护
int ID(const string& item)
{
if (!name2id.count(item)) {
name[++cnt] = item;
name2id[item] = cnt;
}
return name2id[item];
}
// 是否有组件依赖于item
bool needed(int item)
{
for (int i = 0; i < depended[item].size(); i++) {
if (status[depended[item][i]]) {
return true;
}
}
return false;
}
// 安装item,如果有依赖项,递归的继续安装
void install(int item, bool toplevel)
{
if (!status[item]) {
for (int i = 0; i < depend[item].size(); i++) {
install(depend[item][i], false);
}
cout << " Installing " << name[item] << endl;
status[item] = toplevel ? 1 : 2;
installed.push_back(item);
}
}
// 判断是否能删除,如果能,删除之后递归的删除其他所依赖的组件
void remove(int item, bool toplevel)
{
if ((toplevel || status[item] == 2) && !needed(item)) {
status[item] = 0;
installed.erase(remove(installed.begin(), installed.end(), item), installed.end());
cout << " Removing " << name[item] << endl;
for (int i = 0; i < depend[item].size(); i++) {
remove(depend[item][i], false);
}
}
}
// 按照安装顺序输出
void list()
{
for (int i = 0; i < installed.size(); i++) {
cout << " " << name[installed[i]] << endl;
}
}
int main()
{
ios::sync_with_stdio(false);
string line, cmd;
memset(status, 0, sizeof(status));
while (getline(cin, line)) {
cout << line << endl;
stringstream ss(line);
ss >> cmd;
if (cmd[0] == 'E') {
break;
}
string item1, item2;
if (cmd[0] == 'L') {
list();
}
else {
ss >> item1;
int i1 = ID(item1);
if (cmd[0] == 'D') {
while (ss >> item2) {
int i2 = ID(item2);
depend[i1].push_back(i2);
depended[i2].push_back(i1);
}
}
else if (cmd[0] == 'I') {
if (status[i1]) {
cout << " " << item1 << " is already installed.\n";
}
else {
install(i1, true);
}
}
else {
if (!status[i1]) {
cout << " " << item1 << " is not installed.\n";
}
else if (needed(i1)) {
cout << " " << item1 << " is still needed.\n";
}
else {
remove(i1, true);
}
}
}
}
return 0;
}
Uva - 506 - System Dependencies的更多相关文章
- 【STL+模拟】UVa 506 - System Dependencies
System Dependencies Components of computer systems often have dependencies--other components that m ...
- UVa 506 System Dependencies (细节问题)
题意:输入几种指令,让你进行模拟操作,指令如下: DEPEND item1 item2 (item3 ...) 安装item1需要先安装item2(.item3……) INSTALL item1 安装 ...
- UVA 506 System Dependencies(模拟 烂题)
https://vjudge.net/problem/UVA-506 题目是给出了五种指令,DEPEND.INSTALL.REMOVE.LIST.END,操作的格式及功能如下: DEPEND item ...
- Learning ROS: Managing System dependencies
Download and install the system dependencies for turtlesim: roscd turtlesim cat package.xml rosdep i ...
- 【例题 6-21 UVA - 506】System Dependencies
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 记录每个物品它的依赖有哪些,以及它被哪些东西依赖就可以了. 显式安装的东西不能被隐式删除删掉(就是remove item,然后删除i ...
- C C++ TDD单元测试非常好的书
http://product.china-pub.com/199003 测试驱动的嵌入式C语言开发 Test Driven Development for Embedded C <测试驱动的嵌入 ...
- Debian8.3.0下安装Odoo8.0步骤
Debian8.3.0下安装Odoo8.0的方法 假设你已经安装好了Debian 系统,使用root帐号执行如下命令 # apt-get update && apt-get upgra ...
- Classes
Class Organization Following the standard Java convention, a class should begin with a list of varia ...
- maven-dependency-plugin插件的使用
maven-dependency-plugin插件的使用 maven-dependency-plugin是 处理与依赖相关的插件.它有很多可用的goal,大部分是和依赖构建.分析和解决相关的goa ...
随机推荐
- canvas初学 半动态画太极图
可直接复制粘贴运行 <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head&g ...
- R语言集合操作
熟练运用R语言的集合操作在很多时候可以省去for循环,从而提升数据处理效率.废话不多说,集合操作相对简单,贴一段代码就懂了! > A<-: > A [] > B<-,,) ...
- JavaScript 字符串(String)对象
String 对象用于处理已有的字符块. JavaScript 字符串 一个字符串用于存储一系列字符就像 "John Doe". 一个字符串可以使用单引号或双引号: 实例 var ...
- Android简易实战教程--第五十一话《使用Handler实现增加、减少、暂停计数》
转载博客请注明出处:道龙的博客 之前,写过一篇使用异步任务AysncTask实现倒计时的小案例,喜欢的话可以参考博客:Android简易实战教程--第三十三话< AsyncTask异步倒计时&g ...
- MySQL备忘录
1 数据库概念(了解) 1.1 什么是数据库 数据库就是用来存储和管理数据的仓库! 数据库存储数据的优先: l 可存储大量数据: l 方便检索: l 保持数据的一致性.完整性: l 安全,可共享: l ...
- linux C 获取与修改IP地址
主要有两种方法: 一种是用system执行shell命令,如: system("ifconfig usb0 192.168.1.188"); 另一种用ioctl系统调用: int ...
- Dynamics CRM 本地插件注册器连CRMAn unsecured or incorrectly secured fault was received from the other party
今天遇到个问题,在本地打开插件注册器连接到远程CRM服务器时报如下问题 但我在CRM服务器上连接注册器是可以打开的,所以不存在账号权限这类的问题了(当然我用的是超管的账号也不可能存在),最后查询得知是 ...
- ObjectOutputStream 和 ObjectInputStream的使用
一.看一下API文档 ObjectOutputStream : ObjectOutputStream 将 Java 对象的基本数据类型和图形写入 OutputStream.可以使用 ObjectInp ...
- Unity3d导出Recast geomset.txt
Unity3d导出Recast geomset.txt (金庆的专栏) Recast Demo 输入需要 geomset.txt 文件来指定区域类型. 以ObjExporter.cs为基础,编写Uni ...
- 树莓派初体验,安装Ubuntu 14.04 LTS
转载自:http://www.polarxiong.com/archives/%E6%A0%91%E8%8E%93%E6%B4%BE%E5%88%9D%E4%BD%93%E9%AA%8C-%E5%AE ...