UVA506-System Dependencies(拓扑序)
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 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(拓扑序)的更多相关文章
- 【BZOJ-3832】Rally 拓扑序 + 线段树 (神思路题!)
3832: [Poi2014]Rally Time Limit: 20 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 168 Solved: ...
- BZOJ-4010 菜肴制作 贪心+堆+(拓扑图拓扑序)
无意做到...char哥还中途强势插入干我...然后据他所言,看了一会题,一转头,我爆了正解....可怕 4010: [HNOI2015]菜肴制作 Time Limit: 5 Sec Memory L ...
- hdu5438(2015长春赛区网络赛1002)拓扑序+DFS
题意:给出一张无向图,每个节点有各自的权值,问在点数为奇数的圈中的点的权值总和是多少. 通过拓扑序的做法标记出所有非圈上的点,做法就是加每条边的时候将两点的入度都加一,然后将所有度数为1的点入队,删去 ...
- poj3553 拓扑序+排序贪心
题意:有多个任务,每个任务有需要花费的时间和最后期限,任务之间也有一些先后关系,必须先完成某个才能开始某个,对于每个任务,如果没有越期,则超时为0,否则超时为结束时间-最后期限,求总超时时间最小的任务 ...
- poj2762 强连通+拓扑序
题意:有 n 个房间,不同房间之间有单向通道,问是否任意两个房间 A .B 都可以从 A 到 B 或从 B 到 A(有一条有就可以). 在这题中,如果一些点是在同一个强连通分量中,那么这些点肯定能够相 ...
- poj1420 拓扑序
题意:给出一个表格,一部分单元格是给定的数字,而另一部分单元格则是一个式子,表示是其他一些单元格的和,让你输出最后计算出的所有格子的数. 因为有些格子需要其他格子先计算出来,所以计算顺序是按照拓扑序的 ...
- poj1270 拓扑序(DFS)
题意:给出将会出现的多个字母,并紧接着给出一部分字母的大小关系,要求按照字典序从小到大输出所有符合上述关系的排列. 拓扑序,由于需要输出所有排列,所以需要使用 dfs ,只要点从小到大遍历就可以实现字 ...
- poj1128 拓扑序(DFS)
题意:给出一张图,它是由一系列字母框按一定顺序从下到上摆放,因此上面的字母框会覆盖一部分下面的字母框,确保每个字母框的四条边都至少会出现一个点,要求输出所有可行的摆放顺序,字典序从小到大输出. 首先可 ...
- poj3687 拓扑序
题意:有编号 1-n 的球,每个球的质量不同,质量从 1 到 n 不等,给出一系列比较,分别是两个编号的球的大小关系,求一个序列满足上述关系,并且从编号 1 开始依次选择可选的最小质量,输出每个球的质 ...
随机推荐
- Django之模板层
在一个项目里面有一个专门放模板的文件夹Templates,有一个专门放视图的文件views,而且我们大多给浏览器响应的都应该是一个完整的页面,也就是读取的是一个HTML文件,然后再返回给浏览器.但我们 ...
- 编写计算器程序学习JS责任链模式
设计模式中的责任链模式能够很好的处理程序过程的逻辑判断,提高程序可读性. 责任链模式的核心在于责任链上的元素判断能够处理该数据,不能处理的话直接交给它的后继者. 计算器的基本样式: 通过div+css ...
- Yarn的运行原理(执行流程)
服务功能 ResouceManager: 1.处理客户端的请求 2.启动和监控ApplicationMaster 3.监控nodemanager 4.资源的分配和调度 ...
- [android] 手机卫士手机定位的原理
手机定位的三种方式:网络定位,基站定位,GPS定位 网络定位,手机连上wifi 2g 3g的时候,手机会有一个ip,误差很大 基站定位,精确度与基站的多少有关,几十米到几公里的误差 GPS定位,至少需 ...
- java-上转型对象&抽象类-学习记录
上转型对象: 如果B类是A类的子类(或间接子类),当用子类创建对象b并将这个对象的引用放到父类对象a中时,如: A a; a = new b() 或 A a;B b = new B();a = b; ...
- 【协议】4、http状态码
10.4 客户错误 4xx 状态代码4xx类是专门使用在客户看上去错误的情形下的.除非当应答一个HEAD请求时,服务器因该有一个包括错误情形的解释的实体,以及它是否一个临时或者终了的条件.这些状态编码 ...
- spring boot 之 Mybatis 配置
############################## mybatis配置 ######################################mybatis.configuration ...
- 1474 十进制转m进制
1474 十进制转m进制 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题解 题目描述 Description 将十进制数n转换成m进制数 m ...
- 【读书笔记】iOS-Web应用程序的自动化测试
seleniumHQ:https://github.com/seleniumhq/selenium Appium:https://github.com/appium/appium 参考资料:<i ...
- 方差variance, 协方差covariance, 协方差矩阵covariance matrix
https://www.jianshu.com/p/e1c8270477bc?utm_campaign=maleskine&utm_content=note&utm_medium=se ...