{"id":6860,"date":"2020-05-31T10:41:30","date_gmt":"2020-05-31T17:41:30","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6860"},"modified":"2020-05-31T13:53:53","modified_gmt":"2020-05-31T20:53:53","slug":"leetcode-1466-reorder-routes-to-make-all-paths-lead-to-the-city-zero","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-1466-reorder-routes-to-make-all-paths-lead-to-the-city-zero\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1466. Reorder Routes to Make All Paths Lead to the City Zero"},"content":{"rendered":"\n<p>There are&nbsp;<code>n<\/code>&nbsp;cities numbered from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>n-1<\/code>&nbsp;and&nbsp;<code>n-1<\/code>&nbsp;roads such that&nbsp;there is only one way to travel between two&nbsp;different cities (this network form a tree).&nbsp;Last year,&nbsp;The ministry of transport&nbsp;decided to orient the roads in one direction because they are too narrow.<\/p>\n\n\n\n<p>Roads are represented by&nbsp;<code>connections<\/code>&nbsp;where&nbsp;<code>connections[i] = [a, b]<\/code>&nbsp;represents a&nbsp;road&nbsp;from city&nbsp;<code>a<\/code>&nbsp;to&nbsp;<code>b<\/code>.<\/p>\n\n\n\n<p>This year, there will be a big event in the capital (city 0), and many people want to travel to this city.<\/p>\n\n\n\n<p>Your task consists of reorienting&nbsp;some roads such that each city can visit the city&nbsp;0. Return the&nbsp;<strong>minimum<\/strong>&nbsp;number of edges changed.<\/p>\n\n\n\n<p>It&#8217;s&nbsp;<strong>guaranteed<\/strong>&nbsp;that each city can reach the city 0 after reorder.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/05\/13\/sample_1_1819.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]\n<strong>Output:<\/strong> 3\n<strong>Explanation: <\/strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/05\/13\/sample_2_1819.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]\n<strong>Output:<\/strong> 2\n<strong>Explanation: <\/strong>Change the direction of edges show in red such that each node can reach the node 0 (capital).<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 3, connections = [[1,0],[2,0]]\n<strong>Output:<\/strong> 0\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= n &lt;= 5 * 10^4<\/code><\/li><li><code>connections.length == n-1<\/code><\/li><li><code>connections[i].length == 2<\/code><\/li><li><code>0 &lt;= connections[i][0], connections[i][1] &lt;= n-1<\/code><\/li><li><code>connections[i][0] != connections[i][1]<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: BFS<\/strong><\/h2>\n\n\n\n<p>Augment the graph<br>g[u][v] = 1, g[v][u] = 0,  u->v is an edge in the original graph.<\/p>\n\n\n\n<p>BFS from 0, sum up all the edge costs to visit all the nodes.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int minReorder(int n, vector<vector<int>>& connections) {\n    vector<vector<pair<int, int>>> g(n); \/\/ u -> {v, cost}\n    for (const auto& e : connections) {\n      g[e[0]].emplace_back(e[1], 1); \/\/ u -> v, needs flip\n      g[e[1]].emplace_back(e[0], 0); \/\/ v -> u, no cost\n    }\n    int ans = 0;\n    queue<int> q{{0}};\n    vector<int> seen(n);\n    while (!q.empty()) {\n      int cur = q.front(); q.pop();\n      seen[cur] = 1;\n      for (const auto& [nxt, cost] : g[cur]) {\n        if (seen[nxt]) continue;\n        ans += cost;\n        q.push(nxt);\n      }\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are&nbsp;n&nbsp;cities numbered from&nbsp;0&nbsp;to&nbsp;n-1&nbsp;and&nbsp;n-1&nbsp;roads such that&nbsp;there is only one way to travel between two&nbsp;different cities (this network form a tree).&nbsp;Last year,&nbsp;The ministry of transport&nbsp;decided to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[34,77,177],"class_list":["post-6860","post","type-post","status-publish","format-standard","hentry","category-graph","tag-bfs","tag-graph","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6860","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6860"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6860\/revisions"}],"predecessor-version":[{"id":6865,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6860\/revisions\/6865"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}