码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode-684. Redundant Connection [C++][Java]


    LeetCode-684. Redundant ConnectionLevel up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.https://leetcode.com/problems/redundant-connection/

    In this problem, a tree is an undirected graph that is connected and has no cycles.

    You are given a graph that started as a tree with n nodes labeled from 1 to n, with one additional edge added. The added edge has two different vertices chosen from 1 to n, and was not an edge that already existed. The graph is represented as an array edges of length n where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the graph.

    Return an edge that can be removed so that the resulting graph is a tree of n nodes. If there are multiple answers, return the answer that occurs last in the input.

    Example 1:

    Input: edges = [[1,2],[1,3],[2,3]]
    Output: [2,3]
    

    Example 2:

    Input: edges = [[1,2],[2,3],[3,4],[1,4],[1,5]]
    Output: [1,4]
    

    Constraints:

    • n == edges.length
    • 3 <= n <= 1000
    • edges[i].length == 2
    • 1 <= ai < bi <= edges.length
    • ai != bi
    • There are no repeated edges.
    • The given graph is connected.

    【C++】

    1. 并查集

    1. class UF {
    2. vector<int> id, size;
    3. public:
    4. UF(int n): id(n), size(n, 1) {
    5. iota(id.begin(), id.end(), 0); // iota函数可以把数组初始化为0到n-1
    6. }
    7. int find(int p) {
    8. while (p != id[p]) {
    9. id[p] = id[id[p]]; // 路径压缩,使得下次查找更快
    10. p = id[p];
    11. }
    12. return p;
    13. }
    14. void connect(int p, int q) {
    15. int i = find(p), j = find(q);
    16. if (i != j) {
    17. // 按秩合并:每次合并都把深度较小的集合合并在深度较大的集合下面
    18. if (size[i] < size[j]) {
    19. id[i] = j;
    20. size[j] += size[i];
    21. } else {
    22. id[j] = i;
    23. size[i] += size[j];
    24. }
    25. }
    26. }
    27. bool isConnected(int p, int q) {
    28. return find(p) == find(q);
    29. }
    30. };
    31. class Solution {
    32. public:
    33. vector<int> findRedundantConnection(vectorint>>& edges) {
    34. int n = edges.size();
    35. UF uf(n + 1);
    36. for (auto e: edges) {
    37. int u = e[0], v = e[1];
    38. if (uf.isConnected(u, v)) {return e;}
    39. uf.connect(u, v);
    40. }
    41. return vector<int>{-1, -1};
    42. }
    43. };

    2. 粗暴循环打法

    1. class Solution {
    2. public:
    3. vector<int> findRedundantConnection(vectorint>>& edges) {
    4. int n = 0;
    5. for(const auto &e : edges) {
    6. n = max(e[0]+1, n);
    7. n = max(e[1]+1, n);
    8. }
    9. parent_ = vector<int>(n);
    10. for(int i = 0; i < n; ++i) parent_[i] = i;
    11. for(auto it = edges.begin(); it != edges.end(); ++it) {
    12. const auto &e = *it;
    13. if(GetParent(e[0]) == GetParent(e[1])) {
    14. if(e[0] < e[1]) return {e[0], e[1]};
    15. else return {e[1], e[0]};
    16. }
    17. parent_[GetParent(e[0])] = GetParent(e[1]);
    18. }
    19. return {};
    20. }
    21. private:
    22. int GetParent(int x) {
    23. if(parent_[x] != x) parent_[x] = GetParent(parent_[x]);
    24. return parent_[x];
    25. }
    26. vector<int> parent_;
    27. };

    【Java】

    1. class Solution {
    2. private int[] parent_;
    3. public int[] findRedundantConnection(int[][] edges) {
    4. int n = 0;
    5. for(int[] e : edges) {
    6. n = Math.max(e[0]+1, n);
    7. n = Math.max(e[1]+1, n);
    8. }
    9. parent_ = new int[n];
    10. for(int i = 0; i < n; ++i) parent_[i] = i;
    11. for(int[] e : edges) {
    12. if (GetParent(e[0]) == GetParent(e[1])) {
    13. if(e[0] < e[1]) return new int[]{e[0], e[1]};
    14. else return new int[]{e[1], e[0]};
    15. }
    16. parent_[GetParent(e[0])] = GetParent(e[1]);
    17. }
    18. return new int[]{};
    19. }
    20. private int GetParent(int x) {
    21. if(parent_[x] != x) parent_[x] = GetParent(parent_[x]);
    22. return parent_[x];
    23. }
    24. }

  • 相关阅读:
    【CV】Landslide4Sense-2022
    基于共词分析的中国近代史实体关系图构建(毕业设计:图数据渲染)
    决胜B端产品经理学习笔记03
    Springboot泊车收费管理系统97439计算机毕业设计-课程设计-期末作业-毕设程序代做
    超详细Python教程——修改和增加类属性
    java-python高校大学教室管理系统
    Python 蝉联榜首,PHP 即将跌出前十 | TIOBE 11 月编程语言排行榜
    linux源码安装postgresql以及smlar插件
    2023年【安全生产监管人员】考试报名及安全生产监管人员复审考试
    MySQL何时适合创建索引,需要注意什么以及创建原则
  • 原文地址:https://blog.csdn.net/qq_15711195/article/details/126456032
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号