<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="th">
	<id>http://158.108.32.49/wiki/index.php?action=history&amp;feed=atom&amp;title=Algo_lab%2Fmst</id>
	<title>Algo lab/mst - ประวัติรุ่นแก้ไข</title>
	<link rel="self" type="application/atom+xml" href="http://158.108.32.49/wiki/index.php?action=history&amp;feed=atom&amp;title=Algo_lab%2Fmst"/>
	<link rel="alternate" type="text/html" href="http://158.108.32.49/wiki/index.php?title=Algo_lab/mst&amp;action=history"/>
	<updated>2026-05-07T00:21:39Z</updated>
	<subtitle>ประวัติรุ่นแก้ไขของหน้านี้ในวิกิ</subtitle>
	<generator>MediaWiki 1.33.1</generator>
	<entry>
		<id>http://158.108.32.49/wiki/index.php?title=Algo_lab/mst&amp;diff=59468&amp;oldid=prev</id>
		<title>Jittat: สร้างหน้าด้วย &quot;&lt;syntaxhighlight lang=&quot;cpp&quot;&gt; #include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;algorithm&gt;  using namespace std;  int n,m; vector&lt;int&gt; u,v; vector&lt;int&gt; weig...&quot;</title>
		<link rel="alternate" type="text/html" href="http://158.108.32.49/wiki/index.php?title=Algo_lab/mst&amp;diff=59468&amp;oldid=prev"/>
		<updated>2023-10-30T07:02:42Z</updated>

		<summary type="html">&lt;p&gt;สร้างหน้าด้วย &amp;quot;&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;  int n,m; vector&amp;lt;int&amp;gt; u,v; vector&amp;lt;int&amp;gt; weig...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;หน้าใหม่&lt;/b&gt;&lt;/p&gt;&lt;div&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;
&lt;br /&gt;
int n,m;&lt;br /&gt;
vector&amp;lt;int&amp;gt; u,v;&lt;br /&gt;
vector&amp;lt;int&amp;gt; weights;&lt;br /&gt;
vector&amp;lt;pair&amp;lt;int,int&amp;gt;&amp;gt; ew;&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 a,b,w;&lt;br /&gt;
    cin &amp;gt;&amp;gt; a &amp;gt;&amp;gt; b &amp;gt;&amp;gt; w;&lt;br /&gt;
    a--; b--;&lt;br /&gt;
    u.push_back(a);&lt;br /&gt;
    v.push_back(b);&lt;br /&gt;
    weights.push_back(w);&lt;br /&gt;
&lt;br /&gt;
    ew.push_back(make_pair(w,i));&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
const int MAX_N = 200010;&lt;br /&gt;
int parents[MAX_N];&lt;br /&gt;
int ranks[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;
    parents[i] = i;&lt;br /&gt;
    ranks[i] = 1;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
int find(int x) &lt;br /&gt;
{ &lt;br /&gt;
  if(parents[x]==x) {&lt;br /&gt;
    return x;&lt;br /&gt;
  } else {&lt;br /&gt;
    return find(parents[x]);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
void union_sets(int x, int y) &lt;br /&gt;
{&lt;br /&gt;
  if(parents[x] == parents[y]) {&lt;br /&gt;
    return;&lt;br /&gt;
  }&lt;br /&gt;
  int py = find(y);&lt;br /&gt;
  int px = find(x);&lt;br /&gt;
  if(ranks[px] &amp;gt; ranks[py]) {&lt;br /&gt;
    parents[py] = px;&lt;br /&gt;
  } else if(ranks[py] &amp;gt; ranks[px]) {&lt;br /&gt;
    parents[px] = py;&lt;br /&gt;
  } else {&lt;br /&gt;
    parents[py] = px;&lt;br /&gt;
    ranks[px]++;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  read_input();&lt;br /&gt;
  sort(ew.begin(), ew.end());&lt;br /&gt;
  init_union_find();&lt;br /&gt;
   &lt;br /&gt;
  int total_weight = 0;&lt;br /&gt;
&lt;br /&gt;
  for(int i=0; i&amp;lt;m; i++) {&lt;br /&gt;
    int e = ew[i].second;   //  end points are u[e] and v[e]&lt;br /&gt;
&lt;br /&gt;
    if(find(u[e]) != find(v[e])) {&lt;br /&gt;
      // add e to our solution&lt;br /&gt;
&lt;br /&gt;
      total_weight += weights[e];&lt;br /&gt;
      union_sets(u[e], v[e]);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  cout &amp;lt;&amp;lt; total_weight &amp;lt;&amp;lt; endl;&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jittat</name></author>
		
	</entry>
</feed>