上岸 I LeetCode Weekly Contest 194解題報告

2021-02-20 北美上岸君

class Edge {
    int id, a, b, v;

    public Edge(int id, int a, int b, int v) {
        this.id = id;
        this.a = a;
        this.b = b;
        this.v = v;
    }
}

class UnionFind {
    int[] fa;

    public UnionFind(int n) {
        fa = new int[n];
        Arrays.fill(fa, -1);
    }

    public int find(int x) {
        if (fa[x] < 0) return x;
        return fa[x] = find(fa[x]);
    }

    public void merge(int a, int b) {
        int Fa = find(a);
        int Fb = find(b);
        fa[Fa] = Fb;
    }
}

class Solution {

    public List<List<Integer>> findCriticalAndPseudoCriticalEdges(int n, int[][] edges) {
        List<Edge> edgeList = new ArrayList<>();
        for (int i = 0; i < edges.length; i++) {
            int[] e = edges[i];
            edgeList.add(new Edge(i, e[0], e[1], e[2]));
        }
        edgeList.sort((a, b)->a.v-b.v);
        int mst = calcMST(n, edgeList, -1, -1); // 真正的最小生成樹
        List<List<Integer>> res = new ArrayList<>();
        res.add(new ArrayList<>());
        res.add(new ArrayList<>());
        for (int i = 0; i < edges.length; i++) {
            int t1 = calcMST(n, edgeList, i, -1); // 不使用 i 構造最小生成樹
            int t2 = calcMST(n, edgeList, -1, i); // 必使用 i 構造最小生成樹
            if (t1 != mst) { // 不使用, MST變大, 關鍵邊
                res.get(0).add(i);
            } else if (t2 == mst) { // 使用, MST不變, 且不是關鍵邊, 則是偽關鍵邊
                res.get(1).add(i);
            }
        }
        return res;
    }

    private int calcMST(int n, List<Edge> edgeList, int forbidden, int must) {
        // 不使用 forbidden, 而必須使用 must 構造最小生成樹
        UnionFind uf = new UnionFind(n);
        int res = 0;
        int cnt = 0;
        if (must >= 0) {
            cnt++;
            for (var e : edgeList) {
                if (e.id == must) {
                    res += e.v;
                    uf.merge(e.a, e.b);
                    break;
                }
            }
        }
        for (var e : edgeList) {
            if (e.id == forbidden || e.id == must) {
                continue;
            }
            int fa = uf.find(e.a);
            int fb = uf.find(e.b);
            if (fa != fb) {
                uf.merge(fa, fb);
                cnt++;
                res += e.v;

            }
            if (cnt == n - 1) return res;
        }
        return 0x3f3f3f3f;
    }
}

