A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes software (the “receiving schools”). Note that if B is in the distribution list of school A, then A does not necessarily appear in the list of school B
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

The first line contains an integer N: the number of schools in the network (2 <= N <= 100). The schools are identified by the first N positive integers. Each of the next N lines describes a list of receivers. The line i+1 contains the identifiers of the receivers of school i. Each list ends with a 0. An empty list contains a 0 alone in the line.

Output

Your program should write two lines to the standard output. The first line should contain one positive integer: the solution of subtask A. The second line should contain the solution of subtask B.

Sample Input

5
2 4 3 0
4 5 0
0
0
1 0

Sample Output

1
2

题意:

N个学校之间有单向的网络,每个学校可以通过单向网络向周边的学校传输信息。

问题1:初始至少需要向多少个学校发放信息,使得网络内所有的学校最终都能收到信息。

问题2:至少需要添加几条边,使得任意向一个学校发送信息后,经过若干次传送,所有的学校都能收到信息。

题解:

分析题意发现其实就是问对于一个有向无环图:
1.至少要选几个点,才能从这些点出发到达所有点 。

2.至少加入几条边,就能从图中任何一个点出发到达所有点。

当然图不一定是无环图,那么就用到了Tarjan算法来找到强连通分量,然后再缩点就变成了有向无环图。

然后不难想出对于第一个问题答案就是有向无环图中入度为0的点的个数。对于第二个问题仔细想一下就发现了如果要达到题意肯定每个点都有出有入,所以要消灭所有入度为零和出度为零的点,所以如果入度为0的个数是n,出度为0的个数是m,至少添加边的条数就是max(n,m)。

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>using namespace std;#define MAXN 205bool is_instack[MAXN];//记录节点是否在栈中
int stack[MAXN],top;
bool map[MAXN][MAXN];//存图,这里用的矩阵仅供理解,具体做题自己调整。
int DFN[MAXN];//记录节点第一次被访问时的时间
int LOW[MAXN];//记录节点与节点的子树节点中最早的步数
int time;
int Belong[MAXN];//记录每个节点属于的强连通分量编号
int N,cnt;//N是点数,cnt是强连通分量编号。void Tarjan(int x){DFN[x] = LOW[x] = ++time;is_instack[x] = true;stack[++top] = x;for(int i=1 ; i<=N ; ++i){if(map[x][i] == false)continue;if(!DFN[i]){Tarjan(i);if(LOW[i]<LOW[x]){LOW[x] = LOW[i];}}else if(is_instack[i] && DFN[i]<DFN[x]){ //这里注意不能直接else,因为DFN[i]!=0还有可能是横叉边。LOW[x] = min(LOW[x],DFN[i]);}}if(DFN[x] == LOW[x]){++cnt;int mid;do{mid = stack[top--];is_instack[mid] = false;Belong[mid] = cnt;}while(mid != x);}
}int in[MAXN];//记录缩点后点的入度
int out[MAXN];//记录缩点后点的出度 void init(){memset(map,false,sizeof(map));memset(DFN,0,sizeof(DFN));memset(LOW,0,sizeof(LOW));memset(in,0,sizeof(in));memset(out,0,sizeof(out));memset(is_instack,false,sizeof(is_instack));time = 0;cnt = 0;top = 0;
}int main(){while(scanf("%d",&N)!=EOF){init();for(int i=1 ; i<=N ; ++i){int t;while(scanf("%d",&t) && t)map[i][t] = true;}for(int i=1 ; i<=N ; ++i){if(!DFN[i])Tarjan(i);//有可能不是连通图所以遍历一遍。 }for(int i=1 ; i<=N ; ++i){for(int j=1 ; j<=N ; ++j){if(map[i][j] && Belong[i] != Belong[j]){++out[Belong[i]];++in[Belong[j]];}}}if(cnt==1){printf("1\n0\n");continue;}int inzero = 0,outzero = 0;for(int i=1 ; i<=cnt ; ++i){if(!in[i])++inzero;if(!out[i])++outzero;}printf("%d\n%d\n",inzero,max(inzero,outzero));}return 0;
}

转载于:https://www.cnblogs.com/vocaloid01/p/9514080.html

POJ - 1236 Network of Schools相关推荐

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

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

  2. [tarjan] poj 1236 Network of Schools

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

  3. POJ 1236 Network of Schools(tarjan)

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

  4. POJ 1236 Network of Schools(强连通分量缩点求根节点和叶子节点的个数)

    Description: A number of schools are connected to a computer network. Agreements have been developed ...

  5. poj 1236 Network of Schools (强连通分支缩点)

    Description A number of schools are connected to a computer network. Agreements have been developed ...

  6. poj 1236 Network of Schools

    题目描述:有一些学校连接到一个计算机网络.这些学校之间达成了一个协议:每个学校维护着一个学校列表,它向学校列表中的学校发布软件.注意,如果学校B在学校A的列表中,则A不一定在B的列表中.任务A:计算为 ...

  7. POJ - 1236 Network of Schools(强连通缩点)

    题目链接:点击查看 题目大意:一个学校连接在一个计算机网络上,学校之间存在软件支援协议,每个学校都有它应支援的学校名单(学校A支援学校B,并不表示学校B一定支援学校A).当某校获得一个新软件时,无论是 ...

  8. poj 1236 Network of Schools

    强联通分量模版题,tarjan算法: 1 #include<stdio.h> 2 #include<string.h> 3 #define MAXN 110 4 5 int n ...

  9. pku 1236 Network of Schools (tarjan缩点)

    http://poj.org/problem?id=1236 N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,问题1:初始至少需要向多 ...

最新文章

  1. 《Redis设计与实现》之第十一章:AOF持久化
  2. 50个PHP程序性能优化的方法
  3. .net 把一个对象赋值给一个参数_Java:new一个对象的过程中发生了什么?
  4. 值得收藏的JSP连接mysql数据库的例子
  5. 介绍一个代码管理系统-Git
  6. StackExchange.Redis 官方文档(五) Keys, Values and Channels
  7. Python安装指南
  8. k8s serviceAccountName填写后应用没有进行挂载问题处理
  9. 利用python批量创建.xsh文件实现Xshell批量登录服务器
  10. php源码字符串内部表示,PHP源码—implode函数源码分析
  11. 教你文本聚类(参考http://www.kuqin.com/searchengine/20080511/8323.html)
  12. Layui 重载后表格内容重复 更换提交方式已解决
  13. su -user -c
  14. 笔记本加固态小白怎么设置
  15. (01)ORB-SLAM2源码无死角解析-(58) 闭环线程→计算Sim3: 源码Sim3Solver::iterate()讲解
  16. ACM-图论完全总结(知识点+模板)
  17. macbook安装typescript环境added 1 package, and audited 2 packages in 26sfound 0 vulnerabilities代表成功了
  18. Day69(贪心算法)
  19. QCC304x/QCC514x Pydbg在线调试
  20. 测试开发【Mock平台】06开发:项目管理功能(二)Atnd页面搭建经验实战与学习线路梳理

热门文章

  1. Apache上部署Django
  2. mysql 1067错误,服务无法启动 进程意外终止
  3. HNCU1101:马的移动---BFS
  4. 操作系统经典书籍--现代操作系统
  5. 工作场合少聊事事非非
  6. springmvc五:使用pojo作为参数
  7. 汇编:转移目的地址在内存中
  8. 深度学习之线性回归模型
  9. python 获取中文文件名的输出
  10. 五种js判断是否为整数类型方式