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

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

Sample Output

1
2

Source

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

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

代码:

 #include <cstdio>
 #include <cstring>
 #include <algorithm>
 using namespace std;
 #define ms(a, b) memset(a, b, sizeof(a))
 , M = ;
 int h[N], p[M], a[M], fa[N], out[N], in[N], dfn[N], low[N];
 int scc, top, ts, cnt, sta[N], insta[N];
 void add(int x, int y) { p[++cnt] = h[x]; a[cnt] = y; h[x] = cnt; }

 void tarjan(int u) {
     int i;
     dfn[u] = low[u] = ++ts;
     sta[++top] = u; insta[u] = ;
     for (i = h[u]; i; i = p[i])
         if (!dfn[a[i]]) {
             tarjan(a[i]);
             low[u] = min(low[u], low[a[i]]);
         } else if (insta[a[i]])
             low[u] = min(low[u], dfn[a[i]]);
     if (dfn[u] == low[u]) {
         ++scc;
         do {
             i = sta[top--];
             insta[i] = ;
             fa[i] = scc;
         } while (i != u);
     }
 }

 int main() {
     int i, j, n, m, x, b;

     while (scanf("%d", &n) != EOF) {
         ms(dfn, ); ms(); ms(h, ); ms();
         ts = cnt = top = scc = ;
         ; i <= n; ++i)
             while (scanf("%d", &x) && x) add(i, x);
         ; i <= n; ++i)
             if (!dfn[i]) tarjan(i);
         ; i <= n; ++i)
             for (j = h[i]; j; j = p[j])
                 if (fa[i] != fa[a[j]])
                     ++out[fa[i]], ++in[fa[a[j]]];
         ,a2=;
         ; i <= scc; ++i) {
             if (!out[i]) ++a1;
             if (!in[i])  ++a2;
         }
         ) puts("1\n0");
         else printf("%d\n%d\n", a2, max(a1, a2));
     }
     ;
 }

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. overlay 是如何隔离的?- 每天5分钟玩转 Docker 容器技术(53)

    不同的 overlay 网络是相互隔离的.我们创建第二个 overlay 网络 ov_net2 并运行容器 bbox3. bbox3 分配到的 IP 是 10.0.1.2,尝试 ping bbox1( ...

  2. 转换Json中的时间戳为标准时间格式

    //出自http://www.cnblogs.com/ahjesus function ConvertJSONDateToJSDate(jsonDate) {        ///    <su ...

  3. MySQL登录汇总

    --MySQL登录汇总 --------------------2014/5/17 1. ERROR 1045错误ERROR 1045 (28000): Access denied for user ...

  4. 使用bootstrap网格系统自适应盒子宽度时保持所有盒子高度一致。

    使用了bootstrap网格系统的好处是很容易便实现了响应式布局,盒子可以根据设置的样式,随着屏幕的大小改变而自动改变宽度,但是也存在一个问题,那就是盒子的高度是由盒子的内容决定的,如果盒子里的内容不 ...

  5. iOS之UIWebView无法获取web标题

    最近遇到了一个问题,就是在UIWebView的代理方法里,执行document.title的js代码无法获取网页标题,代码如下: - (void)webViewDidFinishLoad:(UIWeb ...

  6. 【.Net Core 2.0 生态】-- 好文收藏

    随笔分类 - .NET Core - dotNet实训营 .Net Core 2.0 生态(1).NET Standard 2.0 特性介绍和使用指南 .Net Core 2.0 生态(2).NET ...

  7. LinkedList之modCount和expectedModCount

    modCount和expectedModCount是用于表示修改次数的,其中modCount表示集合的修改次数,这其中包括了调用集合本身的add方法等修改方法时进行的修改和调用集合迭代器的修改方法进行 ...

  8. sublime代码格式化插件HTML/CSS/JS prettify

    1. 进入下载链接,安装node.js (根据自己使用版本) https://nodejs.org/en/download/ 注意: 记住node.js的 安装路径 2.sublime HTML/CS ...

  9. C#多线程爬虫抓取免费代理IP

    这里用到一个HTML解析辅助类:HtmlAgilityPack,如果没有网上找一个增加到库里,这个插件有很多版本,如果你开发环境是使用VS2005就2.0的类库,VS2010就使用4.0,以此类推.. ...

  10. IDE UltraEdit 图文激活+安装教程

    IDE UltraEdit 安装+激活图文.. ---------------- ---------------- ---------------- ---------------- -------- ...