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

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

题目链接:POJ 1236

由于数据结构老师说期末成绩跟刷题数量有关(有毒),然后就去随便翻了翻校内OJ里会做的题,然后突然发现有一道题跟这题非常类似,细细查看发现题面就TM是中文翻译版本而已,而且翻译的也不好,机翻水平…………。然后时隔几个礼拜就又来做了一次这道题,这次就感觉理解更加深入了。

题目中你其实是一个超级源点,学校编号是1~n,各自均有单向边连向其他点,第一个问题是问你需要发多少个软件包来使得所有学校都可以直接、间接地获得你发的软件包;第二个问题是假如你只发一个软件包,那需要加多少条边来使得所有学校都被传达到。

对于问题一,可以发现只要一个点有入边(入度不为0),则说明他肯定可以被其他学校传递到,显然你找到入度为0的点(即没其他学校给他发软件包的点)所有点就是你要发的学校集合了,如果倒着想,不给这些点发的话肯定是无法到达的,至少这个点肯定是收不到软件包的。

对于问题二,其实问题可以转化成一个有向图(极端情况下为多个强连通分量)加多少条边变成一个强连通分量,先Tarjan缩点可以发现把边加在叶子那里效果是最好的,假如每一个人都可以传递一个能量给你,那么就需要这么一条边把尽量多的能量传出去且这样边数越少越好,显然叶子聚集的能量是最多的,最好就是从叶子连出来边加回根处,形成环,不过当强连通分量只有一个的时候就不用加了,要输出0。

代码:

  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cstdlib>
  5. #include <sstream>
  6. #include <cstring>
  7. #include <bitset>
  8. #include <string>
  9. #include <deque>
  10. #include <stack>
  11. #include <cmath>
  12. #include <queue>
  13. #include <set>
  14. #include <map>
  15. using namespace std;
  16. #define INF 0x3f3f3f3f
  17. #define CLR(arr,val) memset(arr,val,sizeof(arr))
  18. #define LC(x) (x<<1)
  19. #define RC(x) ((x<<1)+1)
  20. #define MID(x,y) ((x+y)>>1)
  21. typedef pair<int,int> pii;
  22. typedef long long LL;
  23. const double PI=acos(-1.0);
  24. const int N=110;
  25. struct edge
  26. {
  27. int to,nxt;
  28. };
  29. edge E[N*N*N];
  30. int head[N],tot;
  31. int dfn[N],low[N],belong[N],scc,ts,top,st[N];
  32. int in[N],out[N];
  33. bitset<N> ins;
  34.  
  35. void init()
  36. {
  37. CLR(head,-1);
  38. tot=0;
  39. CLR(dfn,0);
  40. CLR(low,0);
  41. CLR(belong,0);
  42. scc=ts=top=0;
  43. CLR(in,0);
  44. CLR(out,0);
  45. ins.reset();
  46. }
  47. inline void add(int s,int t)
  48. {
  49. E[tot].to=t;
  50. E[tot].nxt=head[s];
  51. head[s]=tot++;
  52. }
  53. void Tarjan(int u)
  54. {
  55. dfn[u]=low[u]=++ts;
  56. st[top++]=u;
  57. ins[u]=1;
  58. int i,v;
  59. for (i=head[u]; ~i; i=E[i].nxt)
  60. {
  61. v=E[i].to;
  62. if(!dfn[v])
  63. {
  64. Tarjan(v);
  65. low[u]=min(low[u],low[v]);
  66. }
  67. else if(ins[v])
  68. low[u]=min(low[u],dfn[v]);
  69. }
  70. if(low[u]==dfn[u])
  71. {
  72. ++scc;
  73. do
  74. {
  75. v=st[--top];
  76. ins[v]=0;
  77. belong[v]=scc;
  78. }while (u!=v);
  79. }
  80. }
  81. int main(void)
  82. {
  83. int n,a,b,i,j;
  84. while (~scanf("%d",&n))
  85. {
  86. init();
  87. for (i=1; i<=n; ++i)
  88. {
  89. while (scanf("%d",&b)&&b)
  90. add(i,b);
  91. }
  92. for (i=1; i<=n; ++i)
  93. if(!dfn[i])
  94. Tarjan(i);
  95. for (a=1; a<=n; ++a)
  96. {
  97. for (j=head[a]; ~j; j=E[j].nxt)
  98. {
  99. int b=E[j].to;
  100. if(belong[a]!=belong[b])
  101. {
  102. ++out[belong[a]];
  103. ++in[belong[b]];
  104. }
  105. }
  106. }
  107. int leaf=0,root=0;
  108. for (i=1; i<=scc; ++i)
  109. {
  110. if(!in[i])
  111. ++root;
  112. if(!out[i])
  113. ++leaf;
  114. }
  115. printf("%d\n%d\n",root,scc==1?0:max(root,leaf));
  116. }
  117. return 0;
  118. }

