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. stark组件之多级过滤

    一.引子 在我们浏览很多页面时,会发现一般情况下都有一个分类的功能,而且还是多个类别同时控制,这就是多级过滤.如下图: 一行代表一个类别,第一行就是展示了所有的出版社,选中后就会以出版社分类,第二行就 ...

  2. Extjs 项目中常用的小技巧,也许你用得着(2)

    接着来,也是刚刚遇到的 panel怎么进行收缩 collapsible: true, 这会panel就会出现这个 点这个就可以收缩了 panel怎么随便拉伸,也就是让那个小黑三角出现 split: t ...

  3. Vue动态新增对象属性

    Vue.set( target, key, value ) 参数: {Object | Array} target {string | number} key {any} value 返回值:设置的值 ...

  4. Java容器类源码分析前言之集合框架结构(基于JDK8)

    一.基本概念 Java容器类库的用途是"保存对象",容器库类分为两个不同的分支. 1.Collection.可以保存一个或多个对象,将其保存为一个序列.Collection又可以细 ...

  5. 了解java虚拟机—G1回收器(9)

    G1(Garbage-First)回收器是在JDK1.7中正式使用的全新垃圾回收器,G1拥有独特的垃圾回收策略,从分代上看,G1依然属于分代垃圾回收器,它会区分年代和老年代,依然有eden和survi ...

  6. 设计模式之责任链模式(Chain of Responsibility )

    责任链的目的是通过特定的设计对请求者和接收者之间进行解耦,请求者调用操作的对象,接收者接收请求并执行相关操作,通过解耦请求者不需要关心接收者的接口,同时也可增强职责的灵活性,通过改变链内的成员或调用次 ...

  7. nginx报错zero size shared memory zone one

    为了限速,在虚拟主机中加上了一个参数:limit_conn one 1:结果导致重启nginx报错: zero size shared memory zone "one"解决办法是 ...

  8. 2018-01-15 Antlr4: 修改语法规则更接近普通BNF格式

    经 @沈默 在上文Antlr4添加中文变量赋求值,括号,各种问题评论中指出, 语法规则描述依赖于Antlr4生成的语法分析器的默认分析方法, 比如运算符的左联系, 以及优先级处理等等. 于是将语法修改 ...

  9. lcn 分布式事务协调者集群原理

    lcn集群原理图: 1.实现原理: 1.1 LCN获取tm协调者注册地址 1. LCN客户端项目启动的时候,首先会注册到事务协调者中去,然后读取配置文件tm事务协调者的注册地址. 2. 向该事务协调者 ...

  10. CSS盒模型的介绍

    CSS盒模型的概念与分类      CSS盒模型就是一个盒子,封装周围的HTML元素,它包括内容content.边框border.内边距padding.外边距margin. CSS盒模型分为标准模型和 ...