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. 常用类:String,StringBuffer,StringBuilder

    String String类被final修饰符修饰,所以不能将其进行继承,所有对它的改变都要重新创建一个新的地址 1.String的构造器 String() String(byte []bytes)/ ...

  2. 纯CSS实现垂直居中的7种方法

    今天申请博客通过了,给大家讲讲我所看到过的纯css实现垂直居中的各种方法.为什么要把它作为第一篇文章呢?因为这是我刚开始接触前端学到的对我最有用的知识,希望大家也可以从中获益! 在CSS中实现水平居中 ...

  3. 乐卡上海网点地图制作心得 | 百度地图API使用心得

    前言 事情的起因是我的爱人喜欢收集一些美丽的乐卡(明信片的一种,正面是美丽壮阔的风景照).作为一个坚实的后盾自然要支持她!于是我经常借着午休穿梭在大街小巷,凭借乐卡官方提供的乐卡网点地址进行寻找并取卡 ...

  4. pwntools使用简介2

    大致框架 官网的一个简单样例 from pwn import * context(arch = 'i386', os = 'linux') r = remote() # EXPLOIT CODE GO ...

  5. windows 注入 之 CreateRemoteThread

    钩子(Hook),是Windows消息处理机制的一个平台,应用程序可以在上面设置子程以监视指定窗口的某种消息,而且所监视的窗口可以是其他进程所创建的.当消息到达后,在目标窗口处理函数之前处理它.钩子机 ...

  6. JFreeChart与AJAX+JSON+ECharts两种处理方式生成热词统计可视化图表

    本篇的思想:对HDFS获取的数据进行两种不同的可视化图表处理方式.第一种JFreeChar可视化处理生成图片文件查看.第二种AJAX+JSON+ECharts实现可视化图表,并呈现于浏览器上.   对 ...

  7. 【LeetCode】数组-5(566)-按照要求输出矩阵

    题目要求: 思路一:借助队列,先顺序读入input矩阵,然后按照output要求向output矩阵输入 [正确代码] class Solution { public int[][] matrixRes ...

  8. css基础知识之属性选择器

    css属性选择器及属性和值选择器如下: <!DOCTYPE html> <html lang="en"> <head> <meta cha ...

  9. .NET 体系结构(.net core、.net framework、xamarin之间的关系)

    这是一张来自官方的图. 图中.NET FRAMEWORK..NET CORE和XAMARIN是三个不同的运行时,这三个运行时在.NET STANDARD LIBRARY之上,它们都包含一些基础的组件, ...

  10. Oozie调用Spark实例

    oozie调用spark有三样是必须有的: workflow.xml(不可改名) , job.properties(可改名) , jar包(自备). 1.workflow.xml workflow.x ...