• <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
            給出一棵樹中每個節點的值vals,以及各條邊edges,問樹中有多少條path,滿足1.首尾節點vals相同;2.path中其他節點的值均小于首尾節點
            參考了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
            色综合合久久天天给综看| 国产成人精品三上悠亚久久| 久久久久99精品成人片欧美| 国产一区二区精品久久| 久久精品国产亚洲av瑜伽| 久久天天婷婷五月俺也去| 国内精品久久九九国产精品| 久久影院亚洲一区| MM131亚洲国产美女久久| 久久AⅤ人妻少妇嫩草影院| 久久精品国产亚洲αv忘忧草| 国产精品久久久久久搜索| 欧美激情一区二区久久久| 久久se精品一区二区影院| 久久亚洲AV成人无码电影| 亚洲а∨天堂久久精品| 狠狠色丁香婷婷久久综合不卡| 久久人人爽人人爽人人av东京热| 久久国产精品久久| 欧美牲交A欧牲交aⅴ久久| 香蕉久久永久视频| 久久精品国产精品亚洲艾草网美妙| 久久亚洲欧美国产精品| 久久99久国产麻精品66| 日韩十八禁一区二区久久 | 国产精品亚洲美女久久久| 婷婷久久香蕉五月综合加勒比| 久久精品成人| 久久久久国产精品麻豆AR影院| 国内精品久久国产大陆| 久久精品蜜芽亚洲国产AV| 人妻久久久一区二区三区| 久久精品国产男包| 伊人久久大香线蕉av不卡| 中文字幕久久精品| 久久精品成人欧美大片| 精品久久久无码人妻中文字幕| yy6080久久| 午夜欧美精品久久久久久久| 久久亚洲日韩精品一区二区三区| 久久成人国产精品免费软件|