• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Uriel's Corner

            Research Associate @ Harvard University / Research Interests: Computer Vision, Biomedical Image Analysis, Machine Learning
            posts - 0, comments - 50, trackbacks - 0, articles - 594
            找出無向圖最小生成樹的關鍵邊和偽關鍵邊

            定義:
            An MST edge whose deletion from the graph would cause the MST weight to increase is called a critical edge. On the other hand, a pseudo-critical edge is that which can appear in some MSTs but not all.

            并查集求MST+枚舉邊,思路參考->https://leetcode.com/problems/find-critical-and-pseudo-critical-edges-in-minimum-spanning-tree/solutions/3929349/detailed-video-solution-java-c-python/


             1 #1489
             2 #Runtime: 2134 ms (Beats 50%)
             3 #Memory: 13.4 MB (Beats 100%)
             4 
             5 class UnionFind:
             6     def __init__(self, n):
             7         self.pa = list(range(n))
             8 
             9     
            10     def find(self, x):
            11         if self.pa[x] != x:
            12             self.pa[x] = self.find(self.pa[x])
            13         return self.pa[x]
            14 
            15 
            16     def union(self, x, y):
            17         px, py = self.find(x), self.find(y)
            18         self.pa[px] = py
            19 
            20 
            21 class Solution(object):
            22     def findCriticalAndPseudoCriticalEdges(self, n, edges):
            23         """
            24         :type n: int
            25         :type edges: List[List[int]]
            26         :rtype: List[List[int]]
            27         """
            28         def find_MST(block, e):
            29             uf = UnionFind(n)
            30             wt = 0
            31             if e != -1:
            32                 wt += edges[e][2]
            33                 uf.union(edges[e][0], edges[e][1])
            34             for i in range(len(edges)):
            35                 if i == block:
            36                     continue
            37                 if uf.find(edges[i][0]) == uf.find(edges[i][1]):
            38                     continue
            39                 uf.union(edges[i][0], edges[i][1])
            40                 wt += edges[i][2]
            41             for i in range(n):
            42                 if uf.find(i) != uf.find(0):
            43                     return float('inf')
            44             return wt
            45 
            46         c = []
            47         pc = []
            48         for i, e in enumerate(edges):
            49             e.append(i)
            50         edges.sort(key=lambda x: x[2])
            51         mst = find_MST(-1, -1)
            52         for i, e in enumerate(edges):
            53             if mst < find_MST(i, -1):
            54                 c.append(e[3])
            55             elif mst == find_MST(-1, i):
            56                 pc.append(e[3])
            57         return [c, pc]
            久久精品国产久精国产| 国产精品99久久不卡| 2020久久精品亚洲热综合一本| 亚洲伊人久久精品影院| 99国产欧美久久久精品蜜芽| 四虎国产精品免费久久久| 伊人久久大香线蕉成人| 一级做a爱片久久毛片| 亚洲午夜久久久影院| 久久国产成人| 久久久国产精品网站| 少妇内射兰兰久久| 伊人色综合九久久天天蜜桃| 久久久综合九色合综国产| 性做久久久久久久| 久久久久亚洲AV片无码下载蜜桃| 国产精品VIDEOSSEX久久发布| 国内精品久久久久影院一蜜桃| 久久久久久久综合狠狠综合| 26uuu久久五月天| 高清免费久久午夜精品| 久久超碰97人人做人人爱| 欧美精品乱码99久久蜜桃| 亚洲一区精品伊人久久伊人| 精品国产91久久久久久久a| 91精品日韩人妻无码久久不卡| 久久这里只有精品首页| 99久久精品国产高清一区二区 | 久久九九有精品国产23百花影院| 日本五月天婷久久网站| 性做久久久久久免费观看| 久久福利片| 久久婷婷色香五月综合激情| 亚洲色欲久久久久综合网| 久久只这里是精品66| 亚洲综合久久夜AV | 精品久久久久成人码免费动漫| 综合久久给合久久狠狠狠97色| 亚洲精品乱码久久久久久不卡| 欧美激情一区二区久久久| 亚洲精品无码成人片久久|