• <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
            給出一個(gè)無(wú)向圖(給定節(jié)點(diǎn)數(shù)、所有的邊),問(wèn)是否存在連接起點(diǎn)到終點(diǎn)的路,簡(jiǎn)單DFS,用set記錄訪問(wèn)過(guò)的節(jié)點(diǎn)(改為記錄訪問(wèn)過(guò)的邊會(huì)TLE)

            寫(xiě)法一,DFS完判定終點(diǎn)是否到達(dá)過(guò)

             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

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

             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         



            人妻精品久久无码区 | 国产精品岛国久久久久| 久久精品人人做人人爽电影蜜月| 国产精品久久久福利| 久久天天躁狠狠躁夜夜2020老熟妇| 亚洲欧洲久久久精品| 久久99精品国产麻豆| 亚洲国产精品嫩草影院久久| 国产欧美久久一区二区| 97视频久久久| 久久996热精品xxxx| 久久福利青草精品资源站| 午夜精品久久久久久久久| 超级碰久久免费公开视频| 人妻久久久一区二区三区| 久久综合亚洲鲁鲁五月天| 久久久免费观成人影院 | 久久久久亚洲AV成人网| 亚洲国产精品成人久久| 无码人妻少妇久久中文字幕| 青青青青久久精品国产| 精品国产乱码久久久久久郑州公司 | 久久er国产精品免费观看8| 色欲久久久天天天综合网精品 | 久久精品夜夜夜夜夜久久| 香蕉久久永久视频| 久久精品国产精品亚洲人人| 精品久久香蕉国产线看观看亚洲| 久久九九久精品国产免费直播| 国产免费福利体检区久久| 亚洲国产成人久久综合一 | 久久中文字幕人妻熟av女| 国产精品成人久久久久三级午夜电影| 精品无码久久久久久午夜| 人妻精品久久无码区| 91精品国产乱码久久久久久 | 青青草原综合久久大伊人精品| 99久久婷婷免费国产综合精品| 99久久777色| 国内精品久久久久久久影视麻豆| 国产午夜福利精品久久|