POJ 1236 Network of Schools(Tarjan缩点)的更多相关文章

  1. POJ 1236 Network of Schools Tarjan缩点

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

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

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

  3. Poj 1236 Network of Schools (Tarjan)

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

  4. POJ 1236 Network of Schools 连通图缩点

    题目大意:有向图连通图,第一问求至少需要多少个软件才能传输到所有学校,第二问求至少需要增加多少条路使其成为强连通图 题目思路:利用Tarjan算法经行缩点,第一问就是求缩点后入度为0的点的个数(特殊情 ...

  5. POJ 1236 Network of Schools —— (缩点的应用)

    题目大意:有N个学校和一些有向边将它们连结,求: 1.最少需要向几个学校发放软件,使得他们中的每一个学校最终都能够获得软件. 2.最少需要增加几条有向边使得可以从任意一个学校发放软件,使得每一个学校最 ...

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

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

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

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

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

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

  9. [tarjan] poj 1236 Network of Schools

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

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

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

随机推荐

  1. mongDB-- 3. 查询操作

    1. 准备工作 (1)启动mongo 进入mongo安装目录的bin/ 目录 , ./mongod (2)启动mongo客户端 ./mongo (3) 查看所有库 show dbs; (4) 切换到l ...

  2. __new__方法

    __new__:创建对象时调用,返回当前对象的一个实例__init__:创建完对象后调用,对当前对象的实例的一些初始化,无返回值 案例一: >>> class A(object): ...

  3. Linux IO模式及 select、poll、epoll详解

    linux的IO调度文章==> https://segmentfault.com/a/1190000003063859?hmsr=toutiao.io&utm_medium=toutia ...

  4. iOS-OC-基本控件之UIPageControl

    UIPageControl(页面控制器,就是桌面的那些小点点,每个点代表一个界面) 父类是 UIControl. iOS开发中常用的基本控件,主要和UIScrollView一起使用,比较常用的就是有些 ...

  5. RAM和ROM总结

    RAM和ROM总结 一.在解释之前先备注一些缩写的全称便于记忆: 1.EPROM:(Electrically Programmable Read-Only-Memory)电可编程序只读存储器 2.EE ...

  6. 1.oracle 12c基础

    1.查看数据库的创建模式 SQL> select name,cdb,con_id from v$database; NAME      CDB     CON_ID ---------    - ...

  7. 主动模式FTP与被动模式FTP该如何选择

    主动模式FTP与被动模式FTP该如何选择  无论是主动模式还是被动模式,其要进行文件传输都必须依次建立两个连接,分别为命令连接与数据连结.而主动模式与被动模式的差异主要体现在数据连结通道上.为了说明两 ...

  8. cmd中编译java

    cmd定位到.java文件所在位置: 注意.java文件名应与类名相同. javac xxx.java:编译(生成.class文件): java xxx:运行(执行.class文件): 若类间相互调用 ...

  9. python中列表,元组,字符串互相转换

    列表,元组和字符串python中有三个内建函数:,他们之间的互相转换使用三个函数,str(),tuple()和list(),具体示例如下所示 >>> s = "xxxxx& ...

  10. VR寒冬AR暖春,以色列AR公司再获3000万美元融资

    据统计,2015年国内至少有近70家VR公司获得天使或者A轮投资,不过狂欢并没有持续太久.2016年即将结束,资本寒冬和VR头盔的出货远不如预期,让投资者放慢了步伐,不过AR领域的热度依然不减.近日, ...