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 ...
随机推荐
- OpenOfice将offic转为pdf并且在web显示
1.将office首先要安装OpenOfice,傻瓜式安装就好了,之后可以使用下列代码将word转为pdf.这个需要导入jodconverter-2.2.2里的 ja r包 import java.i ...
- 程序设计入门——C语言 习题汇总
<img width="108" height="40" alt="浙江大学" src="http://imgsize.ph ...
- C#快速入门
一.简介 1.C#是由Anders Hejlsberg和他的团队在.Net框架开发期间开发的:是.Net框架的一部分. C#是专为公共语言基础结构(CLI)设计的,CLI由可执行代码和运行时环境组成, ...
- table之thead兼容
今天遇到一个小bug,是关于table中thead,tbody,tfoot的兼容问题: 在开发的时候为了方便写样式,我就把表格的标题部分关于th的内容放在了thead中,当然了,我也没有写tbody和 ...
- 社群系统 ThinkSNS+ 中如何利用 Laravel 表单验证来验证用户名的?(我朝独有需求,两个字母占一个汉字。。。)
ThinkSNS+后端框架使用laravel,每周和 laravel master 保持同步,而后台和 html 5 则采用 vue 开发.语言特性方面,采用 php 7 的严格模式. 言归正传,之所 ...
- tomcat websocket 实现网页在线即时聊天
背景介绍 近一个月完成了公司的一个项目,负责即时聊天部分 寻找了一下,决定使用websocket,要问原因的话,因为tomcat 自带相关消息收发的API,用起来方便 闲话少叙,进入实现步骤 使用工具 ...
- Oracle 11g完全卸载方案(注册表清理)
1.如果数据库配置了自动存储管理(ASM),应该先删除聚集同步服务CSS(Cluster Synchronization Services). 删除CSS服务的方法是在DOS命令行中执行如下命令: ...
- Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead
在某个activity中使用toolbar的使用,必须是在相应的application主题中也设置 <item name="windowNoTitle">true< ...
- [mark] first shellcode
好久没写博客了,今天终于搞出了第一个shellcode,以此mark一下. #include "stdafx.h" #include <Windows.h> #incl ...
- Fedora25和win10双系统安装及使问题汇总
安装问题汇总 1.U盘引导制作后,开机出现":Assuming driver cache: write through" 解决方案:经过排查后,怀疑是U盘启动制作出了问题,后来查阅 ...