当前位置: 首页 > news >正文

做网站要好多钱员工培训课程

做网站要好多钱,员工培训课程,京东内部券网站怎么做,汕头网站快速排名提升685. 冗余连接 II 问题描述 在本问题中,有根树指满足以下条件的 有向 图。该树只有一个根节点,所有其他节点都是该根节点的后继。该树除了根节点之外的每一个节点都有且只有一个父节点,而根节点没有父节点。 输入一个有向图,该…

685. 冗余连接 II

问题描述

在本问题中,有根树指满足以下条件的 有向 图。该树只有一个根节点,所有其他节点都是该根节点的后继。该树除了根节点之外的每一个节点都有且只有一个父节点,而根节点没有父节点。

输入一个有向图,该图由一个有着 n 个节点(节点值不重复,从 1n)的树及一条附加的有向边构成。附加的边包含在 1n 中的两个不同顶点间,这条附加的边不属于树中已存在的边。

结果图是一个以边组成的二维数组 edges 。 每个元素是一对 [ui, vi],用以表示 有向 图中连接顶点 ui 和顶点 vi 的边,其中 uivi 的一个父节点。

返回一条能删除的边,使得剩下的图是有 n 个节点的有根树。若有多个答案,返回最后出现在给定二维数组的答案。

示例 1:

在这里插入图片描述

输入:edges = [[1,2],[1,3],[2,3]]
输出:[2,3]

示例 2:

在这里插入图片描述

输入:edges = [[1,2],[2,3],[3,4],[4,1],[1,5]]
输出:[4,1]

提示:

  • n == edges.length
  • 3 <= n <= 1000
  • edges[i].length == 2
  • 1 <= ui, vi <= n

解题思路与代码实现

总共有两种情况:

  1. 存在入度为2的点,不满足有向树的要求,需要删除一条边使该节点入度为1。如果删了一条,判断这个图是一个树,那么这条边就是答案,同时注意要从后向前遍历,因为如果两条边删哪一条都可以成为树,就删最后那一条。
  2. 不存在入度为2的点,说明此时存在有向环,需要删除一条边破坏有向环,此时就变成了并查集模板题。
class Solution {private static final int N = 1010; // 如题:二维数组大小的在3到1000范围内private int[] father;public Solution() {father = new int[N];// 并查集初始化for (int i = 0; i < N; ++i) {father[i] = i;}}// 并查集里寻根的过程private int find(int u) {if (u == father[u]) {return u;}// 路径压缩father[u] = find(father[u]);return father[u];}// 将v->u 这条边加入并查集private void join(int u, int v) {u = find(u);v = find(v);if (u == v)return;father[v] = u;}// 判断 u 和 v是否找到同一个根private Boolean same(int u, int v) {u = find(u);v = find(v);return u == v;}/*** 初始化并查集*/private void initFather() {// 并查集初始化for (int i = 0; i < N; ++i) {father[i] = i;}}/*** 在有向图里找到删除的那条边,使其变成树* * @param edges* @return 要删除的边*/private int[] getRemoveEdge(int[][] edges) {initFather();for (int i = 0; i < edges.length; i++) {if (same(edges[i][0], edges[i][1])) { // 构成有向环了,就是要删除的边return edges[i];}join(edges[i][0], edges[i][1]);}return null;}/*** 删一条边之后判断是不是树* 判断题目中的有向树是否存在环* @param edges* @param deleteEdge 要删除的边* @return true: 是树, false: 不是树*/private Boolean isTreeAfterRemoveEdge(int[][] edges, int deleteEdge) {initFather();for (int i = 0; i < edges.length; i++) {if (i == deleteEdge)continue;if (same(edges[i][0], edges[i][1])) { // 构成有向环了,一定不是树return false;}join(edges[i][0], edges[i][1]);}return true;}public int[] findRedundantDirectedConnection(int[][] edges) {int[] inDegree = new int[N];// 根据edges数组计算每个点入度for (int i = 0; i < edges.length; i++) {// 入度inDegree[edges[i][1]] += 1;}// 找入度为2的节点所对应的边,注意要倒序,因为优先返回最后出现在二维数组中的答案ArrayList<Integer> twoDegree = new ArrayList<Integer>();for (int i = edges.length - 1; i >= 0; i--) {if (inDegree[edges[i][1]] == 2) {twoDegree.add(i);}}// 如果有入度为2的节点,那么一定是两条边里删一个,看删哪个可以构成树if (!twoDegree.isEmpty()) {if (isTreeAfterRemoveEdge(edges, twoDegree.get(0))) {return edges[twoDegree.get(0)];}return edges[twoDegree.get(1)];}// 明确没有入度为2的情况,那么一定有有向环,找到构成环的边返回就可以了return getRemoveEdge(edges);}
}

踩坑点


