题目链接:

  Poj 1236 Network of Schools

题目描述:

  有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)的更多相关文章

  1. POJ 1236 Network of Schools (Tarjan + 缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12240   Accepted: 48 ...

  2. POJ 1236 Network of Schools Tarjan缩点

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22729   Accepted: 89 ...

  3. POJ 1236 Network of Schools(强连通 Tarjan+缩点)

    POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意:  给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...

  4. POJ 1236 Network of Schools(强连通分量)

    POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...

  5. [tarjan] poj 1236 Network of Schools

    主题链接: http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K To ...

  6. poj 1236 Network of Schools(连通图入度,出度为0)

    http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  7. poj 1236 Network of Schools(又是强连通分量+缩点)

    http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  8. POJ 1236 Network of Schools(Tarjan缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16806   Accepted: 66 ...

  9. poj 1236 Network of Schools(tarjan+缩点)

    Network of Schools Description A number of schools are connected to a computer network. Agreements h ...

随机推荐

  1. vagrant的学习 之 ThinkPHP5.1

    vagrant的学习 之 ThinkPHP5.1 本文根据慕课网的视频教程练习,感谢慕课网! 慕课视频学习地址:https://www.imooc.com/video/14218. 慕课的参考文档地址 ...

  2. [bzoj1978][BeiJing2010]取数游戏 game_动态规划_质因数分解

    取数游戏 game bzoj-1978 BeiJing-2010 题目大意:给定一个$n$个数的$a$序列,要求取出$k$个数.假设目前取出的数是$a_j$,那么下次取出的$a_k$必须保证:$j&l ...

  3. 关于SQL SERVER导出数据的问题!

    前面一段时间,为这个导出数据真是煞费苦心,网上找了好多资料都没有找到. 从SQL SERVER 2008开始,我们就可以很方便的导出数据脚本,而无需再借助存储过程,但是SQL Server 2012和 ...

  4. MongoDB小结01 - MongoDB简介

    我们为什么要去学习MongoDB MongoDB是一种强大.灵活.可扩展的数据存储方式. 丰富的数据模型 MongoDB是面向文档的数据库,不是关系型数据库.它将原来'行'(row)的概念换成了更加灵 ...

  5. oracle索引简单使用

    --查询表索引 select * from user_ind_columns where table_name = upper('HY_PROJECT') and column_name = uppe ...

  6. AHCI IDE

    AHCI模式性能好 IDE模式,提高约20%,使用Windows 7 系统,AHCI 模式是最佳选择,特别是对SSD硬盘IDE是为XP的兼容性,RAID 模式是要有两块以上硬盘才能实现AHCI模式装的 ...

  7. 总结react中遇到的坑和一些小的知识点

    在使用react 中经常会遇到各种个样的问题,如果对react不熟悉则会对遇到的问题感到莫名其妙而束手无策,接下来分析一下react中容易遇到的问题和注意点. 1.setState()是异步的this ...

  8. Android 4.2 project导入 5.0 SDK Eclipse 开发环境出现的问题总结

    Android 4.2 project导入 5.0 SDK Eclipse 开发环境出现的问题总结 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循&qu ...

  9. promise && than

    Promise 构造函数接受一个函数作为参数,该函数的2个参数分别是 resolve 和 reject.他们是2个函数,有 JavaScript 引擎提供,不用自己部署.   var promise ...

  10. openstack(2) -------RabbitMQ集群部署

    一.RabbitMQ简介 RabbitMQ属于一个流行的开源消息队列系统.属于AMQP( 高级消息队列协议 ) 标准的一个 实现.是应用层协议的一个开放标准,为面向消息的中间件设计.用于在分布式系统中 ...