684. 冗余连接(leetcode)

题目描述

在本问题中, 树指的是一个连通且无环的无向图。

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

结果图是一个以边组成的二维数组。每一个边的元素是一对[u, v] ,满足 u < v,表示连接顶点u 和v的无向图的边。

返回一条可以删去的边,使得结果图是一个有着N个节点的树。如果有多个答案,则返回二维数组中最后出现的边。答案边 [u, v] 应满足相同的格式
u < v。

示例

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

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

代码

public class 冗余边 {
        public int[] findRedundantConnection(int[][] edges) {
            if (edges == null || edges.length == 0 || edges[0].length == 0) {
                return null;
            }

            int n = edges.length;

            // 构造邻接表
            Set<Integer>[] adjList = new Set[n + 1];
            for (int[] edge : edges) {
                int from = edge[0];
                int to = edge[1];
                if (adjList[from] == null) {
                    adjList[from] = new HashSet<>();
                }
                adjList[from].add(to);
                if (adjList[to] == null) {
                    adjList[to] = new HashSet<>();
                }
                adjList[to].add(from);
            }

            for (int i = n - 1; i >= 0; i--) {
                int from = edges[i][0];
                int to = edges[i][1];
                adjList[from].remove(to);
                adjList[to].remove(from);
                if (DFS(from, to, adjList, new boolean[n + 1])) {
                    // 如果删除了边之后,也能遍历所有的节点,说明这条边是多余的
                    int[] res = {from, to};
                    return res;
                }
                adjList[from].add(to);
                adjList[to].add(from)
            }
            return new int[0];
        }

        /**
         * @param start      起点
         * @param target     目标点
         * @param adjList    邻接表
         * @param hasVisited 标记结点是否访问
         * @return boolean
         * @description 深度优先遍历,看看能不能从起点,找到目标点
         */
        private boolean DFS(int start, int target, Set<Integer>[] adjList, boolean[] hasVisited) {
            if (start == target)  //访问到目标点,返回true
                return true;
            hasVisited[start] = true;//标记为已访问
            if (adjList[start] != null) {
                // 取出与该顶点连接的所有节点
                for (Integer next : adjList[start]) {
                    if (hasVisited[next])
                        continue;
                    if (DFS(next, target, adjList, hasVisited))
                        return true;
                }
            }
            return false;
        }

}


思路

  1. 易知,只有一条边是多余的,所以只要找出这条边就行了。
  2. 将传入的二维数组转化为邻接表,用有序的HashSet集合作为元素的数组adjList,其中数组的下标表示顶点,而数组下标对应的集合存储着连接该顶点的其他点。
  3. 转化之后就可以做假设,假设去除某条边,看其是不是多余的,如果是多余的,则可以在删除边后的顶点开始遍历,可以遍历完所有顶点,如果不行,则这条边不是多余的。
  4. 遍历采用DFS(深度优先遍历)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值