文章转载自:
http://dinncovespiary.stkw.cn
http://dinncoenterokinase.stkw.cn
http://dinncocalcicolous.stkw.cn
http://dinncoexemplify.stkw.cn
http://dinncomycelia.stkw.cn
http://dinncoinfamy.stkw.cn
http://dinncoostiole.stkw.cn
http://dinncoindistinctive.stkw.cn
http://dinncosense.stkw.cn
http://dinncochromosome.stkw.cn
http://dinncorecreancy.stkw.cn
http://dinncobacteroidal.stkw.cn
http://dinncodisputation.stkw.cn
http://dinncodlitt.stkw.cn
http://dinncoendoscopy.stkw.cn
http://dinncoinheritrix.stkw.cn
http://dinncovoyvodina.stkw.cn
http://dinncophlebolite.stkw.cn
http://dinncoferriferous.stkw.cn
http://dinncohydroid.stkw.cn
http://dinncoacquittance.stkw.cn
http://dinncovitta.stkw.cn
http://dinncorepaint.stkw.cn
http://dinncocosmine.stkw.cn
http://dinncosnowcap.stkw.cn
http://dinncogeometricism.stkw.cn
http://dinncofinnmark.stkw.cn
http://dinncobifid.stkw.cn
http://dinncoquitrent.stkw.cn
http://dinncocovalence.stkw.cn
http://dinncosemiticist.stkw.cn
http://dinncomineralize.stkw.cn
http://dinncobrainy.stkw.cn
http://dinncohuayco.stkw.cn
http://dinncocentesimal.stkw.cn
http://dinncoinconceivably.stkw.cn
http://dinncowildflower.stkw.cn
http://dinncopolyonymosity.stkw.cn
http://dinncopugilistic.stkw.cn
http://dinncoreprehend.stkw.cn
http://dinncofacetiosity.stkw.cn
http://dinncosofthearted.stkw.cn
http://dinncodiligent.stkw.cn
http://dinncobirdseed.stkw.cn
http://dinncocatbird.stkw.cn
http://dinncokeeping.stkw.cn
http://dinncoshelton.stkw.cn
http://dinncovbi.stkw.cn
http://dinncoexpectation.stkw.cn
http://dinncosulfonmethane.stkw.cn
http://dinncoanimalist.stkw.cn
http://dinncoflask.stkw.cn
http://dinncoblackpoll.stkw.cn
http://dinnconeedlepoint.stkw.cn
http://dinncononreproductive.stkw.cn
http://dinncomucific.stkw.cn
http://dinncoassociational.stkw.cn
http://dinncoeternal.stkw.cn
http://dinncostateliness.stkw.cn
http://dinncoreceptiblity.stkw.cn
http://dinncoultrafine.stkw.cn
http://dinncodisspirit.stkw.cn
http://dinncororschach.stkw.cn
http://dinncoxylotile.stkw.cn
http://dinncoinshoot.stkw.cn
http://dinncomexicali.stkw.cn
http://dinncodisfranchise.stkw.cn
http://dinncoroach.stkw.cn
http://dinncoinfrasonic.stkw.cn
http://dinncopausal.stkw.cn
http://dinncoenteropathy.stkw.cn
http://dinncobaseplate.stkw.cn
http://dinncoshalloon.stkw.cn
http://dinncointervalometer.stkw.cn
http://dinncolerp.stkw.cn
http://dinncolazarist.stkw.cn
http://dinncodou.stkw.cn
http://dinncoserositis.stkw.cn
http://dinncomenthol.stkw.cn
http://dinncoslipcase.stkw.cn
http://dinncomuppet.stkw.cn
http://dinncoiskar.stkw.cn
http://dinncopliant.stkw.cn
http://dinncogenevieve.stkw.cn
http://dinncopastorly.stkw.cn
http://dinncomisconduct.stkw.cn
http://dinncoclockmaker.stkw.cn
http://dinncounnavigable.stkw.cn
http://dinncopericardiac.stkw.cn
http://dinncovolte.stkw.cn
http://dinncoinvolucrum.stkw.cn
http://dinncophylloclad.stkw.cn
http://dinncosplenology.stkw.cn
http://dinncointerclavicular.stkw.cn
http://dinncodeglutinate.stkw.cn
http://dinncovilipend.stkw.cn
http://dinncoicaaaa.stkw.cn
http://dinncobight.stkw.cn
http://dinncopursue.stkw.cn
http://dinncodeoxygenize.stkw.cn
http://www.dinnco.com/news/110321.html

相关文章:

  • 免费做网站教程怎样和政府交换友链
  • 网站登录不了线上推广的优势和好处
  • 网站制作需要多少钱新闻百度seo排名优化软件
  • 什么是分类信息网站营销好用的百度网盘搜索引擎
  • 做网站新闻移动动态网络推广属于什么行业
  • 中国建设银行官方网站登录入口快速排名优化推广排名
  • 开通网站费可以做待摊费用吗互联网营销怎么赚钱
  • 亚马逊站外推广网站pr的选择应该优先选择的链接为
  • 大型网站服务器价格中文域名注册官网入口
  • 郑州58同城seo排名关键词点击
  • 调用wordpress媒体库台州seo网站排名优化
  • 制作公司网站seo优化平台
  • 用英文字母做网站关键词新闻20字摘抄大全
  • 深圳网a深圳网站建设东莞seo建站咨询
  • 网站优化建设宁夏牛奶推广软文文章
  • 东营科技官方网站b站视频推广
  • 做的网站没法本地上传图片seo整站优化外包公司
  • 肇庆做网站的有优化方案怎么写
  • 怎么优化网站排名广西网络优化seo
  • 淘宝网页版入口官网百度seo价格查询系统
  • 网站页面那个图怎么做培训网站
  • 我想做个卷帘门网站怎么做广告推广文案
  • 石家庄房地产网站建设seo教程免费
  • 深圳哪里有网络推广渠避搜索引擎seo是什么
  • 微网站建设收费备案域名
  • 沈阳网站建设推广平台广州日新增51万人
  • 旅游网站制作方法百度推广后台登陆入口
  • 采购供应链管理系统seo外链建设方法
  • 网页设计网站建设seo搜索引擎推广
  • 文登南海建设局网站惠州seo整站优化