<?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>spanning tree Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/spanning-tree/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/spanning-tree/</link>
	<description></description>
	<lastBuildDate>Sun, 06 Sep 2020 19:01:07 +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>spanning tree Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/spanning-tree/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1579. Remove Max Number of Edges to Keep Graph Fully Traversable</title>
		<link>https://zxi.mytechroad.com/blog/graph/leetcode-1579-remove-max-number-of-edges-to-keep-graph-fully-traversable/</link>
					<comments>https://zxi.mytechroad.com/blog/graph/leetcode-1579-remove-max-number-of-edges-to-keep-graph-fully-traversable/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 06 Sep 2020 15:29:51 +0000</pubDate>
				<category><![CDATA[Graph]]></category>
		<category><![CDATA[graph]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[spanning tree]]></category>
		<category><![CDATA[union find]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7354</guid>

					<description><![CDATA[<p>Alice and Bob have an undirected graph of&#160;n&#160;nodes&#160;and 3 types of edges: Type 1: Can be traversed by Alice only. Type 2: Can be traversed&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-1579-remove-max-number-of-edges-to-keep-graph-fully-traversable/">花花酱 LeetCode 1579. Remove Max Number of Edges to Keep Graph Fully Traversable</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 1579. Remove Max Number of Edges to Keep Graph Fully Traversable - 刷题找工作 EP355" width="500" height="281" src="https://www.youtube.com/embed/eTQnRrmCWBc?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>Alice and Bob have an undirected graph of&nbsp;<code>n</code>&nbsp;nodes&nbsp;and 3 types of edges:</p>



<ul><li>Type 1: Can be traversed by Alice only.</li><li>Type 2: Can be traversed by Bob only.</li><li>Type 3: Can by traversed by both Alice and Bob.</li></ul>



<p>Given an array&nbsp;<code>edges</code>&nbsp;where&nbsp;<code>edges[i] = [type<sub>i</sub>, u<sub>i</sub>, v<sub>i</sub>]</code>&nbsp;represents a bidirectional edge of type&nbsp;<code>type<sub>i</sub></code>&nbsp;between nodes&nbsp;<code>u<sub>i</sub></code>&nbsp;and&nbsp;<code>v<sub>i</sub></code>, find the maximum number of edges you can remove so that after removing the edges, the graph can still be fully traversed by both Alice and Bob. The graph is fully traversed by Alice and Bob if starting from any node, they can reach all other nodes.</p>



<p>Return&nbsp;<em>the maximum number of edges you can remove, or return</em>&nbsp;<code>-1</code>&nbsp;<em>if it&#8217;s impossible for the graph to be fully traversed by Alice and Bob.</em></p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/08/19/ex1.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 4, edges = [[3,1,2],[3,2,3],[1,1,3],[1,2,4],[1,1,2],[2,3,4]]
<strong>Output:</strong> 2
<strong>Explanation: </strong>If we remove the 2 edges [1,1,2] and [1,1,3]. The graph will still be fully traversable by Alice and Bob. Removing any additional edge will not make it so. So the maximum number of edges we can remove is 2.
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/08/19/ex2.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 4, edges = [[3,1,2],[3,2,3],[1,1,4],[2,1,4]]
<strong>Output:</strong> 0
<strong>Explanation: </strong>Notice that removing any edge will not make the graph fully traversable by Alice and Bob.
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/08/19/ex3.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 4, edges = [[3,2,3],[1,1,2],[2,3,4]]
<strong>Output:</strong> -1
<strong>Explanation: </strong>In the current graph, Alice cannot reach node 4 from the other nodes. Likewise, Bob cannot reach 1. Therefore it's impossible to make the graph fully traversable.</pre>



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



<ul><li><code>1 &lt;= n &lt;= 10^5</code></li><li><code>1 &lt;= edges.length &lt;= min(10^5, 3 * n * (n-1) / 2)</code></li><li><code>edges[i].length == 3</code></li><li><code>1 &lt;= edges[i][0] &lt;= 3</code></li><li><code>1 &lt;= edges[i][1] &lt; edges[i][2] &lt;= n</code></li><li>All tuples&nbsp;<code>(type<sub>i</sub>, u<sub>i</sub>, v<sub>i</sub>)</code>&nbsp;are distinct.</li></ul>



<h2><strong>Solution: Greedy + Spanning Tree / Union Find</strong></h2>



<p>Use type 3 (both) edges first.</p>



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



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

<pre class="crayon-plain-tag">class DSU {
public: 
  DSU(int n): p_(n + 1), e_(0) {
    iota(begin(p_), end(p_), 0);
  }
  
  int find(int x) {
    if (p_[x] == x) return x;
    return p_[x] = find(p_[x]);    
  }
  
  int merge(int x, int y) {
    int rx = find(x);
    int ry = find(y);
    if (rx == ry) return 1;
    p_[rx] = ry;
    ++e_;
    return 0;
  }  
  
  int edges() const { return e_; }
private:
  vector&lt;int&gt; p_;
  int e_;
};

class Solution {
public:
  int maxNumEdgesToRemove(int n, vector&lt;vector&lt;int&gt;&gt;&amp; edges) {
    int ans = 0;
    DSU A(n), B(n);
    for (const auto&amp; e: edges) {
      if (e[0] != 3) continue;
      ans += A.merge(e[1], e[2]);
      B.merge(e[1], e[2]);
    }
    for (const auto&amp; e: edges) {
      if (e[0] == 3) continue;
      DSU&amp; d = e[0] == 1 ? A : B;
      ans += d.merge(e[1], e[2]);
    }
    return (A.edges() == n - 1 &amp;&amp; B.edges() == n - 1) ? ans : -1;
  }
};</pre>

</div><h2 class="tabtitle">python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class DSU:
  def __init__(self, n: int):
    self.p = list(range(n))
    self.e = 0
    
  def find(self, x: int) -&gt; int:
    if x != self.p[x]: self.p[x] = self.find(self.p[x])
    return self.p[x]
  
  def merge(self, x: int, y: int) -&gt; int:
    rx, ry = self.find(x), self.find(y)
    if rx == ry: return 1
    self.p[rx] = ry
    self.e += 1
    return 0
  
class Solution:
  def maxNumEdgesToRemove(self, n: int, edges: List[List[int]]) -&gt; int:
    A, B = DSU(n + 1), DSU(n + 1)    
    ans = 0
    for t, x, y in edges:
      if t != 3: continue
      ans += A.merge(x, y)
      B.merge(x, y)
    for t, x, y in edges:
      if t == 3: continue
      d = A if t == 1 else B
      ans += d.merge(x, y)
    return ans if A.e == B.e == n - 1 else -1</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-1579-remove-max-number-of-edges-to-keep-graph-fully-traversable/">花花酱 LeetCode 1579. Remove Max Number of Edges to Keep Graph Fully Traversable</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-1579-remove-max-number-of-edges-to-keep-graph-fully-traversable/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
