Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 19613   Accepted: 7725

Description

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

  1. 5
  2. 2 4 3 0
  3. 4 5 0
  4. 0
  5. 0
  6. 1 0

Sample Output

  1. 1
  2. 2

Source

思路:求一个有向图从几个点出发可以遍历整个图、以及至少加几条边使整张图强联通。

缩点以后,显然入度为0的点的个数就是第一问的答案。 
然后第二问答案显然是入度为0和出度为0的个数的最大值,即出入度为0的点间连条边就可以了。

代码:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. using namespace std;
  5. #define ms(a, b) memset(a, b, sizeof(a))
  6. , M = ;
  7. int h[N], p[M], a[M], fa[N], out[N], in[N], dfn[N], low[N];
  8. int scc, top, ts, cnt, sta[N], insta[N];
  9. void add(int x, int y) { p[++cnt] = h[x]; a[cnt] = y; h[x] = cnt; }
  10.  
  11. void tarjan(int u) {
  12. int i;
  13. dfn[u] = low[u] = ++ts;
  14. sta[++top] = u; insta[u] = ;
  15. for (i = h[u]; i; i = p[i])
  16. if (!dfn[a[i]]) {
  17. tarjan(a[i]);
  18. low[u] = min(low[u], low[a[i]]);
  19. } else if (insta[a[i]])
  20. low[u] = min(low[u], dfn[a[i]]);
  21. if (dfn[u] == low[u]) {
  22. ++scc;
  23. do {
  24. i = sta[top--];
  25. insta[i] = ;
  26. fa[i] = scc;
  27. } while (i != u);
  28. }
  29. }
  30.  
  31. int main() {
  32. int i, j, n, m, x, b;
  33.  
  34. while (scanf("%d", &n) != EOF) {
  35. ms(dfn, ); ms(); ms(h, ); ms();
  36. ts = cnt = top = scc = ;
  37. ; i <= n; ++i)
  38. while (scanf("%d", &x) && x) add(i, x);
  39. ; i <= n; ++i)
  40. if (!dfn[i]) tarjan(i);
  41. ; i <= n; ++i)
  42. for (j = h[i]; j; j = p[j])
  43. if (fa[i] != fa[a[j]])
  44. ++out[fa[i]], ++in[fa[a[j]]];
  45. ,a2=;
  46. ; i <= scc; ++i) {
  47. if (!out[i]) ++a1;
  48. if (!in[i]) ++a2;
  49. }
  50. ) puts("1\n0");
  51. else printf("%d\n%d\n", a2, max(a1, a2));
  52. }
  53. ;
  54. }

POJ 1236 tarjan的更多相关文章

  1. POJ 1236 Tarjan算法

    这道题认真想了想.. [ 题目大意:有N个学校,从每个学校都能从一个单向网络到另外一个学校,两个问题 1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件. 2:至少需要添加几条 ...

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

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

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

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

  4. Poj 1236 Network of Schools (Tarjan)

    题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...

  5. POJ 1236 Network of Schools - 缩点

    POJ 1236 :http://poj.org/problem?id=1236 参考:https://www.cnblogs.com/TnT2333333/p/6875680.html 题意: 有好 ...

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

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

  7. 有向图 加最少的边 成为强连通分量的证明 poj 1236 hdu 2767

    poj 1236: 题目大意:给出一个有向图, 任务一: 求最少的点,使得从这些点出发可以遍历整张图  任务二: 求最少加多少边 使整个图变成一个强连通分量. 首先任务一很好做, 只要缩点 之后 求 ...

  8. POJ 1236——Network of Schools——————【加边形成强连通图】

    Network of Schools Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u ...

  9. 【POJ 1236 Network of Schools】强联通分量问题 Tarjan算法,缩点

    题目链接:http://poj.org/problem?id=1236 题意:给定一个表示n所学校网络连通关系的有向图.现要通过网络分发软件,规则是:若顶点u,v存在通路,发给u,则v可以通过网络从u ...

随机推荐

  1. SMBLoris windows拒绝服务漏洞

    在美国拉斯维加斯举行的2017年度DEF CON黑客大会上,安全研究人员公布了Windows系统上的一个长达20年没有发现的漏洞,该漏洞名为"SMBLoris",黑客可以轻松的使用 ...

  2. 【前端,干货】react and redux教程学习实践(二)。

    前言 这篇博文接 [前端]react and redux教程学习实践,浅显易懂的实践学习方法. ,上一篇简略的做了一个redux的初级demo,今天深入的学习了一些新的.有用的,可以在生产项目中使用的 ...

  3. CODE大全告诉你java是否开始没落了

    CODE大全告诉你java是否开始没落了! 22 岁,对于一个技术人来说可谓正当壮年.但对于一门编程语言来说,情况可能又有不同.各类编程语言横空出世,纷战不休,然而 TIOBE 的语言排行榜上,Jav ...

  4. CentOS编译PHP过程中常见错误信息的解决方法

    原文链接:http://www.linuxidc.com/Linux/2014-05/102327.htm ********************************************** ...

  5. 身在魔都的她,该不该继续"坚持"前端开发?

    一 这个女孩儿,是我很好很好很好的一位朋友,也是中学的同学,去年从她的本科大学毕业,毕业后由于没找到合适的工作而选择去培训机构培训了比较火爆的前端开发,之后去了上海找工作,但是由于一些原因在从上一家公 ...

  6. 【有意思的BUG】反转的水印

    今天无意中看到一个图集,翻着翻着感觉到哪儿不对劲.是的,水印打反了,怎么会出现这样的局面我也不知道,可能就是手抖了吧. 通过与懂这方面知识的人请教,120%的可能是因为图片本身就自带水印,而不是因为后 ...

  7. NOIP2000提高组 单词接龙

    题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的"龙"(每个单词都最多在"龙" ...

  8. Markdown常用编辑器

    插入图片功能对比 Cmd Markdown只支持通过输入图片在线URL的方式来插入图片,不能上传本地图片. CSDN Markdown.MarkdownPad既支持插入本地图片,也支持插入网上在线图片 ...

  9. IntelliJ IDEA Windows下Spark开发环境部署

    0x01 环境说明 本地 OS: windows 10 jdk: jdk1.8.0_121 scala: scala-2.11.11 IDE: IntelliJ IDEA ULTIMATE 2017. ...

  10. windows域与工作组概念

    局域网上的资源需要管理,“域”和“工作组”就是两种不同的网络资源管理模式.那么二者有何区别呢? 工作组 Work Group 在一个网络内,可能有成百上千台电脑,如果这些电脑不进行分组,都列在“网上邻 ...