http://poj.org/problem?id=1236

Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9481   Accepted: 3767

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

 
【题解】:
  这题大意是给一个有向图,求至少给多少个结点发消息能使消息传遍整个网络,并进一步求出至少添加多少条边能使对图中任意一个结点发消息都能使消息传遍整个网络。可以先用kosaraju将强连通分支缩点,得到原图的基图,然后统计入度为0的连通分量个数和出度为0的连通分量个数,入度为0的必须给它发消息,入度不为0的不必给发消息,所以第一问所求即为缩点后的图中入度为0的个数,至于第二问,只需将入度为0的结点与出度为0的结点连接即可满足要求,最少需加边数目为两者之中的较大者,需注意的是,单只有一个连通分量时,输出结果为0 。
 
【code】:
 
  1. /**
  2. Judge Status:Accepted Memory:772K
  3. Time:0MS Language:G++
  4. Code Length:2155B Author:cj
  5. */
  6.  
  7. #include<iostream>
  8. #include<stdio.h>
  9. #include<stack>
  10. #include<string.h>
  11. #include<algorithm>
  12. #include<vector>
  13.  
  14. #define N 110
  15. using namespace std;
  16.  
  17. vector<int> G[N];
  18. int pre[N],lowlink[N],sccno[N],dfs_cnt,scc_cnt;
  19. stack<int> stk;
  20.  
  21. int visit[N][N],in[N],out[N];
  22. void Tarjan(int u)
  23. {
  24. pre[u] = lowlink[u] = ++dfs_cnt;
  25. stk.push(u);
  26. int i;
  27. for(i=;i<G[u].size();i++)
  28. {
  29. int v = G[u][i];
  30. if(!pre[v])
  31. {
  32. Tarjan(v);
  33. lowlink[u] = min(lowlink[u],lowlink[v]);
  34. }
  35. else if(!sccno[v])
  36. {
  37. lowlink[u] = min(lowlink[u],pre[v]);
  38. }
  39. }
  40. if(pre[u]==lowlink[u])
  41. {
  42. int x;
  43. scc_cnt++;
  44. do
  45. {
  46. x = stk.top();
  47. stk.pop();
  48. sccno[x] = scc_cnt;
  49. }while(x!=u);
  50. }
  51. }
  52.  
  53. void findncc(int n)
  54. {
  55. dfs_cnt = scc_cnt = ;
  56. memset(pre,,sizeof(pre));
  57. memset(lowlink,,sizeof(lowlink));
  58. memset(sccno,,sizeof(sccno));
  59. int i;
  60. for(i=;i<=n;i++) if(!pre[i]) Tarjan(i);
  61. }
  62.  
  63. void getNewMap(int n)
  64. {
  65. int i,j;
  66. memset(visit,,sizeof(visit));
  67. memset(in,,sizeof(in));
  68. memset(out,,sizeof(out));
  69. for(i=;i<=n;i++)
  70. {
  71. for(j=;j<G[i].size();j++)
  72. {
  73. int v = sccno[G[i][j]];
  74. int u = sccno[i]; //注意是对sccno[i]数组里的强联通分量进行操作,也就是缩点的过程
  75. if(u!=v)
  76. {
  77. if(!visit[u][v])
  78. {
  79. visit[u][v] = ;
  80. in[v]++; //出度入度统计
  81. out[u]++;
  82. }
  83. }
  84. }
  85. }
  86. }
  87. int main()
  88. {
  89. int n;
  90. scanf("%d",&n);
  91. int i;
  92. for(i=;i<=n;i++)
  93. {
  94. int a;
  95. G[i].clear();
  96. while(~scanf("%d",&a)&&a)
  97. {
  98. G[i].push_back(a);
  99. }
  100. }
  101. findncc(n);
  102. getNewMap(n);
  103. int cnt_in = ,cnt_out = ;
  104. for(i=;i<=scc_cnt;i++)
  105. {
  106. if(!in[i]) cnt_in++;
  107. if(!out[i]) cnt_out++;
  108. }
  109. printf("%d\n",cnt_in);
  110. if(scc_cnt!=) printf("%d\n",max(cnt_in,cnt_out)); //联通分量只有一个输出0
  111. else printf("0\n");
  112. return ;
  113. }

