{"id":33814,"date":"2024-11-01T09:20:43","date_gmt":"2024-11-01T09:20:43","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33814"},"modified":"2024-11-01T11:46:33","modified_gmt":"2024-11-01T11:46:33","slug":"python-coding-test-course-finding-cities-at-a-specific-distance","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33814\/","title":{"rendered":"Python Coding Test Course, Finding Cities at a Specific Distance"},"content":{"rendered":"<p><body><\/p>\n<article>\n<header>\n<p>Author: [Author Name]<\/p>\n<p>Date: [Date]<\/p>\n<\/header>\n<section>\n<h2>Problem Definition<\/h2>\n<p>\n                This is a problem of finding a list of cities that can be reached exactly at a specific distance K based on the given city and road information.<br \/>\n                Each city is represented by a number, and the distance information between two cities is provided in the form of bidirectional roads.<br \/>\n                The goal is to find cities that are K distance away.\n            <\/p>\n<p>\n                For example, given N cities and M roads, and a starting city, we need to output the cities that are K distance away.\n            <\/p>\n<\/section>\n<section>\n<h2>Input<\/h2>\n<ul>\n<li>N: Number of cities (1 \u2264 N \u2264 300,000)<\/li>\n<li>M: Number of roads (1 \u2264 M \u2264 1,000,000)<\/li>\n<li>K: Distance information (0 \u2264 K \u2264 300,000)<\/li>\n<li>X: Starting city number (1 \u2264 X \u2264 N)<\/li>\n<li>Road information: A, B (road from A to B)<\/li>\n<\/ul>\n<\/section>\n<section>\n<h2>Output<\/h2>\n<p>\n                Output the numbers of cities that are exactly K distance away in ascending order. If there are no such cities, output -1.\n            <\/p>\n<\/section>\n<section>\n<h2>Example<\/h2>\n<pre>\n                <code>\n                Input:\n                4 4 2 1\n                1 2\n                1 3\n                2 3\n                2 4\n\n                Output:\n                4\n                <\/code>\n                <\/pre>\n<p>Explanation: When starting from city 1, city 4 is the only city at distance 2.<\/p>\n<\/section>\n<section>\n<h2>Algorithm Approach<\/h2>\n<p>\n                This problem can be solved using graph search algorithms.<br \/>\n                We will construct a graph that represents cities as nodes and roads as edges.<br \/>\n                We will use the BFS (Breadth-First Search) algorithm to find cities that are K distance away from the starting city X.<br \/>\n                BFS is useful for finding the shortest path and works efficiently even in large graphs, making it suitable for this problem.\n            <\/p>\n<\/section>\n<section>\n<h2>Problem Solving Process<\/h2>\n<h3>1. Graph Creation<\/h3>\n<p>\n                First, we will create a graph from the road information.<br \/>\n                We will represent the graph using a list and use a dictionary where each city is a key and the connected cities are the values.\n            <\/p>\n<h3>2. Implementing BFS<\/h3>\n<p>\n                We will use a queue for implementing BFS.<br \/>\n                We add the starting city to the queue and keep track of visited cities.<br \/>\n                For each city, we explore the cities that are K distance away.\n            <\/p>\n<h3>3. Handling Output<\/h3>\n<p>\n                After the BFS search, we will either sort and output the city numbers that match the K distance or output -1 if there are no cities.\n            <\/p>\n<\/section>\n<section>\n<h2>Python Code<\/h2>\n<pre>\n                <code>\n                from collections import deque\n\n                def find_cities_with_distance(n, m, k, x, roads):\n                    # Create the graph\n                    graph = [[] for _ in range(n + 1)]\n                    for a, b in roads:\n                        graph[a].append(b)\n                    \n                    # Initialize BFS\n                    distance = [-1] * (n + 1)\n                    distance[x] = 0\n                    queue = deque([x])\n                    \n                    while queue:\n                        city = queue.popleft()\n                        for neighbor in graph[city]:\n                            if distance[neighbor] == -1:  # Unvisited city\n                                distance[neighbor] = distance[city] + 1\n                                queue.append(neighbor)\n                    \n                    # Generate results\n                    result = [i for i in range(1, n + 1) if distance[i] == k]\n                    return result if result else [-1]\n                \n                # Test case\n                n = 4\n                m = 4\n                k = 2\n                x = 1\n                roads = [(1, 2), (1, 3), (2, 3), (2, 4)]\n                print(find_cities_with_distance(n, m, k, x, roads))\n                <\/code>\n            <\/pre>\n<p>This code uses BFS to find the cities that can be reached at a specific distance K. The graph is represented using an adjacency list and the connectivity between nodes is defined based on the edge information.<\/p>\n<\/section>\n<section>\n<h2>Conclusion<\/h2>\n<p>\n                The &#8220;Finding Cities at a Specific Distance&#8221; problem can be efficiently solved using graph search algorithms, particularly BFS.<br \/>\n                Through this problem, we can enhance our understanding of the basic principles of BFS, how to construct graphs, and shortest path searches.<br \/>\n                This will serve as a stepping stone from basics to more complex graph problems.\n            <\/p>\n<\/section>\n<footer>\n<p>Thank you!<\/p>\n<\/footer>\n<\/article>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Author: [Author Name] Date: [Date] Problem Definition This is a problem of finding a list of cities that can be reached exactly at a specific distance K based on the given city and road information. Each city is represented by a number, and the distance information between two cities is provided in the form of &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33814\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Python Coding Test Course, Finding Cities at a Specific Distance&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[145],"tags":[],"class_list":["post-33814","post","type-post","status-publish","format-standard","hentry","category-python-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Coding Test Course, Finding Cities at a Specific Distance - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/33814\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Coding Test Course, Finding Cities at a Specific Distance - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Author: [Author Name] Date: [Date] Problem Definition This is a problem of finding a list of cities that can be reached exactly at a specific distance K based on the given city and road information. Each city is represented by a number, and the distance information between two cities is provided in the form of &hellip; \ub354 \ubcf4\uae30 &quot;Python Coding Test Course, Finding Cities at a Specific Distance&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33814\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:20:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:46:33+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/33814\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33814\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Python Coding Test Course, Finding Cities at a Specific Distance\",\"datePublished\":\"2024-11-01T09:20:43+00:00\",\"dateModified\":\"2024-11-01T11:46:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33814\/\"},\"wordCount\":434,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Python Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33814\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33814\/\",\"name\":\"Python Coding Test Course, Finding Cities at a Specific Distance - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:20:43+00:00\",\"dateModified\":\"2024-11-01T11:46:33+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33814\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33814\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33814\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Coding Test Course, Finding Cities at a Specific Distance\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Coding Test Course, Finding Cities at a Specific Distance - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/33814\/","og_locale":"ko_KR","og_type":"article","og_title":"Python Coding Test Course, Finding Cities at a Specific Distance - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Author: [Author Name] Date: [Date] Problem Definition This is a problem of finding a list of cities that can be reached exactly at a specific distance K based on the given city and road information. Each city is represented by a number, and the distance information between two cities is provided in the form of &hellip; \ub354 \ubcf4\uae30 \"Python Coding Test Course, Finding Cities at a Specific Distance\"","og_url":"https:\/\/atmokpo.com\/w\/33814\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:20:43+00:00","article_modified_time":"2024-11-01T11:46:33+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/33814\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33814\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Python Coding Test Course, Finding Cities at a Specific Distance","datePublished":"2024-11-01T09:20:43+00:00","dateModified":"2024-11-01T11:46:33+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33814\/"},"wordCount":434,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Python Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33814\/","url":"https:\/\/atmokpo.com\/w\/33814\/","name":"Python Coding Test Course, Finding Cities at a Specific Distance - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:20:43+00:00","dateModified":"2024-11-01T11:46:33+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33814\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33814\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33814\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Python Coding Test Course, Finding Cities at a Specific Distance"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33814","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=33814"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33814\/revisions"}],"predecessor-version":[{"id":33815,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33814\/revisions\/33815"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33814"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}