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 ...
随机推荐
- angular学习(六)-- Filter
2.6 过滤器:Filter 内置过滤器 currency number date json uppercase lowercase orderBy limitTo filter 自定义过滤器
- 计蒜客模拟赛D2T1 蒜头君的兔子:矩阵快速幂
题目链接:https://nanti.jisuanke.com/t/16442 题意: 有个人在第一年送了你一对1岁的兔子.这种兔子刚生下来的时候算0岁,当它在2~10岁的时候,每年都会生下一对兔子, ...
- IPython+:一个交互式计算和开发环境
一. IPython基础 代码自动补全:Tab键 可补全内容包括:变量名.函数名.成员变量函数.目录文件 内省(Itrospection) 在变量名之前或之后加上问号(?),这样可以显示这个对象的相关 ...
- 腾讯SNG电面
第一次电面. 前半段基本闲聊,问题也记得不太清楚了. 自我介绍. 为什么想去上海工作?除了职业方面有其他原因吗?我猜出来面试官想问私人问题了,你真的可以直接问的,拐弯抹角了好久...有什么爱好.特长. ...
- kali linux安装到U盘并增加persistence功能
这里写两种方法,其实本质是一样的,都是利用grub4dos重新写mbr,同时建立persistence分区并写入配置文件. 一, 1,首先将U盘分区,分两个即可,一个可当U盘使用存储数据,另一个则用来 ...
- 自定义组件-BreadcrumbTreeView 的使用
一.问题概述 树形结构是开发中常用的一种组织数据的结构,不少平台也提供了对应的控件.而在android平台中,出于使用手指操作树形结构不是很方便的原因,并没有提供树形结构控件.但在实际应用中,不可避免 ...
- JSP是什么?
JSP [1] 简介 > HTML - HTML擅长显示一个静态的网页,但是不能调用Java程序. > Se ...
- 在使用pydelicious时出现HTTP Error 500: Internal Server Error的错误的解决方法:
问题:在学习<集体智慧编程>的过程中,第二章中如果你遇到了pydelicious.PyDeliciousException: HTTP Error 500: Internal Server ...
- 复杂JSON反序列化为类对象
有3种常用的反序列化库,gson和fastjson都很棒,json-lib有很大的局限性不推荐使用! 1. net.sf.json(json-lib) 只能用于解析简单的JSON,稍微复杂点的例如,类 ...
- 地图性能测试利器PerfQA Analyzer
PerfQA Analyzer作为一个地图性能测试工具,能针对ArcGIS 技术堆栈的系统进行性能问题的排查,用于系统试运行阶段的压力测试以及在线系统运维.大大减轻了GIS系统管理员的性能调优工作压力 ...