拓扑排序:

两个队列,一个放不须要重新启动入度为0的,一个放须要重新启动入度为0的....从不须要重新启动的队列開始,每弹出一个数就更新下入度,遇到入读为0的就增加到对应队列里,当队列空时,记录重新启动次数+1,交换队列..一直到两个队列都为空

Smart Software Installer

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 78    Accepted Submission(s): 32

Problem Description
The software installation is becoming more and more complex. An automatic tool is often useful to manage this process. An IT company is developing a system management utility to install a set of software packages automatically with the dependencies. They found
that reboot is often required to take effect after installing some software. A software package cannot be installed until all software packages it depends on are installed and take effect. 



In the beginning, they implemented a simple installation algorithm, but the system would reboot many times during the installation process. This will have a great impact on the user experience. After some study, they think that this process can be further optimized
by means of installing as much packages as possible before each reboot.



Now, could you please design and implement this algorithm for them to minimize the number of restart during the entire installation process?

 
Input
The first line is an integer n (1 <= n <= 100), which is the number of test cases. The second line is blank. The input of two test cases is separated by a blank line.



Each test case contains m (1 <= n <= 1000) continuous lines and each line is no longer than 1024 characters. Each line starts with a package name and a comma (:). If an asterisk (*) exists between the package name and the comma, the reboot operation is required
for this package. The remaining line is the other package names it depends on, separated by whitespace. Empty means that there is no dependency for this software. For example, “a: b” means package b is required to be installed before package a. Package names
consist of letters, digits and underscores, excluding other special symbols.



Assume all packages here need to be installed and all referenced packages will be listed in an individual line to define the reboot property. It should be noted that cyclic dependencies are not allowed in this problem.
 
Output
For each test case, you should output a line starting with “Case #: " (# is the No. of the test case, starting from 1) and containing the reboot count for this test case. (Refer to the sample format)
 
Sample Input
2 glibc:
gcc*: glibc uefi*:
gcc*:
raid_util*: uefi
gpu_driver*: uefi
opencl_sdk: gpu_drivergcc
 
Sample Output
Case 1: 1
Case 2: 2
 
Source
 

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <string>
#include <sstream>
#include <map>
#include <queue> using namespace std; const int maxn=2000; map<string,int> mSI;
int nm;
bool restart[maxn];
int degree[maxn]; int hash(string name)
{
int ret=mSI[name];
if(ret==0)
{
mSI[name]=nm++;
ret=nm-1;
}
return ret;
} struct Edge
{
int to,next;
}edge[maxn*maxn]; int Adj[maxn],Size; void init()
{
memset(Adj,-1,sizeof(Adj)); Size=0;
} void add_edge(int u,int v)
{
edge[Size].to=v; edge[Size].next=Adj[u]; Adj[u]=Size++;
} int TUOPU()
{
queue<int> q[2];
int a=0,b=1;
int n=nm-1;
for(int i=1;i<=n;i++)
{
if(degree[i]==0)
{
if(restart[i]==true) q[b].push(i);
else q[a].push(i);
}
}
int time=0;
while(!q[a].empty()||!q[b].empty())
{
while(!q[a].empty())
{
int u=q[a].front(); q[a].pop();
for(int i=Adj[u];~i;i=edge[i].next)
{
int v=edge[i].to;
degree[v]--;
if(degree[v]==0)
{
if(restart[v]==true) q[b].push(v);
else q[a].push(v);
}
}
}
if(q[b].empty()==true) continue;
time++;swap(a,b);
}
return time;
} int main()
{
int T_T,cas=1;
scanf("%d",&T_T);
getchar(); getchar();
while(T_T--)
{
init();
mSI.clear(); nm=1;
memset(restart,false,sizeof(restart));
memset(degree,0,sizeof(degree)); string line,name;
while(getline(cin,line))
{
if(line[0]==0) break;
istringstream sin(line);
sin>>name;
bool flag=false;
int sz=name.size();
if(name[sz-2]=='*')
{
flag=true;
name[sz-2]=0;
name.resize(sz-2);
}
else
{
name[sz-1]=0;
name.resize(sz-1);
} int to=hash(name);
restart[to]=flag;
while(sin>>name)
{
int from=hash(name);
add_edge(from,to);
degree[to]++;
}
}
printf("Case %d: %d\n",cas++,TUOPU());
}
return 0;
}

最新文章

  1. DAO模型
  2. SQL Server下载安装
  3. C#程序使用SQLite数据库
  4. WinForm控件TreeView 只部分节点显示 CheckBox
  5. [原] XAF ListView显示隐藏Footer菜单
  6. 电商大促准备流程v2
  7. KMP算法(转载)
  8. MYSQL 一些用法
  9. could not read data from &#39;/Users/xxxx/myapp-Info.plist&#39;
  10. 【暑假】[实用数据结构]UVa11991 Easy Problem from Rujia Liu?
  11. 使用C#开发ActiveX控件 11
  12. Ajax下载文件(页面无刷新)
  13. [置顶] 内存映射失败MapViewOfFile 失败 返回 8
  14. ubuntu中安装搜狗输入法
  15. Hibernate中的对象有三种状态
  16. 关于IWMS中遇到的问题及解决方法
  17. Js点击按钮下载文件到本地(兼容多浏览器)
  18. The D Programming Language 书评
  19. 《Head First 设计模式》读书笔记
  20. Putty等工具中解决SSH连接超时断开的问题

热门文章

  1. Iterator(迭代器) 和generator
  2. HDU 1667 The Rotation Game (A*迭代搜索)
  3. vue中的methods、computed和watch
  4. 洛谷——P1455 搭配购买
  5. 国庆 day 3 上午
  6. Qt之自定义布局管理器(QBorderLayout)
  7. DDos游戏行业受攻击最多
  8. 3.IntelliJ IDEA 使用详解
  9. tomcat和nginx相互结合的优化调整
  10. 在cncc的最后几天的笔记