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. Django之模板层

    在一个项目里面有一个专门放模板的文件夹Templates,有一个专门放视图的文件views,而且我们大多给浏览器响应的都应该是一个完整的页面,也就是读取的是一个HTML文件,然后再返回给浏览器.但我们 ...

  2. T-SQL :TOP和OFFSET-FETCH筛选 (五)

    通过were和having条件可以对数据进行筛选,那么如何通过排序对数据进行筛选呢? 1.TOP筛选 用于限制查询返回行数或者行数的百分比. 例如 我们对订单表筛选最近产生的订单5条 ) orderi ...

  3. Vue+Highcharts完全使用

    创建Comp组件 <template> <div class="x-bar"> <div :id="id" :option=&qu ...

  4. Django Rest Framework之权限

    基本代码结构 url.py: from django.conf.urls import url, include from app import views urlpatterns = [ url(r ...

  5. 使用git连接本地和远程github

    使用git连接本地和远程github 网上很多github的流程比较乱,自己尝试整理了一下,主要是步骤较为清晰,如果有不清楚的可详细进行搜索对比 1. 申请和设置github https://gith ...

  6. vue项目sql图片动态路径引用问题

    最近遇到一个vue动态图片路径的引用问题?明明路径是正确的但是却渲染不出图片!先看我慢慢说来!! 1.当我们把图片的路径放置在data(){return:{}}中的数组中的时候,然后通过v-for循环 ...

  7. Electron 应用入门 (参考官方示例)

    Electron 应用 1.去官方下案例 # 克隆示例项目的仓库 $ git clone https://github.com/electron/electron-quick-start # 进入这个 ...

  8. 用JS来实现的第一个简单游戏 :贪吃蛇

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

  9. ionic 开发解决ios上qq客服链接不跳转或者跳转到appstore

    不能跳转的情况需要 在ionic项目根目录下,打开config.xml文件,在<access origin="*" />后添加<allow-navigation ...

  10. C# winform三种定时方法

    1. 直接用winform 的 timers 拖控件进去 代码 public partial class Form1 : Form     {         public Form1()       ...