• <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
            給出一個無向圖(給定節點數、所有的邊),問是否存在連接起點到終點的路,簡單DFS,用set記錄訪問過的節點(改為記錄訪問過的邊會TLE)

            寫法一,DFS完判定終點是否到達過

             1 #1971
             2 #Runtime: 3120 ms (Beats 67.40%)
             3 #Memory: 348.8 MB (Beats 5.11%)
             4 
             5 class Solution(object):
             6     def validPath(self, n, edges, source, destination):
             7         """
             8         :type n: int
             9         :type edges: List[List[int]]
            10         :type source: int
            11         :type destination: int
            12         :rtype: bool
            13         """
            14         graph_dict = defaultdict(set)
            15         vis = set()
            16         for x, y in edges:
            17             graph_dict[x].add(y)
            18             graph_dict[y].add(x)
            19 
            20         def DFS(t, des):
            21             vis.add(t)
            22             if t == des:
            23                 return
            24             if t in graph_dict:
            25                 for j in graph_dict[t]:
            26                     if j not in vis:
            27                         DFS(j, des)
            28         DFS(source, destination)
            29         return destination in vis

            寫法二,DFS過程中直接判False或者True,不知為何此種寫法慢一些

             1 #1971
             2 #Runtime: 4947 ms (Beats 17.28%)
             3 #Memory: 353 MB (Beats 5.11%)
             4 
             5 class Solution(object):
             6     def validPath(self, n, edges, source, destination):
             7         """
             8         :type n: int
             9         :type edges: List[List[int]]
            10         :type source: int
            11         :type destination: int
            12         :rtype: bool
            13         """
            14         graph_dict = defaultdict(set)
            15         vis = set()
            16         for x, y in edges:
            17             graph_dict[x].add(y)
            18             graph_dict[y].add(x)
            19 
            20         def DFS(t, des):
            21             vis.add(t)
            22             if t == des:
            23                 return True
            24             if t in graph_dict:
            25                 for j in graph_dict[t]:
            26                     if j not in vis and DFS(j, des):
            27                         return True
            28             return False
            29         return DFS(source, destination)
            30         



            日本久久久久亚洲中字幕| 久久国产精品成人免费 | 一本色道久久99一综合| 久久精品国产清高在天天线| 国内精品久久久久久久coent| 日韩va亚洲va欧美va久久| 久久精品中文无码资源站| 亚洲国产精品综合久久一线| 久久久久久久综合日本亚洲| 久久久久久久波多野结衣高潮| 香港aa三级久久三级| 亚洲va久久久噜噜噜久久狠狠| 精品久久久久中文字幕一区| 日产精品久久久久久久| 色婷婷狠狠久久综合五月| 国产精品无码久久四虎| 久久精品嫩草影院| 色88久久久久高潮综合影院| 狠狠色丁香婷婷久久综合| 激情综合色综合久久综合| 久久精品国产亚洲欧美| 久久人人爽人人爽人人片av高请 | 久久Av无码精品人妻系列| 久久久久久久久66精品片| 国内精品久久久久久久coent| 久久精品国产半推半就| 久久精品aⅴ无码中文字字幕重口| 精品久久久无码21p发布| 无夜精品久久久久久| 日本久久久久久久久久| 久久综合伊人77777麻豆| 思思久久99热免费精品6| 亚洲国产精品久久久久婷婷软件| 97久久精品无码一区二区| 99re久久精品国产首页2020| 国产成人久久精品区一区二区| 99re久久精品国产首页2020| 久久精品国产一区二区三区日韩| aaa级精品久久久国产片| 久久九九全国免费| 久久青青草原精品国产软件|