poj 1236 Network of Schools(又是强连通分量+缩点)的更多相关文章

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

    题目大概: 每个学校都可以把软件复制好,交给它名单上的学校. 问题A:把软件复制成几份,然后交给不同的学校,所有学校才能够都有软件. 问题B:添加几条边,能使得这个图变成强连通图. 思路: 找出所有的 ...

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

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

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

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

  4. POJ 1236 Network of Schools(强连通分量/Tarjan缩点)

    传送门 Description A number of schools are connected to a computer network. Agreements have been develo ...

  5. POJ 1236 Network of Schools 有向图强连通分量

    参考这篇博客: http://blog.csdn.net/ascii991/article/details/7466278 #include <stdio.h> #include < ...

  6. poj 1236 Network of Schools(强连通、缩点、出入度)

    题意:给出一个有向图.1:问至少选出多少个点,才能沿有向边遍历所有节点.2:问至少加多少条有向边,使原图强连通. 分析:第一个问题,缩点后找所有树根(入度为0).第二个问题,分别找出入度为0和出度为0 ...

  7. poj 1236 Network of Schools【强连通求孤立强连通分支个数&&最少加多少条边使其成为强连通图】

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13800   Accepted: 55 ...

  8. POJ 1236.Network of Schools (强连通)

    首先要强连通缩点,统计新的图的各点的出度和入度. 第一问直接输出入度为0的点的个数 第二问是要是新的图变成一个强连通图,那么每一个点至少要有一条出边和一条入边,输出出度和入度为0的点数大的那一个 注意 ...

  9. POJ 1236 Network of Schools (tarjan算法+缩点)

    思路:使用tarjan求强连通分量并进行缩点,判断所有入度为0的点,这个点就是必须要给予文件的点,分别计算出度,入度为零的点的个数,取二者的最大值就是把这个图变成强连通需要加的边数. 一个取值需要讨论 ...

随机推荐

  1. 使用C#动态生成Word文档/Excel文档的程序测试通过后,部署到IIS服务器上,不能正常使用的问题解决方案

    使用C#动态生成Word文档/Excel文档的程序功能调试.测试通过后,部署到服务器上,不能正常使用的问题解决方案: 原因: 可能asp.net程序或iis访问excel组件时权限不够(Ps:Syst ...

  2. 在web界面调用水晶报表导出文件时莫名错误

    原因是水晶报表未破解版有字段限制,不能超过90(具体个数没仔细测)个字段. 建议那些select *的朋友检查一下字段个数

  3. <转载>提升程序的特权(AdjustTokenPrivileges)

    首先列出需要的函数 1.OpenProcessToken 2.AdjustTokenPrivileges 3. LookupPrivilegeValue ----------------------- ...

  4. 第四十四篇、iOS开发中git添加.gitignore文件

    .gitignore文件可以直接使用https://github.com/github/gitignore 1.在项目中设置忽略文件(1)将从github上荡下来的对应的.gitignore文件(Sw ...

  5. C# @符号的多种使用方法

    1.限定字符串用 @ 符号加在字符串前面表示其中的转义字符“不”被处理.如果我们写一个文件的路径,例如"D:/文本文件"路径下的text.txt文件,不加@符号的话写法如下:str ...

  6. java内部类的定义原则

    /*内部类的访问规则:1,内部类可以直接访问外部类中的成员,包括私有.    之所以可以直接访问外部类中的成员,是因为内部类中持有了一个外部类的引用,格式 外部类名.this2,外部类要访问内部类,必 ...

  7. opencv 手写选择题阅卷 (四)Android端 手机应用开发

    opencv 手写选择题阅卷 (四)Android 手机应用开发 在PC端把代码调通以后开始开发Android 手机应用,因为主要功能代码为C++代码,所以需要通过NDK编译,JAVA通过JNI方式调 ...

  8. 【转帖】error C2296: “^”: 非法,左操作数包含“double”类型

    想要实现 ,写的C++程序为 double x; x=2^3; 结果程序总是出现这样的错误:error C2296: “^”: 非法,左操作数包含“double”类型 后来才发现操作符“^”,在C++ ...

  9. Kubernetes Architecture

    reference:https://www.symantec.com/connect/blogs/google-kubernetes-analytical-evaluation

  10. 5步解决移动设备上的300ms点击延迟

    译者:jmouse 大多数基于触摸的浏览器设备,在点击时都会有个 300ms 的事件触发等待时间,做过 web app 开发的同学应该都遇到过这个情况,通过下面的5步可以轻松搞定这个延迟. 1.不要太 ...