Problem UVA506-System Dependencies

Accept:285  Submit:2824

Time Limit: 3000 mSec

Problem Description

Components of computer systems often have dependencies—other components that must be installed before they will function properly. These dependencies are frequently shared by multiple components. For example, both the TELNET client program and the FTP client program require that the TCP/IP networking software be installed before they can operate. If you install TCP/IP and the TELNET client program, and later decide to add the FTP client program, you do not need to reinstall TCP/IP.
For some components it would not be a problem if the components on which they depended were reinstalled; it would just waste some resources. But for others, like TCP/IP, some component configuration may be destroyed if the component was reinstalled.
It is useful to be able to remove components that are no longer needed. When this is done, components that only support the removed component may also be removed, freeing up disk space, memory, and other resources. But a supporting component, not explicitly installed, may be removed only if all components which depend on it are also removed. For example, removing the FTP client program and TCP/IP would mean the TELNET client program, which was not removed, would no longer operate. Likewise, removing TCP/IP by itself would cause the failure of both the TELNET and the FTP client programs. Also if we installed TCP/IP to support our own development, then installed the TELNET client (which depends on TCP/IP) and then still later removed the TELNET client, we would not want TCP/IP to be removed.
We want a program to automate the process of adding and removing components. To do this we will maintain a record of installed components and component dependencies. A component can be installed explicitly in response to a command (unless it is already installed), or implicitly if it is needed for some other component being installed. Likewise, a component, not explicitly installed, can be explicitly removed in response to a command (if it is not needed to support other components) or implicitly removed if it is no longer needed to support another component. Installing an already implicitly-installed component won’t make that component become explicity installed.

 Input

The input file contains several test cases, each of them as described below. The input will contain a sequence of commands (as described below), each on a separate line containing no more than eighty characters. Item names are case sensitive, and each is no longer than ten characters. The command names (DEPEND, INSTALL, REMOVE and LIST) always appear in uppercase starting in column one, and item names are separated from the command name and each other by one or more spaces. All appropriate DEPEND commands will appear before the occurrence of any INSTALL command that uses them. There will be

 Output

For each test case, the output must follow the description below. Echo each line of input. Follow each echoed INSTALL or REMOVE line with the actions taken in response, making certain that the actions are given in the proper order. Also identify exceptional conditions (see Sample Output, below, for examples of all cases). For the LIST command, display the names of the currently installed components in the installation order. No output, except the echo, is produced for a DEPEND command or the line containing END. There will be at most one dependency list per item.

 Sample Input

DEPEND TELNET TCPIP NETCARD
DEPEND TCPIP NETCARD
DEPEND DNS TCPIP NETCARD
DEPEND BROWSER TCPIP HTML
INSTALL NETCARD
INSTALL TELNET
INSTALL foo
REMOVE NETCARD
INSTALL BROWSER
INSTALL DNS
LIST
REMOVE TELNET
REMOVE NETCARD
REMOVE DNS
REMOVE NETCARD
INSTALL NETCARD
REMOVE TCPIP
REMOVE BROWSER
REMOVE TCPIP
END
 
 

 Sample Ouput

DEPEND TELNET TCPIP NETCARD
DEPEND TCPIP NETCARD
DEPEND DNS TCPIP NETCARD
DEPEND BROWSER TCPIP HTML
INSTALL NETCARD
   Installing NETCARD
INSTALL TELNET
   Installing TCPIP
   Installing TELNET
INSTALL foo
   Installing foo
REMOVE NETCARD
   NETCARD is still needed.
INSTALL BROWSER
   Installing HTML
   Installing BROWSER
INSTALL DNS
   Installing DNS
LIST
   NETCARD
   TCPIP
   TELNET
   foo
   HTML
   BROWSER
   DNS
REMOVE TELNET
   Removing TELNET
REMOVE NETCARD
   NETCARD is still needed.
REMOVE DNS
   Removing DNS
REMOVE NETCARD
   NETCARD is still needed.
INSTALL NETCARD
   NETCARD is already installed.
REMOVE TCPIP
   TCPIP is still needed.
REMOVE BROWSER
   Removing BROWSER
   Removing TCPIP
   Removing HTML
