<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="th">
	<id>https://theory.cpe.ku.ac.th/wiki/index.php?action=history&amp;feed=atom&amp;title=Kruskal_union_find</id>
	<title>Kruskal union find - ประวัติรุ่นแก้ไข</title>
	<link rel="self" type="application/atom+xml" href="https://theory.cpe.ku.ac.th/wiki/index.php?action=history&amp;feed=atom&amp;title=Kruskal_union_find"/>
	<link rel="alternate" type="text/html" href="https://theory.cpe.ku.ac.th/wiki/index.php?title=Kruskal_union_find&amp;action=history"/>
	<updated>2026-04-14T16:44:25Z</updated>
	<subtitle>ประวัติรุ่นแก้ไขของหน้านี้ในวิกิ</subtitle>
	<generator>MediaWiki 1.33.1</generator>
	<entry>
		<id>https://theory.cpe.ku.ac.th/wiki/index.php?title=Kruskal_union_find&amp;diff=60025&amp;oldid=prev</id>
		<title>Jittat: สร้างหน้าด้วย &quot;== Slow kruskal ==  &lt;syntaxhighlight lang=&quot;cpp&quot;&gt; #include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;algorithm&gt;  using namespace std; const int MAX_N = 10001...&quot;</title>
		<link rel="alternate" type="text/html" href="https://theory.cpe.ku.ac.th/wiki/index.php?title=Kruskal_union_find&amp;diff=60025&amp;oldid=prev"/>
		<updated>2026-02-23T07:09:42Z</updated>

		<summary type="html">&lt;p&gt;สร้างหน้าด้วย &amp;quot;== Slow kruskal ==  &amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt; #include &amp;lt;iostream&amp;gt; #include &amp;lt;vector&amp;gt; #include &amp;lt;algorithm&amp;gt;  using namespace std; const int MAX_N = 10001...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;หน้าใหม่&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Slow kruskal ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
