2022-05-15:N个学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输。
问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件;
问题2:至少需要添加几条传输线路(边),使任意向一个学校发放软件后。
经过若干次传送,网络内所有的学校最终都能得到软件。
2 <= N <= 1000。
从题意中抽象出的算法模型, 给定一个有向图,求:

  1. 至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点;
  2. 至少要加多少条边,才能使得从任何一个顶点出发,都能到达全部顶点。
    测试链接 : http://poj.org/problem?id=1236,
    注册一下 -> 页面上点击"submit" -> 语言选择java,
    然后把如下代码粘贴进去, 把主类名改成"Main", 可以直接通过。
    强连通分量练习题目。

答案2022-05-15:

tarjan算法。

代码用golang编写。代码如下:

package main

import "fmt"

var sc = []int{5, 2, 4, 3, 0, 4, 5, 0, 0, 0, 1, 0}
var ii = 0 func next() int {
ii++
return sc[ii-1]
}
func hasNext() bool {
return ii < len(sc)
}
func main() {
for hasNext() {
n := next()
edges := make([][]int, 0)
for i := 0; i <= n; i++ {
edges = append(edges, make([]int, 0))
}
for from := 1; from <= n; from++ {
to := 0
to = next()
for to != 0 {
edges[from] = append(edges[from], to)
to = next()
}
}
scc := NewStronglyConnectedComponents(edges)
sccn := scc.getSccn()
in := make([]int, sccn+1)
out := make([]int, sccn+1)
dag := scc.getShortGraph()
for i := 1; i <= sccn; i++ {
for _, j := range dag[i] {
out[i]++
in[j]++
}
}
zeroIn := 0
zeroOut := 0
for i := 1; i <= sccn; i++ {
if in[i] == 0 {
zeroIn++
}
if out[i] == 0 {
zeroOut++
}
}
fmt.Println(zeroIn)
fmt.Println(sccn == 1, 0, getMax(zeroIn, zeroOut))
}
} func getMax(a, b int) int {
if a > b {
return a
} else {
return b
}
} type StronglyConnectedComponents struct {
nexts [][]int
n int
stack []int
stackSize int
dfn []int
low []int
cnt int
scc []int
sccn int
} // 请保证点的编号从1开始,不从0开始
// 注意:
// 如果edges里有0、1、2...n这些点,那么容器edges的大小为n+1
// 但是0点是弃而不用的,所以1..n才是有效的点,所以有效大小是n
func NewStronglyConnectedComponents(edges [][]int) *StronglyConnectedComponents {
ans := &StronglyConnectedComponents{}
ans.nexts = edges
ans.init()
ans.scc0()
return ans
} func (this *StronglyConnectedComponents) init() {
this.n = len(this.nexts)
this.stack = make([]int, this.n)
this.stackSize = 0
this.dfn = make([]int, this.n)
this.low = make([]int, this.n)
this.cnt = 0
this.scc = make([]int, this.n)
this.sccn = 0
this.n--
} func (this *StronglyConnectedComponents) scc0() {
for i := 1; i <= this.n; i++ {
if this.dfn[i] == 0 {
this.tarjan(i)
}
}
} func (this *StronglyConnectedComponents) tarjan(p int) {
this.cnt++
this.dfn[p] = this.cnt
this.low[p] = this.cnt
this.stack[this.stackSize] = p
this.stackSize++
for _, q := range this.nexts[p] {
if this.dfn[q] == 0 {
this.tarjan(q)
}
if this.scc[q] == 0 {
if this.low[p] > this.low[q] {
this.low[p] = this.low[q]
}
}
}
if this.low[p] == this.dfn[p] {
this.sccn++
top := 0
for {
this.stackSize--
top = this.stack[this.stackSize]
this.scc[top] = this.sccn
if top == p {
break
}
}
}
} func (this *StronglyConnectedComponents) getScc() []int {
return this.scc
} func (this *StronglyConnectedComponents) getSccn() int {
return this.sccn
} func (this *StronglyConnectedComponents) getShortGraph() [][]int {
shortGraph := make([][]int, 0)
for i := 0; i <= this.sccn; i++ {
shortGraph = append(shortGraph, make([]int, 0))
}
for u := 1; u <= this.n; u++ {
for _, v := range this.nexts[u] {
if this.scc[u] != this.scc[v] {
shortGraph[this.scc[u]] = append(shortGraph[this.scc[u]], this.scc[v])
}
}
}
return shortGraph
}

执行结果如下:


左神java代码

