<?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>bipartite Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/bipartite/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/bipartite/</link>
	<description></description>
	<lastBuildDate>Tue, 04 Sep 2018 15:34:21 +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>bipartite Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/bipartite/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 886. Possible Bipartition</title>
		<link>https://zxi.mytechroad.com/blog/graph/leetcode-886-possible-bipartition/</link>
					<comments>https://zxi.mytechroad.com/blog/graph/leetcode-886-possible-bipartition/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 12 Aug 2018 08:40:59 +0000</pubDate>
				<category><![CDATA[Graph]]></category>
		<category><![CDATA[bipartite]]></category>
		<category><![CDATA[coloring]]></category>
		<category><![CDATA[DFS]]></category>
		<category><![CDATA[graph]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[partition]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=3499</guid>

					<description><![CDATA[<p>Problem Given a set of N people (numbered 1, 2, ..., N), we would like to split everyone into two groups of any size. Each person may dislike some other&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-886-possible-bipartition/">花花酱 LeetCode 886. Possible Bipartition</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><iframe width="500" height="375" src="https://www.youtube.com/embed/VlZiMD7Iby4?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<h1><strong>Problem</strong></h1>
<p>Given a set of <code>N</code> people (numbered <code>1, 2, ..., N</code>), we would like to split everyone into two groups of <strong>any</strong> size.</p>
<p>Each person may dislike some other people, and they should not go into the same group.</p>
<p>Formally, if <code>dislikes[i] = [a, b]</code>, it means it is not allowed to put the people numbered <code>a</code> and <code>b</code> into the same group.</p>
<p>Return <code>true</code> if and only if it is possible to split everyone into two groups in this way.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>N = <span id="example-input-1-1">4</span>, dislikes = <span id="example-input-1-2">[[1,2],[1,3],[2,4]]</span>
<strong>Output: </strong><span id="example-output-1">true</span>
<strong>Explanation</strong>: group1 [1,4], group2 [2,3]
</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>N = <span id="example-input-2-1">3</span>, dislikes = <span id="example-input-2-2">[[1,2],[1,3],[2,3]]</span>
<strong>Output: </strong><span id="example-output-2">false</span>
</pre>
<p><strong>Example 3:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>N = <span id="example-input-3-1">5</span>, dislikes = <span id="example-input-3-2">[[1,2],[2,3],[3,4],[4,5],[1,5]]</span>
<strong>Output: </strong><span id="example-output-3">false</span>
</pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>1 &lt;= N &lt;= 2000</code></li>
<li><code>0 &lt;= dislikes.length &lt;= 10000</code></li>
<li><code>1 &lt;= dislikes[i][j] &lt;= N</code></li>
<li><code>dislikes[i][0] &lt; dislikes[i][1]</code></li>
<li>There does not exist <code>i != j</code> for which <code>dislikes[i] == dislikes[j]</code>.</li>
</ol>
<p>&nbsp;</p>
<p><img class="alignnone size-full wp-image-3614" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/886-ep217-1.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/886-ep217-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/886-ep217-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/886-ep217-1-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /><br />
<img class="alignnone size-full wp-image-3613" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/886-ep217-2.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/886-ep217-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/886-ep217-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/08/886-ep217-2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<p><ins class="adsbygoogle" style="display: block; text-align: center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-2404451723245401" data-ad-slot="7983117522"><br />
</ins></p>
<h1><strong>Solution: Graph Coloring</strong></h1>
<p>Color a node with one color, and color all it&#8217;s disliked nodes with another color, if can not finish return false.</p>
<p>Time complexity: O(V+E)</p>
<p>Space complexity: O(V+E)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++ / DFS</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 96 ms
class Solution {
public:
    bool possibleBipartition(int N, vector&lt;vector&lt;int&gt;&gt;&amp; dislikes) {
      g_ = vector&lt;vector&lt;int&gt;&gt;(N);
      for (const auto&amp; d : dislikes) {
        g_[d[0] - 1].push_back(d[1] - 1);
        g_[d[1] - 1].push_back(d[0] - 1);
      }
      colors_ = vector&lt;int&gt;(N, 0);  // 0: unknown, 1: red, -1: blue
      for (int i = 0; i &lt; N; ++i)
        if (colors_[i] == 0 &amp;&amp; !dfs(i, 1)) return false;
      return true;      
    }
private:
  vector&lt;vector&lt;int&gt;&gt; g_;
  vector&lt;int&gt; colors_;
  bool dfs(int cur, int color) {
    colors_[cur] = color;
    for (int nxt : g_[cur]) {
      if (colors_[nxt] == color) return false;      
      if (colors_[nxt] == 0 &amp;&amp; !dfs(nxt, -color)) return false;
    }
    return true;
  }
};</pre><p></div><h2 class="tabtitle">C++ / BFS</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 100 ms
class Solution {
public:
  bool possibleBipartition(int N, vector&lt;vector&lt;int&gt;&gt;&amp; dislikes) {
    vector&lt;vector&lt;int&gt;&gt; g(N);
    for (const auto&amp; d : dislikes) {
      g[d[0] - 1].push_back(d[1] - 1);
      g[d[1] - 1].push_back(d[0] - 1);
    }
    queue&lt;int&gt; q;
    vector&lt;int&gt; colors(N, 0);  // 0: unknown, 1: red, -1: blue
    for (int i = 0; i &lt; N; ++i) {
      if (colors[i] != 0) continue;
      q.push(i);
      colors[i] = 1;
      while (!q.empty()) {
        int cur = q.front(); q.pop();
        for (int nxt : g[cur]) {
          if (colors[nxt] == colors[cur]) return false;
          if (colors[nxt] != 0) continue;
          colors[nxt] = -colors[cur];
          q.push(nxt);
        }
      }
    }    
    return true;
  }
};</pre><p></div></div></p>
<h1><strong>Related Problem</strong></h1>
<ul>
<li><a href="https://zxi.mytechroad.com/blog/graph/leetcode-785-is-graph-bipartite/">花花酱 LeetCode 785. Is Graph Bipartite?</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-886-possible-bipartition/">花花酱 LeetCode 886. Possible Bipartition</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-886-possible-bipartition/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
