Poj 1236 Network of Schools (Tarjan)
题目链接:
题目描述:
有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个网络才能够让所有学校被网络覆盖?2:至少要加几条线路就能做到在任意一个学校安装网络都可以覆盖全部学校?
解题思路:
先用Tarjan对强连通分量进行缩点,然后对缩点以后的图进行处理,统计图中节点出度为零的有多少,入度为零的有多少个?
因为入度为零的点不能由其他的点到达,在每个入度为零的节点安装网络可以让所有学校被网络覆盖。
全部学校在一个强连通图里面,就可以实现第二问,因为出度为零的节点不能到达其他节点,入度为零的点不能由其他节点进来,所以要对出度为零加出度,入度为零的便加入度即可。PS:当全图只有一个连通块的时候应该不用加边的。
#include <cstring>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
const int maxn = ;
struct node
{
int to ,next;
} edge[maxn*maxn];
int head[maxn], dfn[maxn], low[maxn], in[maxn], out[maxn], id[maxn];
int tot, cnt, top, time, instack[maxn], stack[maxn];
void init ()
{
tot = top = ;
cnt = time = ;
memset (head, -, sizeof(head));
memset (dfn, , sizeof(dfn));
memset (low, , sizeof(low));
memset (in, , sizeof(in));
memset (out, , sizeof(out));
memset (id, , sizeof(id));
memset (instack, , sizeof(instack));
memset (stack, , sizeof(stack));
}
void Add (int from, int to)
{
edge[tot].to = to;
edge[tot].next = head[from];
head[from] = tot ++;
}
void Tarjan (int u)
{
dfn[u] = low[u] = ++time;
instack[u] = ;
stack[top ++] = u;
for (int i=head[u]; i!=-; i=edge[i].next)
{
int v = edge[i].to;
if (!dfn[v])
Tarjan (v);
if (instack[v])
low[u] = min (low[u], low[v]);
}
if (dfn[u] == low[u])
{
cnt ++;
while ()
{
int v = stack[--top];
instack[v] = ;
id[v] = cnt;
if (v == u)
break;
}
}
}
int main ()
{
int n;
while (scanf ("%d", &n) != EOF)
{
init ();
for (int i=; i<=n; i++)
{
int to;
while (scanf ("%d", &to), to)
Add (i, to);
}
for (int i=; i<=n; i++)
if (!dfn[i])
Tarjan (i);
for (int i=; i<=n; i++)
for (int j=head[i]; j!=-; j=edge[j].next)
{
int v = id[i];
int u = id[edge[j].to];
if (v != u)
{
out[v] ++;
in[u] ++;
}
}
int In, Out;
In = Out = ;
for (int i=; i<=cnt; i++)
{
if (!in[i])
In ++;
if (!out[i])
Out ++;
}
if (cnt == )
printf ("1\n0\n");
else
printf ("%d\n%d\n", In, max(In, Out));
}
return ;
}
Poj 1236 Network of Schools (Tarjan)的更多相关文章
- POJ 1236 Network of Schools (Tarjan + 缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12240 Accepted: 48 ...
- POJ 1236 Network of Schools Tarjan缩点
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22729 Accepted: 89 ...
- POJ 1236 Network of Schools(强连通 Tarjan+缩点)
POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意: 给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...
- POJ 1236 Network of Schools(强连通分量)
POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...
- [tarjan] poj 1236 Network of Schools
主题链接: http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K To ...
- poj 1236 Network of Schools(连通图入度,出度为0)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- poj 1236 Network of Schools(又是强连通分量+缩点)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- 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+缩点)
Network of Schools Description A number of schools are connected to a computer network. Agreements h ...
随机推荐
- org.osgi.framework.BundleException: Exception in org.eclipse.core.resources.ResourcesPlugin.start()
http://freestyle21.cn 不知什么时候,启动eclipse的时候就一直不行,说是an error ..我查了下log 报错:org.osgi.framework.BundleExce ...
- ZOJ 2770_Burn the Linked Camp
题意: 给定每个兵营的最大容量,以及第i到第j个兵营至少有多少个士兵,问所有兵营一共至少有多少个士兵? 分析: 差分约束系统,注意 第i到第j至少有k个 第i到第j最多有最大容量之和个 每个兵营至少有 ...
- Divide Two Integers(模拟计算机除法)
Divide two integers without using multiplication, division and mod operator. 由于不能用乘号,除号,和取余.那么一个数除另外 ...
- spring boot 学习-创建方式
spring boot是什么 spring boot 是一个快速开发框架,适合小白快速上手开发,它集成了很多优秀的和常用的第三方框架,它简化了xml配置,完全采用注解方式,内部集成了Tomcat.Je ...
- Java面试题解析(一)
本文是面试题目系列的第一篇,题目主要来自 GitChat 上一个课程,在此特别记录自身想的答案和在网上查询的结果,分享出来,大家共同学习.如果有不对的地方,欢迎大家指出. Java基础 JDK 和 J ...
- Java内存分配与参数传递
JAVA中方法的参数传递方式只有一种:值传递. JAVA内存分配: 1.栈:存放 基本类型的数据.对象的引用(类似于C语言中的指针) 2.堆:存放用new产生的数据 3.静态域:存放在对象中用stat ...
- JDBC的结果集
以下内容引用自http://wiki.jikexueyuan.com/project/jdbc/result-sets.html: SQL语句从数据库查询中获取数据,并将数据返回到结果集中.SELEC ...
- 图片在 canvas 中的 选中/平移/缩放/旋转,包含了所有canvas的2D变化,让你认识到数学的重要性
1.介绍 canvas 已经出来好久了,相信大家多少都有接触. 如果你是前端页面开发/移动开发,那么你肯定会有做过图片上传处理,图片优化,以及图片合成,这些都是可以用 canvas 实现的. 如果你是 ...
- 采用jmeter和泛化测试dubbo服务接口
采用jmeter和泛化测试dubbo服务接口 http://blog.csdn.net/linuu/article/details/54313560
- 在gentoo中打开tomcat的远程调试开关
在一般象gentoo等发行版中,系统安装tomcat这类软件后会产生一些启动脚本, 比如是/etc/init.d/tomcat-7, 启动方式与原始的tomcat不太一样.在gentoo中,假设须要远 ...