• <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
            給出一些id pairs,[a, b]表示a不想與b分在一組,一共n個id,問是否可以分成兩個group。
            并查集,對于每個id i,預處理所有不能和他一組的ids,這些ids將會被分在一組(并查集union操作),如果發現i和這些id在并查集的同一組,則輸出False


             1 #886
             2 #Runtime: 2223 ms (Beats 5.11%)
             3 #Memory: 19.1 MB (Beats 72.26%)
             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 self.parent[x] != 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 
            18 class Solution(object):
            19     def possibleBipartition(self, n, dislikes):
            20         """
            21         :type n: int
            22         :type dislikes: List[List[int]]
            23         :rtype: bool
            24         """
            25         if n == 1:
            26             return True
            27         graph_dict = {}
            28         for d in dislikes:
            29             if d[0] not in graph_dict:
            30                 graph_dict[d[0]] = [d[1]]
            31             else:
            32                 graph_dict[d[0]].append(d[1])
            33             if d[1] not in graph_dict:
            34                 graph_dict[d[1]] = [d[0]]
            35             else:
            36                 graph_dict[d[1]].append(d[0])
            37         uf_set = UnionFind(n)
            38         for x in graph_dict.keys():
            39             for i in range(len(graph_dict[x]) - 1):
            40                 uf_set.union(graph_dict[x][i], graph_dict[x][i + 1])
            41             if uf_set.find(x) == uf_set.find(graph_dict[x][0]):
            42                 return False
            43         return True
            久久国产视频99电影| 一本色道久久综合狠狠躁篇| 国产精品综合久久第一页 | 久久久久久久久久久| 久久久久亚洲AV片无码下载蜜桃| 久久婷婷五月综合国产尤物app| 国产精品久久久久天天影视| 久久久久国色AV免费观看| 国色天香久久久久久久小说| 久久久久久综合一区中文字幕 | 久久发布国产伦子伦精品 | 久久亚洲电影| 国内精品久久久久影院优| 久久久久无码精品| 久久婷婷五月综合色高清| 久久综合一区二区无码| 精品久久久久中文字幕日本| 亚洲国产成人久久笫一页| 狠狠色丁香婷婷综合久久来| 伊人久久精品影院| 国产亚洲婷婷香蕉久久精品| 一本色道久久88综合日韩精品| 久久免费视频观看| 久久国产乱子伦免费精品| 亚洲欧洲精品成人久久奇米网| 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲 | 久久99热国产这有精品| 中文字幕无码精品亚洲资源网久久| 91亚洲国产成人久久精品| 久久久久人妻一区二区三区vr | 一级做a爰片久久毛片看看 | 久久婷婷是五月综合色狠狠| 久久久久国产精品| 2020久久精品国产免费| 国产69精品久久久久久人妻精品| 一本久道久久综合狠狠躁AV| 亚洲午夜久久久| 日本五月天婷久久网站| 国产69精品久久久久APP下载| 香蕉99久久国产综合精品宅男自 | 欧洲成人午夜精品无码区久久|