双拓扑排序 HDOJ 5098 Smart Software Installer
/*
双拓扑排序:抄的,以后来补
详细解释:http://blog.csdn.net/u012774187/article/details/40736995
*/
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <ctime>
#include <cstdlib>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll; const int MAXN = 1e3 + ;
const int INF = 0x3f3f3f3f;
const double PI = acos (-1.0);
const double EPS = 1e-;
struct Edge
{
int v, nxt;
}e[MAXN * MAXN];
int in[MAXN], out[MAXN];
int re[MAXN], head[MAXN];
bool vis[MAXN];
map<string, int> M;
vector<int> G[MAXN];
int ecnt, cnt; int TopoSort(void)
{
queue<int> Q1, Q2; //Q1 !re Q2 re
for (int i=; i<=cnt; ++i)
{
if (!in[i])
{
if (!re[i]) Q1.push (i);
else Q2.push (i);
}
} int ans = ;
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] = true;
for (int i=head[u]; i!=-; i=e[i].nxt)
{
int v = e[i].v;
if (!(--in[v]))
{
if (!re[v]) Q1.push (v);
else Q2.push (v);
}
}
}
} return ans;
} void init(void)
{
M.clear ();
ecnt = cnt = ;
memset (in, , sizeof (in));
memset (out, , sizeof (out));
memset (re, , sizeof (re));
memset (head, -, sizeof (head));
memset (vis, false, sizeof (vis));
} void add_edge(int u, int v)
{
e[ecnt].nxt = head[u];
e[ecnt].v = v;
head[u] = ecnt++;
} int main(void) //HDOJ 5098 Smart Software Installer
{
//freopen ("I.in", "r", stdin); string s;
char name[];
int n, cas = ; scanf ("%d", &n); getchar (); getchar ();
while (n--)
{
init ();
while (getline (cin, s))
{
if (s[] == '\0') break;
istringstream sin (s);
sin >> name;
int len = strlen (name); int flag = ;
if (name[len-] == '*') {flag = ; name[len-] = '\0';}
else name[len-] = '\0'; string u = name, v;
if (M.find (u) == M.end ())
M[u] = ++cnt;
re[M[u]] = flag; while (sin >> v)
{
if (M.find (v) == M.end ())
M[v] = ++cnt;
add_edge (M[v], M[u]);
out[M[v]]++;
in[M[u]]++;
}
}
printf ("Case %d: %d\n", ++cas, TopoSort ());
} return ;
} /*
Case 1: 1
Case 2: 2
*/
双拓扑排序 HDOJ 5098 Smart Software Installer的更多相关文章
- HDOJ 5098 Smart Software Installer 拓扑排序
拓扑排序: 两个队列,一个放不须要重新启动入度为0的,一个放须要重新启动入度为0的....从不须要重新启动的队列開始,每弹出一个数就更新下入度,遇到入读为0的就增加到对应队列里,当队列空时,记录重新启 ...
- hdu 5098 双队列拓扑排序
http://acm.hdu.edu.cn/showproblem.php?pid=5098 软件在安装之后需要重启才能发挥作用,现在给你一堆软件(有的需要重启有的不需要)以及安装这个软件之前需要哪些 ...
- hdu 3342 Legal or Not(拓扑排序) HDOJ Monthly Contest – 2010.03.06
一道极其水的拓扑排序……但是我还是要把它发出来,原因很简单,连错12次…… 题意也很裸,前面的废话不用看,直接看输入 输入n, m表示从0到n-1共n个人,有m组关系 截下来m组,每组输入a, b表示 ...
- hdoj 4324 Triangle LOVE【拓扑排序判断是否存在环】
Triangle LOVE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- HDOJ 1285 确定比赛名次(拓扑排序)
Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委 ...
- 拓扑排序/DFS HDOJ 4324 Triangle LOVE
题目传送门 题意:判三角恋(三元环).如果A喜欢B,那么B一定不喜欢A,任意两人一定有关系连接 分析:正解应该是拓扑排序判环,如果有环,一定是三元环,证明. DFS:从任意一点开始搜索,搜索过的点标记 ...
- HDOJ 2647 Reward 【逆拓扑排序+分层】
题意:每一个人的基础工资是888. 因为一部分人要显示自己水平比較高,要求发的工资要比其它人中的一个人多.问你能不能满足他们的要求,假设能的话终于一共要发多少钱,假设不能就输出-1. 策略:拓扑排序. ...
- 求拓扑排序的数量,例题 topcoder srm 654 div2 500
周赛时遇到的一道比较有意思的题目: Problem Statement There are N rooms in Maki's new house. The rooms are number ...
- POJ 2762 Going from u to v or from v to u? (强连通分量缩点+拓扑排序)
题目链接:http://poj.org/problem?id=2762 题意是 有t组样例,n个点m条有向边,取任意两个点u和v,问u能不能到v 或者v能不能到u,要是可以就输出Yes,否则输出No. ...
随机推荐
- OpenCV2.3.1在CentOS6.5下的安装
安装的linux版本号是centos6.5.选择的是opencv2.3.1.不是非常新的版本号. 由于在安装opencv2.4.9的时候.make的过程中出现了问题. 一:安装依赖包 依赖包用yum安 ...
- UVA - 11019 Matrix Matcher hash+KMP
题目链接:传送门 题解: 枚举每一行,每一行当中连续的y个我们hash 出来 那么一行就是 m - y + 1个hash值,形成的一个新 矩阵 大小是 n*(m - y + 1), 我们要找到x*y这 ...
- GET和POST解析
Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE.URL全称是资源描述符,我们可以这样认为:一个URL地址,它用于描述一个网络上的资源,而HTTP ...
- MTK平台下Battery驱动分析
主要涉及代码: Kernel: kernel-3.10\drivers\power\mediatek\ kernel-3.10\drivers\misc\mediatek\mach\mt6580\&l ...
- YTU 2429: C语言习题 学生成绩输入和输出
2429: C语言习题 学生成绩输入和输出 时间限制: 1 Sec 内存限制: 128 MB 提交: 1897 解决: 812 题目描述 编写一个函数print,打印一个学生的成绩数组,该数组中有 ...
- codeforces 441B. Valera and Fruits 解题报告
题目链接:http://codeforces.com/problemset/problem/441/B 题目意思:有 n 棵fruit trees,每课水果树有两个参数描述:水果成熟的时间和这棵树上水 ...
- Vue安装及插件Vue Devtools
vue安装: # 最新稳定版 $ npm install vue # 全局安装 vue-cli $ npm install --global vue-cli # 创建一个基于 webpack 模板的新 ...
- [Selenium] Android 中旋转屏幕,触摸,滚动
package com.learingselenium.android; import junit.framework.TestCase import org.openqa.selenium.Rota ...
- bzoj 5072 小A的树 —— 树形DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=5072 由于对于一个子树,固定有 j 个黑点,连通块大小是一个连续的范围: 所以记 f[i][ ...
- ORA-01843: not a valid month 错误
转自:https://blog.csdn.net/jetluning/article/details/48785599 insert into ......的场合发生ORA-01843: not a ...