#include &amp;lt;algorithm&amp;gt;&lt;br /&gt;
&lt;br /&gt;
using namespace std;&lt;br /&gt;
const int MAX_N = 100010;&lt;br /&gt;
const int MAX_M = 200010;&lt;br /&gt;
&lt;br /&gt;
int n,m;&lt;br /&gt;
pair&amp;lt;int,int&amp;gt; edges[MAX_M];&lt;br /&gt;
int weights[MAX_M];&lt;br /&gt;
vector&amp;lt;pair&amp;lt;int,int&amp;gt;&amp;gt; windex;&lt;br /&gt;
&lt;br /&gt;
void read_input()&lt;br /&gt;
{&lt;br /&gt;
  cin &amp;gt;&amp;gt; n &amp;gt;&amp;gt; m;&lt;br /&gt;
  for(int i=0; i&amp;lt;m; i++) {&lt;br /&gt;
    int u,v,l;&lt;br /&gt;
    cin &amp;gt;&amp;gt; u &amp;gt;&amp;gt; v &amp;gt;&amp;gt; l;  u--;  v--;&lt;br /&gt;
    edges[i].first = u;&lt;br /&gt;
    edges[i].second = v;&lt;br /&gt;
    weights[i] = l;&lt;br /&gt;
&lt;br /&gt;
    // for sorting&lt;br /&gt;
    windex.push_back({ l, i });&lt;br /&gt;
  }&lt;br /&gt;
  sort(windex.begin(), windex.end());&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int components[MAX_N];&lt;br /&gt;
void init_union_find()&lt;br /&gt;
{&lt;br /&gt;
  for(int i=0; i&amp;lt;n; i++)&lt;br /&gt;
    components[i] = i;&lt;br /&gt;
}&lt;br /&gt;
int find(int u)&lt;br /&gt;
{&lt;br /&gt;
  return components[u];&lt;br /&gt;
}&lt;br /&gt;
void union_sets(int cu, int cv)&lt;br /&gt;
{&lt;br /&gt;
  if(cu == cv)&lt;br /&gt;
    return;&lt;br /&gt;
  for(int i=0; i&amp;lt;n; i++)&lt;br /&gt;
    if(components[i] == cu)&lt;br /&gt;
      components[i] = cv;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  read_input();&lt;br /&gt;
  int cost = 0;&lt;br /&gt;
  init_union_find();&lt;br /&gt;
  for(int i=0; i&amp;lt;m; i++) {&lt;br /&gt;
    // consider windex[i] -- edge with the i-th min weight&lt;br /&gt;
    int e = windex[i].second;&lt;br /&gt;
    int l = weights[e];&lt;br /&gt;
    int u = edges[e].first;&lt;br /&gt;
    int v = edges[e].second;&lt;br /&gt;
&lt;br /&gt;
    int cu = find(u);&lt;br /&gt;
    int cv = find(v);&lt;br /&gt;
    if(cu != cv) {&lt;br /&gt;
      cost += l;&lt;br /&gt;
      union_sets(cu,cv);&lt;br /&gt;
    }    &lt;br /&gt;
  }&lt;br /&gt;
  cout &amp;lt;&amp;lt; cost &amp;lt;&amp;lt; endl;&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Milk ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
#include &amp;lt;algorithm&amp;gt;&lt;br /&gt;
#include &amp;lt;cassert&amp;gt;&lt;br /&gt;
&lt;br /&gt;
using namespace std;&lt;br /&gt;
const int MAX_N = 100010;&lt;br /&gt;
const int MAX_M = 200010;&lt;br /&gt;
&lt;br /&gt;
int n,q;&lt;br /&gt;
&lt;br /&gt;
int parents[MAX_N];&lt;br /&gt;
int s[MAX_N];&lt;br /&gt;
&lt;br /&gt;
void init_union_find()&lt;br /&gt;
{&lt;br /&gt;
  for(int i=0; i&amp;lt;n; i++) {&lt;br /&gt;
    parents[i] = i;&lt;br /&gt;
    s[i] = 1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
int find(int u)&lt;br /&gt;
{&lt;br /&gt;
  if(parents[u] == u)  // I&amp;#039;m groot&lt;br /&gt;
    return u;&lt;br /&gt;
  else &lt;br /&gt;
    return find(parents[u]);&lt;br /&gt;
}&lt;br /&gt;
void union_sets(int cu, int cv)&lt;br /&gt;
{&lt;br /&gt;
  assert(parents[cu] == cu);&lt;br /&gt;
  assert(parents[cv] == cv);&lt;br /&gt;
  if(cu == cv)&lt;br /&gt;
    return;&lt;br /&gt;
  if(s[cu] &amp;gt; s[cv]) {&lt;br /&gt;
    // ... TODO&lt;br /&gt;
  } else {&lt;br /&gt;
    // ... TODO&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  ios::sync_with_stdio(false);&lt;br /&gt;
  cin.tie(NULL);&lt;br /&gt;
 &lt;br /&gt;
  char cmd[10];&lt;br /&gt;
  cin &amp;gt;&amp;gt; n &amp;gt;&amp;gt; q;&lt;br /&gt;
  init_union_find();&lt;br /&gt;
  for(int i=0; i&amp;lt;q; i++) {&lt;br /&gt;
    int x, y;&lt;br /&gt;
    cin &amp;gt;&amp;gt; cmd &amp;gt;&amp;gt; x &amp;gt;&amp;gt; y; x--; y--;&lt;br /&gt;
&lt;br /&gt;
    int cx = find(x);&lt;br /&gt;
    int cy = find(y);&lt;br /&gt;
    if(cmd[0] == &amp;#039;c&amp;#039;) {&lt;br /&gt;
      union_sets(cx,cy);&lt;br /&gt;
    } else {&lt;br /&gt;
      if(cx == cy)&lt;br /&gt;
	cout &amp;lt;&amp;lt; &amp;quot;yes\n&amp;quot;;&lt;br /&gt;
      else&lt;br /&gt;
	cout &amp;lt;&amp;lt; &amp;quot;no\n&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jittat</name></author>
		
	</entry>
</feed>