相關焦點

  • Leetcode weekly contest三月小結
    Maximum Performance of a Teamhttps://leetcode.com/problems/maximum-performance-of-a-team/有編號為1至N的N個工程師,每個工程師的工作速度和工作效率分別為speed[i]和efficiency[i],要求選擇其中最多K個工程師所能達到的最大performance。
  • LeetCode Weekly Contest 224 題解
    Tuple with Same Product題目連結:https://leetcode.com/problems/tuple-with-same-product/題解連結:https://github.com/yular/CC--InterviewProblem/blob/master/LeetCode/leetcode_tuple-with-same-product.cpp
  • LeetCode Weekly Contest 204 題解
    Detect Pattern of Length M Repeated K or More Times題目連結:https://leetcode.com/problems/detect-pattern-of-length-m-repeated-k-or-more-times/題解連結:https://github.com/yular/CC--InterviewProblem
  • LeetCode Weekly Contest 196
    Solution {public: bool canMakeArithmeticProgression(vector<int>& arr) { sort(arr.begin(), arr.end()); int n = arr.size(); int delta = arr[1] - arr[0]; for (int i
  • LeetCode中文解題思路重磅問世!
    資源內容第一個部分是 leetcode 經典題目的解析,包括思路,關鍵點和具體的代碼實現。第二部分是對於數據結構與算法的總結第三部分是 anki 卡片, 將 leetcode 題目按照一定的方式記錄在 anki 中,方便大家記憶。
  • 用這種解題方法,我解決了大部分的 leetcode 貪心算法題
    「這是本人第40篇原創博文,每周兩篇更新,謝謝賞臉閱讀文章今天介紹一種解決常規的貪心策略或者字典排序的題目的通用解題方法。第一題,leetcode中等難度題目先來一道簡單的字典序排列的問題,這個題目我這裡不會用最優解來解決這個問題,這個是leetcode的中等難度的題目,最優解還是需要再思考一下的,這道題目作為文章開頭只是為了介紹我想要介紹的貪心的解題的一種思路而已,大佬請勿噴!!
  • 最長公共前綴 | Leetcode題解
    ans;    }}C++ 實現// 作者:LeetCode-Solution// 連結:https://leetcode-cn.com/problems/longest-common-prefix/solution/zui-chang-gong-gong-qian-zhui-by-leetcode-solution
  • [LeetCode] 916. Word Subsets 單詞子集合
    ","apple","facebook","google","leetcode"], B = ["l","e"]Output: ["apple","google","leetcode"]Example 3:Input: A = ["amazon","apple","facebook","google","leetcode"], B =
  • LeetCode刷題第三周【數組(簡單)】
    1.1 解題思路我們通過示例發現,數組的下標 i 與 arr[i] 的差值 distance 就是缺失的正整數個數。因此我們可以通過判斷 distance 和 k 的大小,來知道我們要找的數所在的區間。我們同樣也可以通過 distance 計算出,我們需要的值。
  • [LeetCode] 1143. Longest Common Subsequence 最長公共子序列
    LeetCode 之前也有題目需要藉助求 LCS 來解題,比如 Delete Operation for Two Strings,當時博主還疑惑怎麼 LeetCode 中沒有專門求 LCS 的題呢,這不,終於補上了。
  • ​LeetCode刷題實戰139:單詞拆分
    示例 1:輸入: s = "leetcode", wordDict = ["leet", "code"]輸出: true解釋: 返回 true 因為 "leetcode示例 3:輸入: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]輸出: false解題
  • LeetCode
    一維數組的動態和連結:https://leetcode-cn.com/problems/running-sum-of-1d-array/給你一個數組 nums 。擁有最多糖果的孩子連結:https://leetcode-cn.com/problems/kids-with-the-greatest-number-of-candies/給你一個數組 candies 和一個整數 extraCandies ,其中 candies[i] 代表第 i 個孩子擁有的糖果數目對每一個孩子,檢查是否存在一種方案
  • leetcode刷題指南之RussianDollEnvelopes
    解題思路考慮如果只有一個維度,比方說w的話,那麼我們直接排序,然後掃一遍,看有多少不同的數就可以了,這樣顯然是最優的。;pair<int, int>>& envelopes) 5{ 6    sort(envelopes.begin(),envelopes.end()); 7    int n=envelopes.size(); 8    if(n==0)    return 0; 9    int dp[n];10    for(int i=
  • 笨方法刷 leetcode(一)
    )本篇記錄5道題的解題思路,可能都是最笨的方法題目描述:實現一個算法,確定一個字符串 s 的所有字符是否全都不同示例 1:輸入: s = "leetcode"輸出: falsein range(0, len(nums)): for j in range(i + 1, len(nums)): if nums[i] + nums[j] == target: return i, j else: continue
  • AK leetcode 流浪計劃 - 數組反轉
    示例:輸入:"Let's take LeetCode contest"輸出:"s'teL ekat edoCteeL tsetnoc"提示:在字符串中,每個單詞由單個空格分隔下面我精心準備了leetcode幾個題目(首先AK F.*ing leetcode),給大家準備了一些題目,供大家練習參考。幹他F.*ing (Fighting?)。八、實戰訓練代碼基礎訓練題光說不練假把式,學完了怎麼也要實操一下吧,快快動用把剛才那4題A了。
  • 數字問題-LeetCode 524、525、526、528、530、537、539、540
    示例 1:輸入:s = "abpcplea", d = ["ale","apple","monkey","plea"]輸出: "apple"解題思路:首先找出符合子序列的字符串,然後根據字典序以及長度進行排序。
  • 整數反轉 | Leetcode題解
    由於字符串轉換的效率較低且使用較多庫函數,所以解題方案不考慮該方法,而是通過數學計算來解決。通過循環將數字x的每一位拆開,在計算新值時每一步都判斷是否溢出。溢出條件有兩個,一個是大於整數最大值MAX_VALUE,另一個是小於整數最小值MIN_VALUE,設當前計算結果為ans,下一位為pop。
  • 最全leetcode解題攻略:思路知識點代碼都有,搞定AI大廠筆試
    GitHub上有個叫lucifer的中國小哥哥,將Leetcode題庫中數百道題目的解題過程全盤分享,解題思路和代碼都有。民間曾一度流傳,leetcode上,基本就是網際網路大廠拿來應聘面試者的考題了。來看看。
  • AK leetcode 流浪計劃 - 回文串
    二、解題步驟解題步驟三、作用異型回文子串問題(比如對原串的字符進行相應的變化,對回文規則進行改變或加限制,將數字作為字符串)四、經典算法介紹單次查詢可以使用對撞指針法進行判斷。多次查詢的方法有雙指針動態規劃,中心擴展法,Manacher(馬拉車)算法。
  • 最長回文子串 | Leetcode題解
    i,j - i + 1);            }        }    }    return ans;}/***  @作者:liuxukang*  @連結:https://leetcode-cn.com/problems/longest-palindromic-substring