REMOVE TCPIP
   TCPIP is not installed.
END

题解:一个比较麻烦的模拟,对于一个软件来说,有依赖和被依赖两种参数,因此用了depend,depend2两个vector将整个图用类似双链表的方式串了起来,这一步操作十分关键,有了双链表就可以方便地找到要删除的元素和要添加的元素。这个题还有一个比较麻烦的地方就是安装有显式和隐式两种,而显式安装的只能够被显式删除,隐式安装的都可以,为了区分这个,引入status数组,0表示未安装,1表示显式,2表示隐式,模拟的时候加一个判断就好,然后还要注意的是安装和删除均为递归操作,那么输出的语句应该放在哪里。其实这个问题很简单,安装时一定要先安装拓扑序中最靠前的软件,因此要递归到底再输出,删除就是翻着来,可以类比树的后序、先序遍历。

 #include <bits/stdc++.h>
using namespace std; const int maxn = +;
vector<int> depend[maxn],depend2[maxn];
vector<int> installed;
map<string,int> ID;
string name[maxn];
int cnt;
int status[maxn]; int find_ID(string &item){
if(!ID.count(item)){
ID[item] = cnt;
name[cnt++] = item;
}
return ID[item];
} bool needed(int id){
for(int i = ;i < depend2[id].size();i++){
if(status[depend2[id][i]]) return true;
}
return false;
} void List(){
for(int i = ;i < installed.size();i++){
cout << " " << name[installed[i]] << endl;
}
} void install(int id,bool toplevel){
if(!status[id]){
for(int i = ;i < depend[id].size();i++){
install(depend[id][i],false);
}
status[id] = toplevel ? : ;
cout << " Installing " << name[id] << endl;
installed.push_back(id);
}
} void Remove(int id,bool toplevel){
if((toplevel || status[id]==) && !needed(id)){
installed.erase(remove(installed.begin(),installed.end(),id),installed.end());
cout << " Removing " << name[id] << endl;
status[id] = ;
for(int i = ;i < depend[id].size();i++){
Remove(depend[id][i],false);
}
}
} int main()
{
//freopen("input.txt","r",stdin);
cnt = ;
string ope;
memset(status,,sizeof(status));
while(getline(cin,ope)){
cout << ope << endl;
stringstream ss(ope);
ss >> ope;
if(ope[] == 'E') break;
string item1,item2;
if(ope[] == 'L') List();
else{
ss >> item1;
int id = find_ID(item1);
if(ope[] == 'D'){
while(ss >> item2){
int id2 = find_ID(item2);
depend[id].push_back(id2);
depend2[id2].push_back(id);
}
}
else if(ope[] == 'I'){
if(status[id]) cout << " " << item1 << " is already installed." << endl;
else install(id,true);
}
else{
if(!status[id]) cout << " " << item1 << " is not installed." << endl;
else if(needed(id)) cout << " " << item1 << " is still needed." << endl;
else Remove(id,true);
}
}
}
return ;
}

