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

题意:有n所学校,它们的网络可能不是完全连通的,现在要传送软件,有可能一份软件不能让每个学校都能接收到问题A:求出需要几份软件才能让每个学校都能接受到,即求出入度为0的点的个数(根节点个数)问题B:求出需要再增加几次网络连接才能只需要一份软件不管从哪发,每个学校都能收到,这里需要求出度为0的个数(叶子节点个数)

总之这道题的A就是为了求出根节点的个数,而问题B就是为了求出根节点个数和叶子节点个数更大的那个

(假设根节点有n个,叶子节点有m个)加边的时候可以考虑:t = min(n, m), 先将t个根节点和t个叶子节点连接,然后再将多出的部分连接到其他节点上
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#define N 110
using namespace std;
int dfn[N], low[N], Stack[N], vis[N];
int be[N], In[N], Out[N], Map[N][N]; //be数组存放该点属于哪一个强连通分量
int Time, top, ans, in, out, n;      //In数组存放入度的值,用于判断该点入度是否为0,即该点是否为根节点(没有点可以到达它)
vector<vector<int> >G;               //Out数组存放出度的值,用于判断该点出度是否为0,即该点是否为叶子节点(它不能到达任何点)
void Init()                          //in表示入度为0,即根节点的个数
{                                    //out表示出度为0,即叶子节点的个数
    G.clear();G.resize(n+1);memset(dfn, 0, sizeof(dfn));memset(low, 0, sizeof(low));memset(vis, 0, sizeof(vis));memset(In, 0, sizeof(In));memset(Out, 0, sizeof(Out));memset(be, 0, sizeof(be));memset(Map, 0, sizeof(Map));Time = top = in = out = ans = 0;
}
void Tarjan(int u)
{int i, len, v;dfn[u] = low[u] = ++Time;Stack[top++] = u;vis[u] = 1;len = G[u].size();for (i = 0; i < len; i++){v = G[u][i];if (!dfn[v]){Tarjan(v);low[u] = min(low[u], low[v]);}else if (vis[v]) low[u] = min(low[u], dfn[v]);}if (dfn[u] == low[u]){do{v = Stack[--top];be[v] = ans; //记录该点属于哪一个强连通分量,便于重新构图vis[v] = 0;}while(u != v);ans++; //连通分量的个数
    }
}
int main ()
{int i, b, len, v, j;while (scanf("%d", &n) != EOF){Init();for (i = 1; i <= n; i++){while (scanf("%d", &b), b)G[i].push_back(b);}for (i = 1; i <= n; i++)if (!dfn[i]) Tarjan(i); for (i = 1; i <= n; i++){len = G[i].size();for (j = 0; j < len; j++){v = G[i][j];Map[be[i]][be[v]] = 1; //重新构图,将每个强连通分量变为新的节点,表示be[i]可以到达be[v]
            }}for (i = 0; i < ans; i++){for (j = 0; j < ans; j++){if (i == j) continue; //自己肯定能到达自己,跳过else if (Map[i][j]) //当i可以到达j时,表示j肯定不是根节点,i肯定不是叶子节点,为了标记让其值++
                {In[j]++; Out[i]++;}}}for (i = 0; i < ans; i++){if (!In[i]) in++; //只有当入度为0是才是根节点,记录根节点个数if (!Out[i]) out++; //只有当出度为0是才是叶子节点,记录叶子节点个数
        }printf("%d\n", in);if (ans == 1) printf("0\n"); //如果本来就只有1个强连通分量,则不需要再添加任何边else printf("%d\n", max(in, out));}return 0;
}

转载于:https://www.cnblogs.com/syhandll/p/4708587.html

POJ 1236 Network of Schools(强连通分量缩点求根节点和叶子节点的个数)相关推荐

  1. POJ - 1236 Network of Schools(强连通缩点)

    题目链接:点击查看 题目大意:一个学校连接在一个计算机网络上,学校之间存在软件支援协议,每个学校都有它应支援的学校名单(学校A支援学校B,并不表示学校B一定支援学校A).当某校获得一个新软件时,无论是 ...

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

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

  3. [tarjan] poj 1236 Network of Schools

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

  4. POJ1236Network of Schools——强连通分量缩点建图

    [题目描述] A number of schools are connected to a computer network. Agreements have been developed among ...

  5. poj 1236 Network of Schools (强连通分支缩点)

    Description A number of schools are connected to a computer network. Agreements have been developed ...

  6. POJ 1236 Network of Schools(tarjan)

    Network of Schools Description A number of schools are connected to a computer network. Agreements h ...

  7. POJ - 1236 Network of Schools

    A number of schools are connected to a computer network. Agreements have been developed among those ...

  8. poj 1236 Network of Schools

    题目描述:有一些学校连接到一个计算机网络.这些学校之间达成了一个协议:每个学校维护着一个学校列表,它向学校列表中的学校发布软件.注意,如果学校B在学校A的列表中,则A不一定在B的列表中.任务A:计算为 ...

  9. POJ 2186 Popular Cows(强连通分量缩点,Tarjan算法)

    [题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=16578 [解题报告] 给你一个有向图,问你有多少个点可以被其它 ...

最新文章

  1. 【搜索专题】BFS中的多源BFS-双端队列BFS
  2. 【研究报告】城市云脑,智慧城市2.0产生背后的深层原因,两个重要特征是关键
  3. RS232与RS485的功能与区别!
  4. 简单python画圣诞树图片-python圣诞树代码
  5. 北京python培训班价格-北京python培训一对一
  6. C语言侏儒排序Gnome sort 算法(附完整源码)
  7. php 删除硬链接,为什么要用软链接硬链接
  8. TensorFlow2-高阶操作
  9. 1.1收集域名信息-完整介绍
  10. UVA10608 Friends【并查集】
  11. JS 运算、判断优化
  12. 分享不会迟到,只会来的晚一点——16倍速视频播放
  13. HTTP报文格式详解
  14. ABAP里面的OCCURS与HEADER LINE之间的一些区别
  15. pcfg 自然语言处理_自然语言处理的笔记
  16. 文件搜索(File)
  17. Python实现冒泡排序,从小到大输出(bubble)
  18. GlobeLand30影像下载、去除黑边、镶嵌、裁剪、重分类
  19. 半导体车间测量尘埃粒子浓度等级仪器解决方案
  20. 【Python数据结构与算法】(三):递归(Recursion)

热门文章

  1. Java高并发编程详解系列-内存模型
  2. 面试题之Java内存区域
  3. Mybatis There is no getter for property named 'XXX' in 'class java.lang.XXX
  4. gin源码解析(1) - gin 与 net/http 的关系
  5. php oop基础,php面向对象编程(oop)基础
  6. 关于面试时碰到的几个多线程手撕代码题
  7. 说说关于JVM三色标记算法
  8. 海量数据的topK问题
  9. es使用pencentiles对网站访问延时统计
  10. 04.Android之动画问题