• <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热| 99久久人人爽亚洲精品美女| 亚洲AⅤ优女AV综合久久久| 亚洲欧美成人久久综合中文网 | 久久天天躁狠狠躁夜夜avapp| 久久精品人人槡人妻人人玩AV| Xx性欧美肥妇精品久久久久久| 久久久久亚洲av综合波多野结衣 | 久久久青草久久久青草| 合区精品久久久中文字幕一区| 久久综合国产乱子伦精品免费| 久久久精品国产亚洲成人满18免费网站| 日本亚洲色大成网站WWW久久 | 91超碰碰碰碰久久久久久综合| 深夜久久AAAAA级毛片免费看| 久久精品国产亚洲网站| 久久久久久精品成人免费图片| 亚洲午夜久久影院| 亚洲色婷婷综合久久| 欧美粉嫩小泬久久久久久久 | 久久精品不卡| 91久久精品91久久性色| 久久天天躁狠狠躁夜夜不卡| 亚洲国产精久久久久久久| 久久精品99久久香蕉国产色戒| 热久久视久久精品18| 亚洲国产精品嫩草影院久久| 国内精品欧美久久精品| 99久久国产综合精品网成人影院| 久久久噜噜噜www成人网| 久久99热这里只有精品国产| 久久久国产亚洲精品| 色综合久久久久综合99| 久久久久免费视频| 久久久久无码中| 亚洲人成无码www久久久| 日韩久久久久中文字幕人妻 | 亚洲精品视频久久久| 婷婷久久五月天| 中文字幕无码精品亚洲资源网久久| 久久精品免费一区二区|