• <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热狠狠色精品一区| 国产成人久久激情91| 久久婷婷人人澡人人| 亚洲精品无码久久千人斩| 婷婷久久综合九色综合98| 青青久久精品国产免费看| 国产成人无码久久久精品一| 精品久久综合1区2区3区激情| 亚洲国产欧洲综合997久久| 99久久亚洲综合精品成人| 99久久国产精品免费一区二区| 亚洲国产成人久久综合一| 久久AV高潮AV无码AV| 欧美午夜A∨大片久久 | 97久久精品人人做人人爽| 中文无码久久精品| 午夜精品久久久久9999高清| 国产精品岛国久久久久| 亚洲国产精品久久久天堂| 亚洲国产精品成人久久蜜臀 | 国内精品人妻无码久久久影院导航| 久久午夜电影网| 久久久久久综合一区中文字幕 | 国产无套内射久久久国产| 国产精品久久亚洲不卡动漫| 狠狠色婷婷久久综合频道日韩| 久久人人爽人人精品视频| 一本一道久久精品综合 | 久久久久国产一级毛片高清板| 成人久久久观看免费毛片| 99久久成人国产精品免费| 久久久久人妻一区精品性色av| 亚洲综合精品香蕉久久网| 国内精品人妻无码久久久影院导航 | 久久精品视频91| 久久综合伊人77777麻豆| 久久露脸国产精品| 久久久99精品成人片中文字幕| 久久久久久亚洲精品不卡|