http://acm.hdu.edu.cn/showproblem.php?pid=5098

软件在安装之后需要重启才能发挥作用,现在给你一堆软件(有的需要重启有的不需要)以及安装这个软件之前需要哪些软件发挥作用,求最少的重启次数

可以看出是拓扑排序,但是需要用两个队列q1,q2分别来存 不需要重启的software 和 需要重启的software。根据题目输入建好图后,按照拓扑序规则,首先将入度的0的点加进队列,不需要重启的进q1,需要的进q2。然后处理q1中的所有节点(即要删除这些点),那么要让与他们相连的节点的入度减1,如果发现减完入度为0,再判断其是否需要重启,并加进q1 or q2。一旦发现q1为空,那么使答案加1(不能继续任何安装即重启一次),把q2中所有元素加入q1,q2清空。如此循环直到q1,q2均为空即可。

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define clr0(x) memset(x,0,sizeof(x))
#define clr1(x) memset(x,-1,sizeof(x))
#define eps 1e-9
const double pi = acos(-1.0);
typedef long long LL;
const int maxn = 1050;
map <string , int> mp;
int ecnt,cnt;
int reboot[maxn],out[maxn],in[maxn];
struct edge{
int v,next;
}e[maxn*maxn];
int head[maxn],vis[maxn];
void init()
{
mp.clear();
ecnt = cnt = 0;
clr0(in),clr0(out),clr0(reboot);
clr1(head),clr0(vis);
}
void add(int u,int v)
{
e[ecnt].next = head[u];
e[ecnt].v = v;
head[u] = ecnt++;
}
int topo(){
queue<int> q1,q2;// 不需要重启的进q1,需要的进q2
for(int i = 1;i <= cnt;++i)if(in[i] == 0){
if(reboot[i] == 0)
q1.push(i);
else
q2.push(i);
}
int ans = 0;
while(!q1.empty() || !q2.empty()){
if(q1.empty() && !q2.empty()){
ans++;
while(!q2.empty()){
q1.push(q2.front());
q2.pop();
}
}
while(!q1.empty()){
int u = q1.front();q1.pop();
vis[u] = 1;
for(int i = head[u];i != -1;i = e[i].next){
int v = e[i].v;
in[v]--;
if(in[v] == 0){
if(reboot[v] == 0)
q1.push(v);
else
q2.push(v);
}
}
}
}
return ans;
}
int main()
{
string s;
char name[1050];
int _,cas = 1;RD(_);getchar();getchar();
while(_--)
{
init();
while(getline(cin,s)){
if(s[0] == '\0')
break;
istringstream sin(s);
sin >> name;
int len = strlen(name),flag = 0;
if(name[len - 2] == '*'){
flag = 1;
name[len - 2] = '\0';
}else
name[len - 1] = '\0';
string id = name,_id;
if(mp.find(id) == mp.end())
mp[id] = ++cnt;
reboot[mp[id]] = flag;
while(sin >> _id){
if(mp.find(_id) == mp.end())
mp[_id] = ++cnt;
add(mp[_id],mp[id]);
out[mp[_id]]++;
in[mp[id]]++;
}
}
printf("Case %d: %d\n",cas++,topo());
}
}

hdu 5098 双队列拓扑排序的更多相关文章

  1. ACM/ICPC 之 数据结构-邻接表+DP+队列+拓扑排序(TSH OJ-旅行商TSP)

    做这道题感觉异常激动,因为在下第一次接触拓扑排序啊= =,而且看了看解释,猛然发现此题可以用DP优化,然后一次A掉所有样例,整个人激动坏了,哇咔咔咔咔咔咔咔~ 咔咔~哎呀,笑岔了- -|| 旅行商(T ...

  2. 题解报告:hdu 2647 Reward(拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 Problem Description Dandelion's uncle is a boss ...

  3. HDU 5811 Colosseo(拓扑排序+单调DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5811 [题目大意] 给出 一张单向图,现在将其划分成了两个部分,问划分之后的点是否分别满足按照一定 ...

  4. HDU 4857 (反向拓扑排序 + 优先队列)

    题意:有N个人,M个优先级a,b表示a优先于b.而且每一个人有个编号的优先级.输出顺序. 思路来自:与PKU3687一样 在主要的拓扑排序的基础上又添加了一个要求:编号最小的节点要尽量排在前面:在满足 ...

  5. HDU 2647 Reward(拓扑排序+判断环+分层)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题目大意:要给n个人发工资,告诉你m个关系,给出m行每行a b,表示b的工资小于a的工资,最低工 ...

  6. HDU 4857 逃生 【拓扑排序+反向建图+优先队列】

    逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...

  7. hdu 1811(缩点+拓扑排序+并查集)

    Rank of Tetris Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. HDU 4857 逃生(拓扑排序)

    拓扑排序 一.定义 对一个有向无环图(Directed Acyclic Graph简称DAG)G进行拓扑排序,是将G中所有顶点排成一个线性序列,使得图中任意一对顶点u和v,若<u,v> ∈ ...

  9. hdu 3231 Box Relations (拓扑排序)

    Box Relations Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

随机推荐

  1. TableView使用CATransform3D特效动画

    效果一: 在代理方法中实现: - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell fo ...

  2. Spring:No bean named 'beanScope' is defined

    初学Spring,“No bean named 'beanScope' is defined”这个问题困扰了我好几个小时,查资料无果后,重写好几遍代码后发现问题居然是配置文件不能放在包里...要放在s ...

  3. SQL Server常用技巧

    1:在SQL语句中,将存储过程结果集(表)存入到临时表中 insert into #tmp EXEC P_GET_AllChildrenComany '80047' 说明:#tmp要提前创建好 2:字 ...

  4. jQuery图片渐变特效的简单实现

    (document).ready(function() {(document).ready(function() {("div.baba").mouseleave(function ...

  5. 將後台的Json數據返回到前台

    前台JS代碼 $.post('/Book/GetBookClassIDByName', { BookName: "旅遊手冊" }, function (data) { if (da ...

  6. 用BlendFunc实现舞台灯光和刮刮卡效果

    [转]http://code.lovemiao.com/?p=136#more-136 之前写过一篇<不规则形状按钮的点击判定>,利用了CCRenderTexture创建一块画布,可以在上 ...

  7. information_schema系列之字符集校验(CHARACTER_SETS,COLLATIONS,COLLATION_CHARACTER_SET_APPLICABILITY)

    1:CHARACTER_SETS 首先看一下查询前十条的结果: root@localhost [information_schema]>select * from CHARACTER_SETS ...

  8. ajax请求后根据条件进行页面跳转

    $.ajx({ url: "@Url.Action("DetectCorporationCompetencyCreated", "DataBase") ...

  9. 控件的相对位置与绝对位置-UI界面编辑器(SkinStudio)教程

    绝对位置: 相对位置: 相对位置配合Anchor属性使用 例如Anchor属性选择left|top(即相对位置的left和top保持不变) 窗口大小改变前: 窗口大小改变后: 可以看到控件相对位置的l ...

  10. 编程范式 episode3 and 4,5

    episode 3 --storage structure. ampersand operate with asterisk --library function episode 4 --generi ...