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. 笔记本电脑连接上WiFi后,弹不出登录界面怎么办?

    以CMCC为例子 步骤: 1 连接成功CMCC之后,打开适配器设置.右击无线网卡,选择属性 2.双击INTERNET协议版本4 3.将DNS改成自动获取,然后确定 4.再确定 5.然后重新断开CMCC ...

  2. 聪明的质监员[NOIP2011]

    时间限制:1 s   内存限制:128 MB [问题描述] 小 T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有n个矿石,从 1 到n逐一编号,每个矿石都有自己的重量wi以及价值vi.检 ...

  3. centos7修改网卡名称

    [root@localhost ~]# cd /etc/sysconfig/network-scripts/[root@localhost network-scripts]# mv ifcfg-eno ...

  4. Android融合推送MixPush SDK集成多家推送平台,共享系统级推送,杀死APP也能收到推送

    消息推送是App运营的重要一环,为了优化消息推送成功率,降低电量和流量消耗,系统级的推送服务显得尤为重要.小米和魅族由此推出了自家的推送平台,在MIUI和Flyme上共享系统级推送服务,让APP在被杀 ...

  5. openstack中dashboard页面RuntimeError: Unable to create a new session key. It is likely that the cache is unavailable.

    环境是centos7,直接跑在服务器上. 按照官网一步步安装openstack,到验证dashborad时出错. 登录http://192.168.1.73/dashboard ,输入域名,用户名,密 ...

  6. 部署Node.js项目(CentOS)

    Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境,用来方便地搭建快速的易于扩展的网络应用.Node.js 使用了一个事件驱动.非阻塞式 I/O 的模型,使其轻量又 ...

  7. 移动端为何不使用click而模拟tap事件及解决方案

    移动端click会遇到2个问题,click会有200-300ms的延迟,同时click事件的延迟响应,会出现穿透,即点击会触发非当前层的点击事件. 为什么会存在延迟? Google开发者文档中有提到: ...

  8. Hadoop分布式集群搭建hadoop2.6+Ubuntu16.04

    前段时间搭建Hadoop分布式集群,踩了不少坑,网上很多资料都写得不够详细,对于新手来说搭建起来会遇到很多问题.以下是自己根据搭建Hadoop分布式集群的经验希望给新手一些帮助.当然,建议先把HDFS ...

  9. 腾讯SNG电面

    第一次电面. 前半段基本闲聊,问题也记得不太清楚了. 自我介绍. 为什么想去上海工作?除了职业方面有其他原因吗?我猜出来面试官想问私人问题了,你真的可以直接问的,拐弯抹角了好久...有什么爱好.特长. ...

  10. 如何正确地修改.data和.item文件的‘utf-8’格式

    问题:有时候,我们在做分类信息提取的时候文件的格式可能不是我们想要的.txt文件的格式,如何进行修改? 解决:(1)将文件复制一份,并保存为.txt的格式(2)将复制之后的文件通过另存为的方式更改为u ...