题目链接:

  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. bzoj 1430 小猴打架 prufer 性质

    小猴打架 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 709  Solved: 512[Submit][Status][Discuss] Descri ...

  2. python之模块随笔记-os

    操作系统模块:import os os.remove() 删除文件 os.unlink() 删除链接文件 os.rename() 重命名文件 os.listdir() 列出指定目录下所有文件 os.c ...

  3. Webstrom打开太慢

    让webstrom将安装包目录屏蔽,settings-搜索file types-在ignore file and folders加入node_modules目录,操作方式如下:

  4. jackson的应用

    直接上代码 package com.demo.jackson.utils; import com.fasterxml.jackson.core.JsonProcessingException; imp ...

  5. SAS编程基础 - 数据获取与数据集操作(1)

    1. 数据来源 SAS数据来源主要有两种:一是通过input语句创建,另外一种方式是通过外部数据文件获取. 1.1 libname 1.2 odbc 1.3 passthrough 1.4 impor ...

  6. ppc_85xx-gcc -shared -fPIC liberr.c -o liberr.so

    fPIC作用于编译阶段,告诉编译器产生与位置无关代码(Position-Independent Code),   则产生的代码中,没有绝对地址,所有使用相对地址.故而代码能够被载入器载入到内存的随意 ...

  7. struts1与struts2的差别

     Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架.其全新的Struts 2的体系结构与Struts 1的体系结 ...

  8. bootstrap 时间控件

    近期使用了bootstrap的UI感觉确实非常美丽,非常值得学习和使用. 以下先简单了解下bootstrap的时间控件. 这个时间控件使用起来还是很的简单.仅仅须要引入主要的css和js就能够了 须要 ...

  9. C#字符串数组排序 C#排序算法大全 C#字符串比较方法 一个.NET通用JSON解析/构建类的实现(c#) C#处理Json文件 asp.net使用Jquery+iframe传值问题

    C#字符串数组排序   //排序只带字符的数组,不带数字的 private   string[]   aa   ={ "a ", "c ", "b & ...

  10. iOS DeepLinkKit使用简单介绍

    Update: 2017.04.08 添加了使用iOS DeepLinkKit使用Universal Links的部分 ---------------------------------------- ...