• <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
            久久久久无码精品国产app| 久久国产精品国语对白| 久久亚洲天堂| 久久91精品国产91久久户| 国产精品久久久久久福利漫画| 亚洲国产精品无码久久98| 久久AV高潮AV无码AV| 久久久久久久久久久久中文字幕 | 亚洲国产精品无码久久青草| 国内精品伊人久久久久网站| 国内精品久久久久影院亚洲| 精品多毛少妇人妻AV免费久久| 久久精品9988| 中文字幕无码久久精品青草| 久久只有这精品99| 国内精品久久国产| 无码国内精品久久人妻蜜桃| 久久久久99精品成人片直播| 色综合色天天久久婷婷基地| 久久天天婷婷五月俺也去| 精品人妻伦九区久久AAA片69 | 亚洲国产精品无码成人片久久| 狼狼综合久久久久综合网| 久久久这里只有精品加勒比| 亚洲va国产va天堂va久久| 亚洲综合久久综合激情久久| 国产69精品久久久久9999| 久久婷婷是五月综合色狠狠| 久久精品国产亚洲沈樵| 中文精品99久久国产| 色综合久久最新中文字幕| 久久精品草草草| 久久大香香蕉国产| 综合久久给合久久狠狠狠97色| 久久天天躁狠狠躁夜夜av浪潮| 久久国产劲爆AV内射—百度| 香蕉99久久国产综合精品宅男自 | 久久国产乱子伦免费精品| 日韩久久无码免费毛片软件| 国产—久久香蕉国产线看观看| 综合网日日天干夜夜久久|