2022-05-15:N个学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输。 问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件; 问题2:至的更多相关文章

  1. 如何在Windows系统上用抓包软件Wireshark截获iPhone等网络通讯数据

    http://www.jb51.net/os/windows/189090.html 今天给大家介绍一种如何在Windows操作系统上使用著名的抓包工具软件Wireshark来截获iPhone.iPa ...

  2. 安装xmlspy之后,链接及邮箱等都用这个软件打开,怎样取消?

    安装xmlspy之后,链接及邮箱等都用这个软件打开,怎样取消? 安装xmlspy之后,大部分的链接就会用这个软件打开,比较糟心.所以尝试很多的方法,终于解决了. (1)打开控制面板,找到默认程序: ( ...

  3. oracle12安装软件后安装数据库,然后需要自己配置监听

    oracle12安装软件后安装数据库,然后需要自己配置监听 没想到你是这样的oracle12: 不能同时安装软件和数据库,分别安装之后,\NETWORD\ADMIN\下面竟然没有listener.or ...

  4. 软件工程师需要了解的网络知识:从铜线到HTTP(一)—— 前言

    转自:https://lvwenhan.com/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/485.html?hmsr=toutiao.io&utm_medium ...

  5. Day 18 软件管理3之搭建网络仓库

    搭建一个网络仓库 服务端: 10.0.0.200   1.准备软件包( 1.光盘 2.缓存 3.联网下载 4.同步 ) 2.通过p共享软件包存放的目录 3.将光盘中的软件包都拷贝至p的共享目录下 4. ...

  6. MySQL 4.1-5.0-5.1-5.5-5.6各版本的主要区别

    各版本的一些命令差异:  show innodb status\G mysql-5.1  show engines innodb status\G mysql-5.5  关于grant授权 mysql ...

  7. 2015.05.15,外语,学习笔记-《Word Power Made Easy》 01 “如何讨论人格特点”

    2015.03.17,外语,读书笔记-<Word Power Made Easy> 01 “如何讨论人格特点”学习笔记 SESSIONS 1 本来这些章节都是在一两年前学习的,现在趁给友人 ...

  8. Case of the Zeros and Ones 分类: CF 2015-07-24 11:05 15人阅读 评论(0) 收藏

    A. Case of the Zeros and Ones time limit per test 1 second memory limit per test 256 megabytes input ...

  9. 2016/05/15 ThinkPHP3.2.2 表单自动验证实例 验证规则的数组 直接写在相应的控制器里

    使用TP 3.2框架 验证规则也可以写到模型里,但感觉有些麻烦, 一是有时候不同页面验证的方式会不一样, 二是看到这个   Add  事件里的代码,就清楚要接收什么数据,如何验证数据能够在第一眼有个大 ...

  10. 2015.05.15,外语,学习笔记-《Word Power Made Easy》 02 “如何谈论医生”

    包括Sessions 4-6: Prefix Person,nous,etc. Practice,etc. Adjective internus内部 internist [ɪn'tɝnɪst] n.内 ...

随机推荐

  1. Swust OJ977: 统计利用先序遍历创建的二叉树中的空链域个数

    题目描述 利用先序递归遍历算法创建二叉树并计算该二叉树中的空链域个数. 输入 输入为接受键盘输入的由大写英文字符和"#"字符构成的一个字符串(用于创建对应的二叉树). 输出 输出该 ...

  2. AttributeError: module 'torchvision' has no attribute 'transforms'

    代码:maskrcnn-benchmark Python 3.6.13 |Anaconda, Inc Traceback (most recent call last): File "too ...

  3. 学习ASP.NET Core Blazor编程系列二十八——JWT登录(3)

    学习ASP.NET Core Blazor编程系列文章之目录 学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应 ...

  4. linux网络编程中的errno处理

    在Linux网络编程中,errno是一个非常重要的变量.它记录了最近发生的系统调用错误代码.在编写网络应用程序时,合理处理errno可以帮助我们更好地了解程序出现的问题并进行调试. 通常,在Linux ...

  5. Python学习笔记--图像的进一步学习

    演示地图的可视化的实现 示例: 设置全局选项 可以设置出不同的颜色,不会显得很干巴: 国内地图: 那么,我们应当如何找到相对应的颜色的编号呢? 基本步骤: 前往ab173.com网站 然后在,前端那里 ...

  6. Python实战项目-10文件存储/支付宝支付/支付成功回调接口

    文件存储 视频文件存储在某个位置,如果放在自己服务器上 放在项目的media文件夹 服务器上线后,用户既要访问接口,又需要看视频,都是使用一个域名和端口 分开:问价你单独放在文件服务器上,文件服务器带 ...

  7. Innodb的Buffer Pool

    什么是Buffer Pool 为了缓存磁盘中的页,MySQL服务器启动的时候就向操作系统申请了一片连续的内存,他们给这片内存起了个名,叫做Buffer Pool(中文名是缓冲池).innodb_buf ...

  8. openfoam并行通信探索(一)

    前言 最近在忙,快一两周没更新了,今天说下如何实现openfoam内的并行通信 为什么要并行通信 说到并行通信大家不要害怕啊,只是不同核之间数据传递,比如说咱们仿真开16个核,3号计算单元对4号计算单 ...

  9. 你可能需要的 6 个 React 开发小技巧

    ​ 这是一个可怕的问题,在React中,我们经常会编写条件语句来显示不同的视图,比如这个简单的例子. const App = () => { return ( <> { loadin ...

  10. java多线性--线程创建

    java多线性--线程创建 什么是多线程:不同的功能同时进行 Process(进程)与Thread(线程) 进程是执行程序的一次执行过程,是一个动态的概念.是系统分配资源的单位. 一个进程分为多个线程 ...