<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>O(V+E) Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/ove/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/ove/</link>
	<description></description>
	<lastBuildDate>Thu, 30 Jan 2020 03:52:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>O(V+E) Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/ove/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1319. Number of Operations to Make Network Connected</title>
		<link>https://zxi.mytechroad.com/blog/graph/leetcode-1319-number-of-operations-to-make-network-connected/</link>
					<comments>https://zxi.mytechroad.com/blog/graph/leetcode-1319-number-of-operations-to-make-network-connected/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 12 Jan 2020 06:15:27 +0000</pubDate>
				<category><![CDATA[Graph]]></category>
		<category><![CDATA[DFS]]></category>
		<category><![CDATA[graph]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[O(V+E)]]></category>
		<category><![CDATA[union find]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6077</guid>

					<description><![CDATA[<p>There are&#160;n&#160;computers numbered from&#160;0&#160;to&#160;n-1&#160;connected by&#160;ethernet cables&#160;connections&#160;forming a network where&#160;connections[i] = [a, b]&#160;represents a connection between computers&#160;a&#160;and&#160;b. Any computer&#160;can reach any other computer directly or indirectly&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-1319-number-of-operations-to-make-network-connected/">花花酱 LeetCode 1319. Number of Operations to Make Network Connected</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 1319. Number of Operations to Make Network Connected - 刷题找工作 EP297" width="500" height="281" src="https://www.youtube.com/embed/8d8vfU3LYHM?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>There are&nbsp;<code>n</code>&nbsp;computers numbered from&nbsp;<code>0</code>&nbsp;to&nbsp;<code>n-1</code>&nbsp;connected by&nbsp;ethernet cables&nbsp;<code>connections</code>&nbsp;forming a network where&nbsp;<code>connections[i] = [a, b]</code>&nbsp;represents a connection between computers&nbsp;<code>a</code>&nbsp;and&nbsp;<code>b</code>. Any computer&nbsp;can reach any other computer directly or indirectly through the network.</p>



<p>Given an initial computer network&nbsp;<code>connections</code>. You can extract certain cables between two directly connected computers, and place them between any pair of disconnected computers to make them directly connected. Return the&nbsp;<em>minimum number of times</em>&nbsp;you need to do this in order to make all the computers connected. If it&#8217;s not possible, return -1.&nbsp;</p>



<p><strong>Example 1:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/01/02/sample_1_1677.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 4, connections = [[0,1],[0,2],[1,2]]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Remove cable between computer 1 and 2 and place between computers 1 and 3.
</pre>



<p><strong>Example 2:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/01/02/sample_2_1677.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 6, connections = [[0,1],[0,2],[0,3],[1,2],[1,3]]
<strong>Output:</strong> 2
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 6, connections = [[0,1],[0,2],[0,3],[1,2]]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There are not enough cables.
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 5, connections = [[0,1],[0,2],[3,4],[2,3]]
<strong>Output:</strong> 0
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= n &lt;= 10^5</code></li><li><code>1 &lt;= connections.length &lt;= min(n*(n-1)/2, 10^5)</code></li><li><code>connections[i].length == 2</code></li><li><code>0 &lt;= connections[i][0], connections[i][1]&nbsp;&lt; n</code></li><li><code>connections[i][0] != connections[i][1]</code></li><li>There are no repeated connections.</li><li>No two computers are connected by more than one cable.</li></ul>



<h2><strong>Solution 1: Union-Find</strong></h2>



<p>Time complexity: O(V+E)<br>Space complexity: O(V)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int makeConnected(int n, vector&lt;vector&lt;int&gt;&gt;&amp; connections) {
    if (connections.size() &lt; n - 1) return -1;
    vector&lt;int&gt; p(n);
    iota(begin(p), end(p), 0);
    
    function&lt;int(int)&gt; find = [&amp;](int x) {
      return p[x] == x ? x : p[x] = find(p[x]);
    };
    
    for (const auto&amp; c : connections)
      p[find(c[0])] = find(c[1]);    
    
    unordered_set&lt;int&gt; s;
    for (int i = 0; i &lt; n; ++i)
      s.insert(find(i));
    
    return s.size() - 1;        
  }
};</pre>
</div></div>



<h2><strong>Solution 2: DFS</strong></h2>



<p>Time complexity: O(V+E)<br>Space complexity: O(V+E)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int makeConnected(int n, vector&lt;vector&lt;int&gt;&gt;&amp; connections) {
    if (connections.size() &lt; n - 1) return -1;
    vector&lt;vector&lt;int&gt;&gt; g(n);
    for (const auto&amp; c : connections) {
      g[c[0]].push_back(c[1]);
      g[c[1]].push_back(c[0]);
    }
    vector&lt;int&gt; seen(n);
    int count = 0;
    function&lt;void(int)&gt; dfs = [&amp;](int cur) {
      for (int nxt : g[cur])
        if (!seen[nxt]++) dfs(nxt);      
    };
    for (int i = 0; i &lt; n; ++i)
      if (!seen[i]++ &amp;&amp; ++count)
        dfs(i);        
    return count - 1;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-1319-number-of-operations-to-make-network-connected/">花花酱 LeetCode 1319. Number of Operations to Make Network Connected</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/graph/leetcode-1319-number-of-operations-to-make-network-connected/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1192. Critical Connections in a Network</title>
		<link>https://zxi.mytechroad.com/blog/graph/leetcode-1192-critical-connections-in-a-network/</link>
					<comments>https://zxi.mytechroad.com/blog/graph/leetcode-1192-critical-connections-in-a-network/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 15 Sep 2019 23:07:27 +0000</pubDate>
				<category><![CDATA[Graph]]></category>
		<category><![CDATA[graph]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[O(V+E)]]></category>
		<category><![CDATA[tarjan]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5556</guid>

					<description><![CDATA[<p>There are&#160;n&#160;servers numbered from&#160;0&#160;to&#160;n-1&#160;connected by&#160;undirected server-to-server&#160;connections&#160;forming a network where&#160;connections[i] = [a, b]&#160;represents a connection between servers&#160;a&#160;and&#160;b. Any server can reach any other server directly or&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-1192-critical-connections-in-a-network/">花花酱 LeetCode 1192. Critical Connections in a Network</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>There are&nbsp;<code>n</code>&nbsp;servers numbered from&nbsp;<code>0</code>&nbsp;to&nbsp;<code>n-1</code>&nbsp;connected by&nbsp;undirected server-to-server&nbsp;<code>connections</code>&nbsp;forming a network where&nbsp;<code>connections[i] = [a, b]</code>&nbsp;represents a connection between servers&nbsp;<code>a</code>&nbsp;and&nbsp;<code>b</code>. Any server can reach any other server directly or indirectly through the network.</p>



<p>A&nbsp;<em>critical connection</em>&nbsp;is a connection that, if removed, will make some server unable to reach some other server.</p>



<p>Return all critical connections in the network in any order.</p>



<p><strong>Example 1:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2019/09/03/1537_ex1_2.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 4, connections = [[0,1],[1,2],[2,0],[1,3]]
<strong>Output:</strong> [[1,3]]
<strong>Explanation:</strong> [[3,1]] is also accepted.
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= n &lt;= 10^5</code></li><li><code>n-1 &lt;= connections.length &lt;= 10^5</code></li><li><code>connections[i][0] != connections[i][1]</code></li><li>There are no repeated connections.</li></ul>



<h2><strong>Solution: Tarjan</strong></h2>



<p>Time complexity: O(v+e)<br>Space complexity: O(v+e)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;vector&lt;int&gt;&gt; criticalConnections(int n, vector&lt;vector&lt;int&gt;&gt;&amp; connections) {
    vector&lt;vector&lt;int&gt;&gt; g(n);
    vector&lt;int&gt; ts(n, INT_MAX);
    vector&lt;vector&lt;int&gt;&gt; ans;
    int t = 0;
    
    function&lt;int(int, int)&gt; tarjan = [&amp;](int i, int p) {
      int min_i = ts[i] = ++t;
      for (int j : g[i]) {
        if (ts[j] == INT_MAX) {
          int min_j = tarjan(j, i);
          min_i = min(min_i, min_j);
          if (ts[i] &lt; min_j)
            ans.push_back({i, j});
        } else if (j != p) {
          min_i = min(min_i, ts[j]);
        }
      }
      return min_i;
    };
    
    for (const auto&amp; e : connections) {
      g[e[0]].push_back(e[1]);
      g[e[1]].push_back(e[0]);
    }
    
    tarjan(0, -1);
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-1192-critical-connections-in-a-network/">花花酱 LeetCode 1192. Critical Connections in a Network</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/graph/leetcode-1192-critical-connections-in-a-network/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