UVA506-System Dependencies(拓扑序)的更多相关文章

  1. 【BZOJ-3832】Rally 拓扑序 + 线段树 (神思路题!)

    3832: [Poi2014]Rally Time Limit: 20 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 168  Solved:  ...

  2. BZOJ-4010 菜肴制作 贪心+堆+(拓扑图拓扑序)

    无意做到...char哥还中途强势插入干我...然后据他所言,看了一会题,一转头,我爆了正解....可怕 4010: [HNOI2015]菜肴制作 Time Limit: 5 Sec Memory L ...

  3. hdu5438(2015长春赛区网络赛1002)拓扑序+DFS

    题意:给出一张无向图,每个节点有各自的权值,问在点数为奇数的圈中的点的权值总和是多少. 通过拓扑序的做法标记出所有非圈上的点,做法就是加每条边的时候将两点的入度都加一,然后将所有度数为1的点入队,删去 ...

  4. poj3553 拓扑序+排序贪心

    题意:有多个任务,每个任务有需要花费的时间和最后期限,任务之间也有一些先后关系,必须先完成某个才能开始某个,对于每个任务,如果没有越期,则超时为0,否则超时为结束时间-最后期限,求总超时时间最小的任务 ...

  5. poj2762 强连通+拓扑序

    题意:有 n 个房间,不同房间之间有单向通道,问是否任意两个房间 A .B 都可以从 A 到 B 或从 B 到 A(有一条有就可以). 在这题中,如果一些点是在同一个强连通分量中,那么这些点肯定能够相 ...

  6. poj1420 拓扑序

    题意:给出一个表格,一部分单元格是给定的数字,而另一部分单元格则是一个式子,表示是其他一些单元格的和,让你输出最后计算出的所有格子的数. 因为有些格子需要其他格子先计算出来,所以计算顺序是按照拓扑序的 ...

  7. poj1270 拓扑序(DFS)

    题意:给出将会出现的多个字母,并紧接着给出一部分字母的大小关系,要求按照字典序从小到大输出所有符合上述关系的排列. 拓扑序,由于需要输出所有排列,所以需要使用 dfs ,只要点从小到大遍历就可以实现字 ...

  8. poj1128 拓扑序(DFS)

    题意:给出一张图,它是由一系列字母框按一定顺序从下到上摆放,因此上面的字母框会覆盖一部分下面的字母框,确保每个字母框的四条边都至少会出现一个点,要求输出所有可行的摆放顺序,字典序从小到大输出. 首先可 ...

  9. poj3687 拓扑序

    题意:有编号 1-n 的球,每个球的质量不同,质量从 1 到 n 不等,给出一系列比较,分别是两个编号的球的大小关系,求一个序列满足上述关系,并且从编号 1 开始依次选择可选的最小质量,输出每个球的质 ...

随机推荐

  1. asp.net mvc 碰到 XML 解析错误:找不到根元素 位置

    具体报错信息如下: XML 解析错误:找不到根元素 位置:moz-nullprincipal:{4a1d2b7c-6d07-468e-9df9-2267a0422c93} 行 1,列 1: 网上给出的 ...

  2. java_单词长度

    题目内容: 你的程序要读入一行文本,其中以空格分隔为若干个单词,以‘.’结束.你要输出这行文本中每个单词的长度.这里的单词与语言无关,可以包括各种符号,比如“it's”算一个单词,长度为4.注意,行中 ...

  3. GDB使用技巧

    最近使用GDB比较多,发现除了最常用的run.break.continue.next等命令的基本用法外,还有一些非常有用的命令和用法,能让你更加得心应手地使用GDB,在这里做了一下简单的总结. 1. ...

  4. [转] Mac下 快速写博客的软件 MarsEdit

    正文 marsedit最好. 这东西还是收费的,这里, 我给个注册码: Name: The Blade SN: RSME3-DA4KUN-3EL6Y-MXD2X-LYMT9-6KGX8-4 ~~~~~ ...

  5. linux 系统filezilla无法上传文件 553 Could not create

    做网站过程中遇见了很多问题,解决了但是解决方法过一段时间就会遗忘,整理出来以便以后查看. 响应: 553 Could not create file.错误: 严重文件传输错误 解决方案: 一.必须将站 ...

  6. canvas-2arcTo.html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Archlinux/Manjaro使用笔记-安装配置搜狗输入法步骤

    我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 一.安装qtwebkit-bin软件包解决qtwebkit无法编译安装问题 aurman -S qtwebkit-bin 二.安 ...

  8. react中这些细节你注意过没有?

    react中的一些细节知识点: 1.组件中get的使用(作为类的getter) ES6知识:class类也有自己的getter和setter,写法如下: Class Component { const ...

  9. mysql 之库, 表的简易操作

    一. 库的操作 1.创建数据库 创建数据库: create database 库名 charset utf8;   charset uft8  可选项 1.2 数据库命名规范: 可以由字母.数字.下划 ...

  10. Kotlin入门(15)独门秘笈之特殊类

    上一篇文章介绍了Kotlin的几种开放性修饰符,以及如何从基类派生出子类,其中提到了被abstract修饰的抽象类.除了与Java共有的抽象类,Kotlin还新增了好几种特殊类,这些特殊类分别适应不同 ...