• <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
            狠狠精品久久久无码中文字幕| 亚洲午夜精品久久久久久人妖| 亚洲国产成人久久综合区| 久久婷婷五月综合97色直播| 日产精品久久久久久久| 国产成人无码久久久精品一| 久久国产综合精品五月天| 亚洲国产精品无码久久一线| 大蕉久久伊人中文字幕| 久久久久亚洲AV成人网人人网站| 国产精品一区二区久久精品| 日本精品久久久久影院日本| 久久国产乱子伦免费精品| 亚州日韩精品专区久久久| 九九久久99综合一区二区| 久久精品国产久精国产果冻传媒 | 国内精品伊人久久久久av一坑| 久久精品免费观看| AV无码久久久久不卡蜜桃| 久久国产免费| 国产亚洲美女精品久久久| www久久久天天com| 国产三级久久久精品麻豆三级| 亚洲精品成人网久久久久久| 国产精品久久久久久久午夜片| 91精品国产91久久综合| 久久精品国产第一区二区三区| 狠狠色狠狠色综合久久| 一级A毛片免费观看久久精品| 久久男人中文字幕资源站| 国产高潮久久免费观看| 亚洲精品高清国产一久久| 久久精品国产亚洲综合色| 久久综合九色综合精品| 久久er国产精品免费观看2| 久久精品国产亚洲AV电影| 国内精品久久人妻互换| 久久久中文字幕| 精品国产综合区久久久久久| 国产成人久久精品二区三区| 精品国产综合区久久久久久|