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

图论题目,须要解决这个问题:

1 使用Tarjan算法求子强连通图

2 标识顶点属于哪个子强连通图

3 计算各个子强连通图的零入度数和零出度数

图论中高级内容了,是有点难度的,不细心一点肯定会出错的。

这次本博主认真注解好差点儿每一个语句。希望大家能够follow我的程序。

#include <cstdio>
#include <stack>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std; const int MAX_N = 101;//最大的顶点数
vector<int> graAdj[MAX_N];//vector表示邻接表法
int visNo[MAX_N];//记录深搜各个顶点的訪问顺序标号
int lowLink[MAX_N];//连通图的最低标识号,记录好是否递归到已经訪问过的顶点了。假设是,那么就以最低的顶点訪问顺序标号为,这样能够统一子连通图的标号。 通过推断当前最低连通图的标识号和訪问顺序号是否一致来推断是否找到了一个子强连通图
int dfsNo;//记录深搜总的訪问号
int connectNo;//当前的子强连通图的标号,终于为全部子强连通图的数量
int markNo[MAX_N];//markNo[v]代表顶点v属于子强连通图markNo[v],其值就为子强连通图
int in[MAX_N], out[MAX_N];//分别记录一个子强连通图的入度数和出度数
stack<int> stk;//深搜顶点入栈,找到子强连通图时候出栈,直到当前顶点数,全部点都属于同一个子强连通图 //深搜查找子强连通图,并记录好顶点属于哪个子强连通图
void getStrongConnected(int u)
{
visNo[u] = lowLink[u] = ++dfsNo;//第一次进入当前顶点的时候的初值
int n = (int)graAdj[u].size();
stk.push(u);
for (int i = 0; i < n; i++)//遍历当前顶点的全部连接点
{
int v = graAdj[u][i];
if (!visNo[v])//没有訪问过的时候
{
getStrongConnected(v);//递归
lowLink[u] = min(lowLink[u], lowLink[v]);//记录最低序号
}
//已经訪问过,可是还在栈里面,即还没有记录该顶点属于哪个强连通图
else if (!markNo[v]) lowLink[u] = min(lowLink[u], lowLink[v]);
}
if (visNo[u] == lowLink[u])//当前訪问顺序号等于最低标号,
{//那么就是找到了一个子强连通图
++connectNo;//每次要添加全局的连通标号
int v;
do
{
v = stk.top(); stk.pop();
markNo[v] = connectNo;//顶点对用强连通图号
} while (u != v);
}
} void Tarjan(int n)
{
//前期清零工作
dfsNo = 0, connectNo = 0;
fill(visNo, visNo+n+1, 0);
fill(lowLink, lowLink+n+1, 0);
fill(markNo, markNo+n+1, 0);
while (!stk.empty()) stk.pop(); for (int u = 1; u <= n; u++)
{
//某些顶点或许是分离的。就是图的顶点有不相连的,故此要遍历全部顶点
if (!visNo[u]) getStrongConnected(u);
}
} int main()
{
int N, u, v;
scanf("%d", &N);
for (u = 1; u <= N; u++)
{
scanf("%d", &v);
while (v)//为零表示结束
{
graAdj[u].push_back(v);//使用vector建立一个邻接表
scanf("%d", &v);
}
}
Tarjan(N);//计算子强连通图的个数,并表出各个顶点属于哪个子强连通图 for (u = 1; u <= N; u++)
{//遍历全部顶点,然后遍历顶点的邻接边。相当于遍历全部边
for (int i = 0; i < (int)graAdj[u].size(); i++)
{
int v = graAdj[u][i];
if (markNo[u] != markNo[v])//不是属于同一个子强连通图
{//分别添加该强连通图的入度和出度
out[markNo[u]]++;
in[markNo[v]]++;
}
}
} int zeroIn = 0, zeroOut = 0;
for (int i = 1; i <= connectNo; i++)
{
if (in[i] == 0) zeroIn++;
if (out[i] == 0) zeroOut++;
}
//入度为零则须要放置一个软件拷贝
printf("%d\n", zeroIn);
//变为一个强连通图,分2个情况:1 本身是一个强连通图;2 零入度或出度最大值
printf("%d\n", connectNo == 1? 0 : max(zeroIn, zeroOut)); return 0;
}

POJ 1236 Network of Schools 强连通图的更多相关文章

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

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

  2. Poj 1236 Network of Schools (Tarjan)

    题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...

  3. POJ 1236 Network of Schools(强连通分量)

    POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...

  4. poj 1236 Network of Schools(连通图入度,出度为0)

    http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  5. poj 1236 Network of Schools(又是强连通分量+缩点)

    http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  6. [tarjan] poj 1236 Network of Schools

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

  7. POJ 1236——Network of Schools——————【加边形成强连通图】

    Network of Schools Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u ...

  8. poj 1236 Network of Schools【强连通求孤立强连通分支个数&&最少加多少条边使其成为强连通图】

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13800   Accepted: 55 ...

  9. POJ 1236 Network of Schools(Tarjan缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16806   Accepted: 66 ...

随机推荐

  1. C#操作Mysql类

    using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Text. ...

  2. 仿QQ浏览器mac版官网主页 html+css3特效

    这是一款超酷的CSS3动态背景动画特效,CSS3仿QQ浏览器官网彗星动画背景特效. 在线演示本地下载

  3. Java基础4一数组

    数组 数组是用来存储一组相同类型数据的数据结构 数组变量属于引用数据类型 数组中的元素可以是任何数据类型(基本类型和引用类型) 备注:数组中存放的都是相同数据类型的数据. 1.一维数组的声明 语法:数 ...

  4. Android ToolBar自定义图标,关联DrawerLayout

    Android5.0出现了一个可以代替ActionBar的控件ToolBar,使用更加灵活,一般我们使用ToolBar来和DrawerLayout配合使用,官方提供了一个开关类ActionBarDra ...

  5. 揭秘IPHONE X刷脸认证的技术奥秘

    苹果最新发布的Iphone X具有一个全新的功能叫做刷脸认证,背后的技术其实是生物密码的更新,通过人脸识别取代了传统的指纹识别,大家肯定对这种新技术非常感兴趣,下面我们通过这篇文章为大家介绍人脸识别的 ...

  6. root密码忘记怎么办?

    忘记root密码:按 e进入内核在按e,后面加1 .按b启动 进入命令行输入passwd,设置新的密码后exit退出即可

  7. 京东专业“卖”队友,魅族手环将亮相1206魅蓝note新品发布会

    京东一直是国内顶级的数码产品自营销售渠道,但是,正因为庞大的数据体系和平台特殊性,经常会帮我们发现一些“好玩的”保密性较高的东西,譬如价格.信息.谍照等.而在最新上线的京东超级品牌日活动页面上,专业“ ...

  8. (转)Bootstrap 之 Metronic 模板的学习之路 - (7)GULP 前端自动化工具

    https://segmentfault.com/a/1190000006738327 初步了解 Metronic 的结构和应用后,我们就可以在项目中应用起来了.考虑到实际项目应用时,会有很多文件需要 ...

  9. 创建一个dynamics CRM workflow (三) - Creating Configuration Entity for Custom Workflow

    上个帖子中, 我们创建了个发email的workflow. 但是我们邮件当中的tax 值是 hard code, 这在开发当中是不容许的. 那今天我们来把这个build in workflow用 in ...

  10. 一款 App 开发到上架

    随着互联网时代的发展,越来越多的 App 诞生啦.App 是手机软件的简称,手机主流的有 iOS.Andriod. 开发一个 App 需要哪些步骤呢?下面我和大家分享一下. 一.APP 的 idea( ...