POJ 1236 tarjan
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 19613 | Accepted: 7725 |
Description
You are to write a program that computes the minimal number of schools that must receive a copy of the new software in order for the software to reach all schools in the network according to the agreement (Subtask A). As a further task, we want to ensure that by sending the copy of new software to an arbitrary school, this software will reach all schools in the network. To achieve this goal we may have to extend the lists of receivers by new members. Compute the minimal number of extensions that have to be made so that whatever school we send the new software to, it will reach all other schools (Subtask B). One extension means introducing one new member into the list of receivers of one school.
Input
Output
Sample Input
5 2 4 3 0 4 5 0 0 0 1 0
Sample Output
1 2
Source
思路:求一个有向图从几个点出发可以遍历整个图、以及至少加几条边使整张图强联通。
缩点以后,显然入度为0的点的个数就是第一问的答案。
然后第二问答案显然是入度为0和出度为0的个数的最大值,即出入度为0的点间连条边就可以了。
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define ms(a, b) memset(a, b, sizeof(a))
, M = ;
int h[N], p[M], a[M], fa[N], out[N], in[N], dfn[N], low[N];
int scc, top, ts, cnt, sta[N], insta[N];
void add(int x, int y) { p[++cnt] = h[x]; a[cnt] = y; h[x] = cnt; }
void tarjan(int u) {
int i;
dfn[u] = low[u] = ++ts;
sta[++top] = u; insta[u] = ;
for (i = h[u]; i; i = p[i])
if (!dfn[a[i]]) {
tarjan(a[i]);
low[u] = min(low[u], low[a[i]]);
} else if (insta[a[i]])
low[u] = min(low[u], dfn[a[i]]);
if (dfn[u] == low[u]) {
++scc;
do {
i = sta[top--];
insta[i] = ;
fa[i] = scc;
} while (i != u);
}
}
int main() {
int i, j, n, m, x, b;
while (scanf("%d", &n) != EOF) {
ms(dfn, ); ms(); ms(h, ); ms();
ts = cnt = top = scc = ;
; i <= n; ++i)
while (scanf("%d", &x) && x) add(i, x);
; i <= n; ++i)
if (!dfn[i]) tarjan(i);
; i <= n; ++i)
for (j = h[i]; j; j = p[j])
if (fa[i] != fa[a[j]])
++out[fa[i]], ++in[fa[a[j]]];
,a2=;
; i <= scc; ++i) {
if (!out[i]) ++a1;
if (!in[i]) ++a2;
}
) puts("1\n0");
else printf("%d\n%d\n", a2, max(a1, a2));
}
;
}
POJ 1236 tarjan的更多相关文章
- POJ 1236 Tarjan算法
这道题认真想了想.. [ 题目大意:有N个学校,从每个学校都能从一个单向网络到另外一个学校,两个问题 1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件. 2:至少需要添加几条 ...
- POJ 1236 Network of Schools(Tarjan缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16806 Accepted: 66 ...
- POJ 1236 Network of Schools(强连通 Tarjan+缩点)
POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意: 给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...
- Poj 1236 Network of Schools (Tarjan)
题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...
- POJ 1236 Network of Schools - 缩点
POJ 1236 :http://poj.org/problem?id=1236 参考:https://www.cnblogs.com/TnT2333333/p/6875680.html 题意: 有好 ...
- POJ 1236 Network of Schools(强连通分量)
POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...
- 有向图 加最少的边 成为强连通分量的证明 poj 1236 hdu 2767
poj 1236: 题目大意:给出一个有向图, 任务一: 求最少的点,使得从这些点出发可以遍历整张图 任务二: 求最少加多少边 使整个图变成一个强连通分量. 首先任务一很好做, 只要缩点 之后 求 ...
- POJ 1236——Network of Schools——————【加边形成强连通图】
Network of Schools Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u ...
- 【POJ 1236 Network of Schools】强联通分量问题 Tarjan算法,缩点
题目链接:http://poj.org/problem?id=1236 题意:给定一个表示n所学校网络连通关系的有向图.现要通过网络分发软件,规则是:若顶点u,v存在通路,发给u,则v可以通过网络从u ...
随机推荐
- poj 3253 Fence Repair 优先队列
poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...
- 安装nginx+ngx_lua支持WAF防护功能
安装nginx+ngx_lua支持WAF防护功能 nginx lua模块淘宝开发的nginx第三方模块,它能将lua语言嵌入到nginx配置中,从而使用lua就极大增强了nginx的能力.nginx以 ...
- 从入门到放弃,.net构建博客系统(一):系统构建篇
demo:http://tonyblogs.top/ Git源码:https://github.com/Halifa/TonyBlogs 项目采用的技术有:asp.net mvc4 + autofac ...
- Linux下SVN配置
1.安装svnyum install subversion 2.创建版本库fengyu(版本库的名字取来和你web项目的名字相同,否则你在服务器检出后会变成web项目里还有一层版本库的目录,里面才是项 ...
- QT 延时函数设置
QT 的延时函数分为非阻塞延时 和 阻塞型延时 非阻塞延时: void GreenPass3::delaymsec(int msec){ QTime dieTime = QTime::curre ...
- python_求相邻数
什么是相邻数? 比如5,相邻数为4和6,和5相差1的数,连续相差为1的一组数 需求: 遍历inputList 所有数字,取出所有数字,判断是否有相邻数, 不相邻数字 和 相邻数字 都以 “数组”形式 ...
- 开源社交系统ThinkSNS+ 0.7.3研发周报
什么是ThinkSNS+ ThinkSNS(简称TS),一款全平台综合性社交系统,为国内外大中小企业和创业者提供社会化软件研发及技术解决方案,目前最新版本为ThinkSNS+. 亲爱的粉丝,授权客户, ...
- [Python] wxPython 菜单栏控件学习总结(原创)
1.总结 1.大体创建过程 1.创建一个 菜单栏 : menuBar = wx.MenuBar() 相当于这个白色地方,没有File这个菜单 2.创建 菜单 : fileMenu = wx.Menu ...
- JSP 标准标签库(JSTL)(JSP Standard Tag Library)
[1] JSTL简介 > JSTL是JSP的标准标签库 > JSTL为我们提供了一些常用的标签,供我们日常开发使用(if . if...else .遍历 . 日期格式化) ...
- Ubuntu Docker 简单安装 GitLab
相关博文: Ubuntu 简单安装 Docker Ubuntu 简单安装和配置 GitLab 服务器版本 Ubuntu 16.04 LTS. 1. 安装和配置 安装命令: sudo docker ru ...