• <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
            給出一棵樹(shù)中每個(gè)節(jié)點(diǎn)的值vals,以及各條邊edges,問(wèn)樹(shù)中有多少條path,滿足1.首尾節(jié)點(diǎn)vals相同;2.path中其他節(jié)點(diǎn)的值均小于首尾節(jié)點(diǎn)
            參考了Discussion:https://leetcode.com/problems/number-of-good-paths/solutions/3053513/python3-union-find-explained/

             1 #2421
             2 #Runtime: 2295 ms (Beats 93.75%)
             3 #Memory: 39 MB (Beats 81.25%)
             4 
             5 class UnionFind:
             6     def __init__(self, n):
             7         self.parent = [i for i in range(n + 1)]
             8     def find(self, x):
             9         i = x
            10         while x != self.parent[x]:
            11             x = self.parent[x]
            12         self.parent[i] = x
            13         return x
            14     def union(self, x, y):
            15         self.parent[self.find(x)] = self.find(y)
            16 
            17 class Solution(object):
            18     def numberOfGoodPaths(self, vals, edges):
            19         """
            20         :type vals: List[int]
            21         :type edges: List[List[int]]
            22         :rtype: int
            23         """
            24         uf = UnionFind(len(vals))
            25         node_val_set = defaultdict(set)
            26         node_dict = defaultdict(list)
            27         ans = len(vals)
            28         for x, y in edges:
            29             node_val_set[vals[x]].add(x)
            30             node_val_set[vals[y]].add(y)
            31             node_dict[x].append(y)
            32             node_dict[y].append(x)
            33         for i in sorted(node_val_set.keys()):
            34             for j in node_val_set[i]:
            35                 for k in node_dict[j]:
            36                     if vals[k] <= i:
            37                         uf.union(j, k)
            38             cnt = defaultdict(int)
            39             for j in node_val_set[i]:
            40                 cnt[uf.find(j)] += 1
            41             for r in cnt.keys():
            42                 ans += (cnt[r] - 1) * cnt[r] // 2
            43         return ans
            久久夜色撩人精品国产| 亚洲欧美伊人久久综合一区二区| 亚洲人AV永久一区二区三区久久 | 亚洲乱码精品久久久久.. | 国产成人无码精品久久久免费| 人妻久久久一区二区三区| 国内精品伊人久久久影院| 欧美午夜精品久久久久久浪潮| 国产精品无码久久四虎| 久久精品国产亚洲欧美| 免费国产99久久久香蕉| 99久久精品无码一区二区毛片 | 久久天天躁狠狠躁夜夜2020| 国产激情久久久久影院小草 | 久久亚洲精品无码VA大香大香| 国产精品VIDEOSSEX久久发布| 99久久99久久精品国产片| 99久久国产热无码精品免费久久久久| 久久天天躁狠狠躁夜夜avapp| 久久精品亚洲日本波多野结衣 | 亚洲va久久久噜噜噜久久| 久久天天躁狠狠躁夜夜网站 | 久久久亚洲欧洲日产国码二区 | 国产午夜精品久久久久免费视| 国产综合久久久久| 久久久91人妻无码精品蜜桃HD| 久久播电影网| 久久久久久久精品成人热色戒| 久久人人爽人人爽人人AV东京热| 99999久久久久久亚洲| 久久精品中文字幕有码| 亚洲精品高清国产一线久久| 国产精品久久久久无码av| 青草久久久国产线免观| 狠狠88综合久久久久综合网| 国产成人精品久久亚洲高清不卡 | 亚洲国产成人久久一区久久| 久久久久久久久久久久久久| 国产精品激情综合久久| 亚洲国产精品成人久久| 久久精品国产精品亚洲下载|