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. 获取本机IP(适用于Linux系统)

    获取本机IP(适用于Linux系统) /** * @desc 获取本机IP(适用于Linux系统) * @return Ip */ public static String getLocalIP() ...

  2. Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

    我用String代替了链表显示,本题的大意是每k个进行逆序处理,剩下的不够k个的就按照原顺序保留下来. public class ReverseNodes { public static void m ...

  3. JavaScript数组遍历(迭代)方法 8种

    最近工作中经常涉及到数据的处理,数组尤其常见,经常需要对其进行遍历.转换操作,网上的文章零零散散,不得已自己又找出红宝书来翻出来看,顺便记一笔,便于以后查询. 数组常用的方法 ECMAScript5为 ...

  4. iOS设置状态栏样式

    iOS设置状态栏样式可以使用两种方式. 方式一: 直接在需要改变默认状态栏样式的控制器中实现一个方法(其他任何事情都不用做): // 返回状态栏的样式 - (UIStatusBarStyle)pref ...

  5. 百分比相对计算注意事项CSS3

    百分比计算 1.涉及元素定位,和大小计算,基于元素自身的包含块. 2.元素背景图的大小计算,位置计算,基于元素自身的宽,高. 2.元素图片边框使用的图图片大小计算,基于元素自身的宽,高.

  6. netstat命令---输出网络相关的信息

    简介 Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Member ...

  7. HTTP和HTTPS有什么区别? 什么是SSL证书?使用ssl证书优势?

    什么是SSL? SSL是指安全套接层协议(以及传输层协议TLS),位于TCP/IP协议与各种应用层协议之间,为数据通讯提供安全支持,是目前使用最广泛的安全协议.它为互联网或内部网络连接,进行操作的两台 ...

  8. Appium实现的技巧

    截图功能: //截屏并保存至本地             File screen = driver.getScreenshotAs(OutputType.FILE);             File ...

  9. org.apache.commons.io——FileUtils学习笔记

    FileUtils类的应用 1.写入一个文件: 2.从文件中读取: 3.创建一个文件夹,包括文件夹: 4.复制文件和文件夹: 5.删除文件和文件夹: 6.从URL地址中获取文件: 7.通过文件过滤器和 ...

  10. 在Python3.5中使用 The del.icio.us API

    问题:参考<集体智慧编程>一书的第二章中访问del.icio.us网站的数据需要使用到事先编好的Python API.但是书上提供的API并不适用与Python3.5的版本. 解决方法:在 ...