• <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
            中文成人久久久久影院免费观看| 亚洲一区二区三区日本久久九| 亚洲国产精品综合久久一线| yy6080久久| 久久99久久99小草精品免视看 | 伊人久久大香线蕉AV色婷婷色| 麻豆精品久久久久久久99蜜桃| 色欲综合久久躁天天躁蜜桃| 国产无套内射久久久国产| 一级女性全黄久久生活片免费| 精品人妻久久久久久888| 无码8090精品久久一区| 2021久久国自产拍精品| 精品国产乱码久久久久久人妻 | 一本久道久久综合狠狠躁AV| 国内精品人妻无码久久久影院| 久久影院久久香蕉国产线看观看| 国产69精品久久久久777| 久久综合色老色| 久久AAAA片一区二区| 久久国产高清字幕中文| 亚洲午夜久久久影院| 久久久久波多野结衣高潮| 久久99精品国产99久久6| 精品亚洲综合久久中文字幕| 久久婷婷人人澡人人爽人人爱| 久久久久综合中文字幕| 久久精品国产69国产精品亚洲| 亚洲国产精品久久电影欧美| 成人综合久久精品色婷婷| 亚洲AⅤ优女AV综合久久久| 久久精品国产一区二区三区不卡 | 色婷婷综合久久久中文字幕| 久久人人爽人爽人人爽av| 久久国产视频99电影| 久久国产福利免费| 久久国产影院| 无码任你躁久久久久久久| 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 中文国产成人精品久久不卡| 久久精品人人做人人爽电影|