index.html 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
  6. <meta name="theme-color" content="#222">
  7. <meta name="generator" content="Hexo 4.2.0">
  8. <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  9. <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  10. <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  11. <link rel="mask-icon" href="/images/logo.svg" color="#222">
  12. <link rel="stylesheet" href="/css/main.css">
  13. <link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">
  14. <link rel="stylesheet" href="/lib/pace/pace-theme-minimal.min.css">
  15. <script src="/lib/pace/pace.min.js"></script>
  16. <script id="hexo-configurations">
  17. var NexT = window.NexT || {};
  18. var CONFIG = {"hostname":"schtonn.github.io","root":"/","scheme":"Muse","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"flat"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":false,"nav":null,"activeClass":"valine"},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideLeftIn"}}};
  19. </script>
  20. <meta name="description" content="前置知识 哈哈,简单到爆,没有。 引入 并查集是一种快到爆炸的集合算法,可以进行两项基本操作:合并两个集合(并)、查询两个参数是否在一个集合内(查)。这也是它名字的由来。">
  21. <meta property="og:type" content="article">
  22. <meta property="og:title" content="并查集">
  23. <meta property="og:url" content="https://schtonn.github.io/posts/union-find/index.html">
  24. <meta property="og:site_name" content="Alex&#39;s Blog">
  25. <meta property="og:description" content="前置知识 哈哈,简单到爆,没有。 引入 并查集是一种快到爆炸的集合算法,可以进行两项基本操作:合并两个集合(并)、查询两个参数是否在一个集合内(查)。这也是它名字的由来。">
  26. <meta property="og:locale" content="en_US">
  27. <meta property="og:image" content="https://img-blog.csdnimg.cn/20200105142700396.png">
  28. <meta property="og:image" content="https://img-blog.csdnimg.cn/20200105142531778.png">
  29. <meta property="og:image" content="https://img-blog.csdnimg.cn/20200105143342729.png">
  30. <meta property="og:image" content="https://img-blog.csdnimg.cn/20200105144059847.png">
  31. <meta property="og:image" content="https://img-blog.csdnimg.cn/20200105143532966.png">
  32. <meta property="og:image" content="https://img-blog.csdnimg.cn/20200105143644622.png">
  33. <meta property="og:image" content="https://img-blog.csdnimg.cn/20200105143847598.png">
  34. <meta property="og:image" content="https://img-blog.csdnimg.cn/20200105144933595.png">
  35. <meta property="article:published_time" content="2020-03-01T14:44:33.000Z">
  36. <meta property="article:modified_time" content="2020-03-29T07:59:58.393Z">
  37. <meta property="article:author" content="Alex">
  38. <meta property="article:tag" content="struct">
  39. <meta name="twitter:card" content="summary">
  40. <meta name="twitter:image" content="https://img-blog.csdnimg.cn/20200105142700396.png">
  41. <link rel="canonical" href="https://schtonn.github.io/posts/union-find/">
  42. <script id="page-configurations">
  43. // https://hexo.io/docs/variables.html
  44. CONFIG.page = {
  45. sidebar: "",
  46. isHome : false,
  47. isPost : true
  48. };
  49. </script>
  50. <title>并查集 | Alex's Blog</title>
  51. <noscript>
  52. <style>
  53. .use-motion .brand,
  54. .use-motion .menu-item,
  55. .sidebar-inner,
  56. .use-motion .post-block,
  57. .use-motion .pagination,
  58. .use-motion .comments,
  59. .use-motion .post-header,
  60. .use-motion .post-body,
  61. .use-motion .collection-header { opacity: initial; }
  62. .use-motion .site-title,
  63. .use-motion .site-subtitle {
  64. opacity: initial;
  65. top: initial;
  66. }
  67. .use-motion .logo-line-before i { left: initial; }
  68. .use-motion .logo-line-after i { right: initial; }
  69. </style>
  70. </noscript>
  71. </head>
  72. <body itemscope itemtype="http://schema.org/WebPage">
  73. <div class="container use-motion">
  74. <div class="headband"></div>
  75. <header class="header" itemscope itemtype="http://schema.org/WPHeader">
  76. <div class="header-inner"><div class="site-brand-container">
  77. <div class="site-nav-toggle">
  78. <div class="toggle" aria-label="Toggle navigation bar">
  79. <span class="toggle-line toggle-line-first"></span>
  80. <span class="toggle-line toggle-line-middle"></span>
  81. <span class="toggle-line toggle-line-last"></span>
  82. </div>
  83. </div>
  84. <div class="site-meta">
  85. <div>
  86. <a href="/" class="brand" rel="start">
  87. <span class="logo-line-before"><i></i></span>
  88. <span class="site-title">Alex's Blog</span>
  89. <span class="logo-line-after"><i></i></span>
  90. </a>
  91. </div>
  92. <p class="site-subtitle">Middle school OIer</p>
  93. </div>
  94. <div class="site-nav-right">
  95. <div class="toggle popup-trigger">
  96. </div>
  97. </div>
  98. </div>
  99. <nav class="site-nav">
  100. <ul id="menu" class="menu">
  101. <li class="menu-item menu-item-home">
  102. <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>Home</a>
  103. </li>
  104. <li class="menu-item menu-item-tags">
  105. <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>Tags</a>
  106. </li>
  107. <li class="menu-item menu-item-archives">
  108. <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>Archives</a>
  109. </li>
  110. <li class="menu-item menu-item-games">
  111. <a href="/games/" rel="section"><i class="fa fa-fw fa-gamepad"></i>Games</a>
  112. </li>
  113. <li class="menu-item menu-item-sans">
  114. <a href="/sans/" rel="section"><i class="fa fa-fw fa-heart"></i>sans</a>
  115. </li>
  116. </ul>
  117. </nav>
  118. </div>
  119. </header>
  120. <div class="back-to-top">
  121. <i class="fa fa-arrow-up"></i>
  122. <span>0%</span>
  123. </div>
  124. <div class="reading-progress-bar"></div>
  125. <main class="main">
  126. <div class="main-inner">
  127. <div class="content-wrap">
  128. <div class="content">
  129. <div class="posts-expand">
  130. <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="en">
  131. <link itemprop="mainEntityOfPage" href="https://schtonn.github.io/posts/union-find/">
  132. <script>
  133. (function(){
  134. if(''){
  135. if (prompt('Password?') !== ''){
  136. alert('GOAWAY!');
  137. history.back();
  138. }
  139. }
  140. })();
  141. </script>
  142. <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
  143. <meta itemprop="image" content="/images/avatar.gif">
  144. <meta itemprop="name" content="Alex">
  145. <meta itemprop="description" content="The blog of a middle school OIer">
  146. </span>
  147. <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
  148. <meta itemprop="name" content="Alex's Blog">
  149. </span>
  150. <header class="post-header">
  151. <h1 class="post-title" itemprop="name headline">
  152. 并查集
  153. </h1>
  154. <div class="post-meta">
  155. <span class="post-meta-item">
  156. <span class="post-meta-item-icon">
  157. <i class="fa fa-calendar-o"></i>
  158. </span>
  159. <span class="post-meta-item-text">Posted on</span>
  160. <time title="Created: 2020-Mar-01 22:44:33" itemprop="dateCreated datePublished" datetime="2020-03-01T22:44:33+08:00">2020-Mar-01</time>
  161. </span>
  162. <span class="post-meta-item">
  163. <span class="post-meta-item-icon">
  164. <i class="fa fa-calendar-check-o"></i>
  165. </span>
  166. <span class="post-meta-item-text">Edited on</span>
  167. <time title="Modified: 2020-Mar-29 15:59:58" itemprop="dateModified" datetime="2020-03-29T15:59:58+08:00">2020-Mar-29</time>
  168. </span>
  169. <span class="post-meta-item">
  170. <span class="post-meta-item-icon">
  171. <i class="fa fa-comment-o"></i>
  172. </span>
  173. <span class="post-meta-item-text">Valine: </span>
  174. <a title="valine" href="/posts/union-find/#valine-comments" itemprop="discussionUrl">
  175. <span class="post-comments-count valine-comment-count" data-xid="/posts/union-find/" itemprop="commentCount"></span>
  176. </a>
  177. </span>
  178. </div>
  179. </header>
  180. <div class="post-body" itemprop="articleBody">
  181. <h3 id="前置知识"><a class="markdownIt-Anchor" href="#前置知识"></a> 前置知识</h3>
  182. <p>哈哈,简单到爆,没有。</p>
  183. <h3 id="引入"><a class="markdownIt-Anchor" href="#引入"></a> 引入</h3>
  184. <p>并查集是一种快到爆炸的集合算法,可以进行两项基本操作:合并两个集合(并)、查询两个参数是否在一个集合内(查)。这也是它名字的由来。</p>
  185. <a id="more"></a>
  186. <h3 id="速度"><a class="markdownIt-Anchor" href="#速度"></a> 速度</h3>
  187. <p>他有多快呢?<br />
  188. <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>O</mi><mo stretchy="false">(</mo><mo>∗</mo><mi>l</mi><mi>o</mi><mi>g</mi><mi>n</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">O(*log n)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault" style="margin-right:0.02778em;">O</span><span class="mopen">(</span><span class="mord">∗</span><span class="mord mathdefault" style="margin-right:0.01968em;">l</span><span class="mord mathdefault">o</span><span class="mord mathdefault" style="margin-right:0.03588em;">g</span><span class="mord mathdefault">n</span><span class="mclose">)</span></span></span></span><br />
  189. <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo>∗</mo><mi>l</mi><mi>o</mi><mi>g</mi></mrow><annotation encoding="application/x-tex">*log</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord">∗</span><span class="mord mathdefault" style="margin-right:0.01968em;">l</span><span class="mord mathdefault">o</span><span class="mord mathdefault" style="margin-right:0.03588em;">g</span></span></span></span>有多可怕:</p>
  190. <table>
  191. <thead>
  192. <tr>
  193. <th><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>n</mi></mrow><annotation encoding="application/x-tex">n</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.43056em;vertical-align:0em;"></span><span class="mord mathdefault">n</span></span></span></span></th>
  194. <th><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo>∗</mo><mi>l</mi><mi>o</mi><mi>g</mi><mi>n</mi></mrow><annotation encoding="application/x-tex">*log n</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8888799999999999em;vertical-align:-0.19444em;"></span><span class="mord">∗</span><span class="mord mathdefault" style="margin-right:0.01968em;">l</span><span class="mord mathdefault">o</span><span class="mord mathdefault" style="margin-right:0.03588em;">g</span><span class="mord mathdefault">n</span></span></span></span></th>
  195. </tr>
  196. </thead>
  197. <tbody>
  198. <tr>
  199. <td><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">(</mo><mn>1</mn><mo separator="true">,</mo><mn>2</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">(1,2]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord">1</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">2</span><span class="mclose">]</span></span></span></span></td>
  200. <td><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mn>1</mn></mrow><annotation encoding="application/x-tex">1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">1</span></span></span></span></td>
  201. </tr>
  202. <tr>
  203. <td><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">(</mo><mn>2</mn><mo separator="true">,</mo><mn>4</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">(2,4]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord">2</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">4</span><span class="mclose">]</span></span></span></span></td>
  204. <td><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mn>2</mn></mrow><annotation encoding="application/x-tex">2</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">2</span></span></span></span></td>
  205. </tr>
  206. <tr>
  207. <td><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">(</mo><mn>4</mn><mo separator="true">,</mo><mn>16</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">(4,16]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord">4</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">1</span><span class="mord">6</span><span class="mclose">]</span></span></span></span></td>
  208. <td><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mn>3</mn></mrow><annotation encoding="application/x-tex">3</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">3</span></span></span></span></td>
  209. </tr>
  210. <tr>
  211. <td><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">(</mo><mn>16</mn><mo separator="true">,</mo><mn>65536</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">(16,65536]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord">1</span><span class="mord">6</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord">6</span><span class="mord">5</span><span class="mord">5</span><span class="mord">3</span><span class="mord">6</span><span class="mclose">]</span></span></span></span></td>
  212. <td><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mn>4</mn></mrow><annotation encoding="application/x-tex">4</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">4</span></span></span></span></td>
  213. </tr>
  214. <tr>
  215. <td><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo stretchy="false">(</mo><mn>65536</mn><mo separator="true">,</mo><msup><mn>2</mn><mn>65536</mn></msup><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">(65536,2^{65536}]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1.064108em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord">6</span><span class="mord">5</span><span class="mord">5</span><span class="mord">3</span><span class="mord">6</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.16666666666666666em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">6</span><span class="mord mtight">5</span><span class="mord mtight">5</span><span class="mord mtight">3</span><span class="mord mtight">6</span></span></span></span></span></span></span></span></span><span class="mclose">]</span></span></span></span></td>
  216. <td><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mn>5</mn></mrow><annotation encoding="application/x-tex">5</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">5</span></span></span></span></td>
  217. </tr>
  218. </tbody>
  219. </table>
  220. <p>所以n是<span class="katex"><span class="katex-mathml"><math><semantics><mrow><msup><mn>2</mn><mn>65536</mn></msup></mrow><annotation encoding="application/x-tex">2^{65536}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">6</span><span class="mord mtight">5</span><span class="mord mtight">5</span><span class="mord mtight">3</span><span class="mord mtight">6</span></span></span></span></span></span></span></span></span></span></span></span>的时候复杂度还只有5。<br />
  221. <a href="https://sites.google.com/site/largenumbers/home/appendix/a/numbers/265536" target="_blank" rel="noopener" title="哈哈!不跨越GREATWALL打不开"><span class="katex"><span class="katex-mathml"><math><semantics><mrow><msup><mn>2</mn><mn>65536</mn></msup></mrow><annotation encoding="application/x-tex">2^{65536}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8141079999999999em;vertical-align:0em;"></span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8141079999999999em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">6</span><span class="mord mtight">5</span><span class="mord mtight">5</span><span class="mord mtight">3</span><span class="mord mtight">6</span></span></span></span></span></span></span></span></span></span></span></span></a>有多大,我把他拷进来的时候整个电脑卡死了。我不得不强制重启,然后重新写一遍这段。他有19729位。想通了吧?</p>
  222. <h3 id="如何实现"><a class="markdownIt-Anchor" href="#如何实现"></a> 如何实现</h3>
  223. <p>这么高端的算法,是怎么实现的呢?<br />
  224. 其实</p>
  225. <p>它的本质就是一个数组<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>f</mi><mo stretchy="false">[</mo><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">f[]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault" style="margin-right:0.10764em;">f</span><span class="mopen">[</span><span class="mclose">]</span></span></span></span>,和一个函数<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>g</mi><mi>e</mi><mi>t</mi><mi>f</mi><mo stretchy="false">(</mo><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">getf()</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault" style="margin-right:0.03588em;">g</span><span class="mord mathdefault">e</span><span class="mord mathdefault">t</span><span class="mord mathdefault" style="margin-right:0.10764em;">f</span><span class="mopen">(</span><span class="mclose">)</span></span></span></span><br />
  226. <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>f</mi><mo stretchy="false">[</mo><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">f[]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault" style="margin-right:0.10764em;">f</span><span class="mopen">[</span><span class="mclose">]</span></span></span></span>存的实际上就是几棵树。<br />
  227. <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>f</mi><mo stretchy="false">[</mo><mi>i</mi><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">f[i]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault" style="margin-right:0.10764em;">f</span><span class="mopen">[</span><span class="mord mathdefault">i</span><span class="mclose">]</span></span></span></span>就是<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>i</mi></mrow><annotation encoding="application/x-tex">i</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.65952em;vertical-align:0em;"></span><span class="mord mathdefault">i</span></span></span></span>的父亲。<br />
  228. <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>g</mi><mi>e</mi><mi>t</mi><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">getf(i)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault" style="margin-right:0.03588em;">g</span><span class="mord mathdefault">e</span><span class="mord mathdefault">t</span><span class="mord mathdefault" style="margin-right:0.10764em;">f</span><span class="mopen">(</span><span class="mord mathdefault">i</span><span class="mclose">)</span></span></span></span>做的操作就是递归顺着<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>f</mi><mo stretchy="false">[</mo><mi>i</mi><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">f[i]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault" style="margin-right:0.10764em;">f</span><span class="mopen">[</span><span class="mord mathdefault">i</span><span class="mclose">]</span></span></span></span>找<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>i</mi></mrow><annotation encoding="application/x-tex">i</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.65952em;vertical-align:0em;"></span><span class="mord mathdefault">i</span></span></span></span>所在的树的根。<br />
  229. <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>g</mi><mi>e</mi><mi>t</mi><mi>f</mi><mo stretchy="false">(</mo><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">getf()</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault" style="margin-right:0.03588em;">g</span><span class="mord mathdefault">e</span><span class="mord mathdefault">t</span><span class="mord mathdefault" style="margin-right:0.10764em;">f</span><span class="mopen">(</span><span class="mclose">)</span></span></span></span>代码:</p>
  230. <figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">getf</span><span class="params">(<span class="keyword">int</span> x)</span></span>&#123;</span><br><span class="line"> <span class="keyword">if</span>(f[x]==x)<span class="keyword">return</span> x;</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">return</span> getf(f[x]);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
  231. <p><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi></mrow><annotation encoding="application/x-tex">...</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.10556em;vertical-align:0em;"></span><span class="mord">.</span><span class="mord">.</span><span class="mord">.</span></span></span></span></p>
  232. <p>那这个算法就很低端了<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi></mrow><annotation encoding="application/x-tex">...</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.10556em;vertical-align:0em;"></span><span class="mord">.</span><span class="mord">.</span><span class="mord">.</span></span></span></span></p>
  233. <p>那还讲个鬼啊<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi><mi mathvariant="normal">.</mi></mrow><annotation encoding="application/x-tex">...</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.10556em;vertical-align:0em;"></span><span class="mord">.</span><span class="mord">.</span><span class="mord">.</span></span></span></span></p>
  234. <p>所以</p>
  235. <h3 id="超级优化"><a class="markdownIt-Anchor" href="#超级优化"></a> 超级优化</h3>
  236. <p>我们首先随机造出一些操作:</p>
  237. <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">merge 1 5</span><br><span class="line">merge 5 2</span><br><span class="line">merge 1 3</span><br><span class="line">check 2 3</span><br><span class="line">merge 3 4</span><br><span class="line">merge 6 7</span><br><span class="line">check 1 7</span><br><span class="line">merge 7 8</span><br><span class="line">merge 8 9</span><br><span class="line">merge 1 9</span><br><span class="line">check 7 4</span><br></pre></td></tr></table></figure>
  238. <p>其中,merge代表合并,check代表查询。<br />
  239. 如果按照刚才所的算法,那么在第一次查询之前,就会出来这样的森林:<img data-src="https://img-blog.csdnimg.cn/20200105142700396.png" alt="Insert mother fucker" /><br />
  240. 到最后,就形成了这样一个繁杂的森林,要找到一个点的根,就需要走很长一段路。这就拉长了时间。<br />
  241. <img data-src="https://img-blog.csdnimg.cn/20200105142531778.png" alt="Insert mother fucker" /><br />
  242. 为了缩减时间,超级优化就出现了:路径压缩。<br />
  243. 路径压缩其实也很简单:在<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>g</mi><mi>e</mi><mi>t</mi><mi>f</mi><mo stretchy="false">(</mo><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">getf()</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault" style="margin-right:0.03588em;">g</span><span class="mord mathdefault">e</span><span class="mord mathdefault">t</span><span class="mord mathdefault" style="margin-right:0.10764em;">f</span><span class="mopen">(</span><span class="mclose">)</span></span></span></span>查找根节点的同时,把自己也链接到根节点上,使得树的深度不超过2。<br />
  244. 代码:</p>
  245. <figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">getf</span><span class="params">(<span class="keyword">int</span> x)</span></span>&#123;</span><br><span class="line"> <span class="keyword">if</span>(f[x]==x)<span class="keyword">return</span> x;</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">return</span> f[x]=getf(f[x]);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
  246. <p>发现没有,和之前的代码相比,只改了一个地方,就让时间大大压缩。<br />
  247. 这时我们在模拟一下。<br />
  248. 第一次合并:<img data-src="https://img-blog.csdnimg.cn/20200105143342729.png" alt="Insert mother fucker" /><br />
  249. 第二次合并时,首先寻找2,5两个节点的根节点,2的根就是2,5的根是1,于是直接把2链接到1上。<br />
  250. <img data-src="https://img-blog.csdnimg.cn/20200105144059847.png" alt="Insert mother fucker" /><br />
  251. 第三次,第四次合并把3链接到了1上,然后把4顺着3也链接到了1上,第五次连接了6和7。<br />
  252. <img data-src="https://img-blog.csdnimg.cn/20200105143532966.png" alt="Insert mother fucker" /><br />
  253. 第七次第八次链接成了<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mn>9</mn><mo>→</mo><mn>8</mn><mo>→</mo><mn>7</mn><mo>→</mo><mn>6</mn></mrow><annotation encoding="application/x-tex">9\rightarrow8\rightarrow7\rightarrow6</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">9</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">→</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">8</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">→</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">7</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span><span class="mrel">→</span><span class="mspace" style="margin-right:0.2777777777777778em;"></span></span><span class="base"><span class="strut" style="height:0.64444em;vertical-align:0em;"></span><span class="mord">6</span></span></span></span>一长串,然后经过路径压缩都链接到6上了。<br />
  254. <img data-src="https://img-blog.csdnimg.cn/20200105143644622.png" alt="Insert mother fucker" /><br />
  255. 最后一次,把9和1链接起来了,这时深度又超过了2,一下还压缩不下去,不过没关系,查询的时候就会把它压缩的。<img data-src="https://img-blog.csdnimg.cn/20200105143847598.png" alt="Insert mother fucker" /><br />
  256. 比如查询7和4的时候就会分别寻找7和4的根节点,一路递归找上去的时候就直接把路径压缩好了,除了8还链接在6上,其他全部链接到1上了。<br />
  257. <img data-src="https://img-blog.csdnimg.cn/20200105144933595.png" alt="Insert mother fucker" /><br />
  258. 多么有趣啊!</p>
  259. <h3 id="代码"><a class="markdownIt-Anchor" href="#代码"></a> 代码</h3>
  260. <p>自己想去吧,核心代码和思路都给出来了。<br />
  261. 有一个巨大的坑,就是<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>f</mi><mo stretchy="false">[</mo><mi>i</mi><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">f[i]</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault" style="margin-right:0.10764em;">f</span><span class="mopen">[</span><span class="mord mathdefault">i</span><span class="mclose">]</span></span></span></span>要预设成<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>i</mi></mrow><annotation encoding="application/x-tex">i</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.65952em;vertical-align:0em;"></span><span class="mord mathdefault">i</span></span></span></span>,不然会爆炸。<br />
  262. 加油!<s>克服恐惧的最好办法就是面对恐忄</s>快去写吧!</p>
  263. </div>
  264. <div>
  265. <ul class="post-copyright">
  266. <li class="post-copyright-author">
  267. <strong>Post author: </strong>Alex
  268. </li>
  269. <li class="post-copyright-link">
  270. <strong>Post link: </strong>
  271. <a href="https://schtonn.github.io/posts/union-find/" title="并查集">https://schtonn.github.io/posts/union-find/</a>
  272. </li>
  273. <li class="post-copyright-license">
  274. <strong>Copyright Notice: </strong>All articles in this blog are licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fa fa-fw fa-creative-commons"></i>BY-NC-SA</a> unless stating additionally.
  275. </li>
  276. </ul>
  277. </div>
  278. <footer class="post-footer">
  279. <div class="post-tags">
  280. <a href="/tags/struct/" rel="tag"><i class="fa fa-tag"></i> struct</a>
  281. </div>
  282. <div class="post-nav">
  283. <div class="post-nav-item">
  284. <a href="/posts/tree-link/" rel="prev" title="树链剖分">
  285. <i class="fa fa-chevron-left"></i> 树链剖分
  286. </a></div>
  287. <div class="post-nav-item">
  288. <a href="/posts/min-span-tree/" rel="next" title="最小生成树">
  289. 最小生成树 <i class="fa fa-chevron-right"></i>
  290. </a></div>
  291. </div>
  292. </footer>
  293. </article>
  294. </div>
  295. </div>
  296. <div class="comments" id="valine-comments"></div>
  297. <script>
  298. window.addEventListener('tabs:register', () => {
  299. let activeClass = CONFIG.comments.activeClass;
  300. if (CONFIG.comments.storage) {
  301. activeClass = localStorage.getItem('comments_active') || activeClass;
  302. }
  303. if (activeClass) {
  304. let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
  305. if (activeTab) {
  306. activeTab.click();
  307. }
  308. }
  309. });
  310. if (CONFIG.comments.storage) {
  311. window.addEventListener('tabs:click', event => {
  312. if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
  313. let commentClass = event.target.classList[1];
  314. localStorage.setItem('comments_active', commentClass);
  315. });
  316. }
  317. </script>
  318. </div>
  319. <div class="toggle sidebar-toggle">
  320. <span class="toggle-line toggle-line-first"></span>
  321. <span class="toggle-line toggle-line-middle"></span>
  322. <span class="toggle-line toggle-line-last"></span>
  323. </div>
  324. <aside class="sidebar">
  325. <div class="sidebar-inner">
  326. <ul class="sidebar-nav motion-element">
  327. <li class="sidebar-nav-toc">
  328. Table of Contents
  329. </li>
  330. <li class="sidebar-nav-overview">
  331. Overview
  332. </li>
  333. </ul>
  334. <!--noindex-->
  335. <div class="post-toc-wrap sidebar-panel">
  336. <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#前置知识"><span class="nav-number">1.</span> <span class="nav-text"> 前置知识</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#引入"><span class="nav-number">2.</span> <span class="nav-text"> 引入</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#速度"><span class="nav-number">3.</span> <span class="nav-text"> 速度</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#如何实现"><span class="nav-number">4.</span> <span class="nav-text"> 如何实现</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#超级优化"><span class="nav-number">5.</span> <span class="nav-text"> 超级优化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#代码"><span class="nav-number">6.</span> <span class="nav-text"> 代码</span></a></li></ol></div>
  337. </div>
  338. <!--/noindex-->
  339. <div class="site-overview-wrap sidebar-panel">
  340. <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  341. <p class="site-author-name" itemprop="name">Alex</p>
  342. <div class="site-description" itemprop="description">The blog of a middle school OIer</div>
  343. </div>
  344. <div class="site-state-wrap motion-element">
  345. <nav class="site-state">
  346. <div class="site-state-item site-state-posts">
  347. <a href="/archives">
  348. <span class="site-state-item-count">10</span>
  349. <span class="site-state-item-name">posts</span>
  350. </a>
  351. </div>
  352. <div class="site-state-item site-state-tags">
  353. <a href="/tags/">
  354. <span class="site-state-item-count">4</span>
  355. <span class="site-state-item-name">tags</span></a>
  356. </div>
  357. </nav>
  358. </div>
  359. <div class="links-of-author motion-element">
  360. <span class="links-of-author-item">
  361. <a href="https://github.com/schtonn" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;schtonn" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
  362. </span>
  363. <span class="links-of-author-item">
  364. <a href="skype:alex?call|chat" title="Skype → skype:alex?call|chat" rel="noopener" target="_blank"><i class="fa fa-fw fa-skype"></i>Skype</a>
  365. </span>
  366. </div>
  367. <div class="links-of-blogroll motion-element">
  368. <div class="links-of-blogroll-title">
  369. <i class="fa fa-fw fa-link"></i>
  370. Links
  371. </div>
  372. <ul class="links-of-blogroll-list">
  373. <li class="links-of-blogroll-item">
  374. <a href="https://yonghong.github.io/" title="https:&#x2F;&#x2F;yonghong.github.io" rel="noopener" target="_blank">Yonghong</a>
  375. </li>
  376. <li class="links-of-blogroll-item">
  377. <a href="https://source.unsplash.com/random/1600x900" title="https:&#x2F;&#x2F;source.unsplash.com&#x2F;random&#x2F;1600x900" rel="noopener" target="_blank">Background</a>
  378. </li>
  379. </ul>
  380. </div>
  381. </div>
  382. <div id="treefrog" style="text-align: center;margin-top: 18px;">
  383. <object type="application/x-shockwave-flash" style="outline:none;" data="/js/treefrog.swf?up_bodyColor=444444&up_pattern=0&up_flyColor=777777&up_tongueColor=555555&up_patternColor=000000&up_releaseFly=0&up_frogName=Froggie&up_backgroundImage=http://&up_bellySize=.5&up_footColor=444444&up_eyeColor=444444&up_backgroundColor=222222&" width="300" height="600"><param name="movie" value="http://cdn.abowman.com/widgets/treefrog/treefrog.swf?up_bodyColor=444444&up_pattern=0&up_flyColor=777777&up_tongueColor=555555&up_patternColor=000000&up_releaseFly=0&up_frogName=Froggie&up_backgroundImage=http://&up_bellySize=.5&up_footColor=444444&up_eyeColor=444444&up_backgroundColor=222222&"></param><param name="AllowScriptAccess" value="always"></param><param name="wmode" value="opaque"></param><param name="scale" value="noscale"/><param name="salign" value="tl"/></object>
  384. </div>
  385. </div>
  386. </aside>
  387. <div id="sidebar-dimmer"></div>
  388. </div>
  389. </main>
  390. <footer class="footer">
  391. <div class="footer-inner">
  392. <div class="copyright">
  393. &copy;
  394. <span itemprop="copyrightYear">2020</span>
  395. <span class="with-love">
  396. <i class="fa fa-user"></i>
  397. </span>
  398. <span class="author" itemprop="copyrightHolder">Alex Xiang</span>
  399. </div>
  400. </div>
  401. </footer>
  402. </div>
  403. <script src="/lib/anime.min.js"></script>
  404. <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>
  405. <script src="/lib/velocity/velocity.min.js"></script>
  406. <script src="/lib/velocity/velocity.ui.min.js"></script>
  407. <script src="/js/utils.js"></script>
  408. <script src="/js/motion.js"></script>
  409. <script src="/js/schemes/muse.js"></script>
  410. <script src="/js/next-boot.js"></script>
  411. <script>
  412. (function(){
  413. var bp = document.createElement('script');
  414. var curProtocol = window.location.protocol.split(':')[0];
  415. bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
  416. var s = document.getElementsByTagName("script")[0];
  417. s.parentNode.insertBefore(bp, s);
  418. })();
  419. </script>
  420. <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/katex@0/dist/katex.min.css">
  421. <script>
  422. NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  423. NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
  424. var GUEST = ['nick', 'mail', 'link'];
  425. var guest = 'nick,mail,link';
  426. guest = guest.split(',').filter(item => {
  427. return GUEST.includes(item);
  428. });
  429. new Valine({
  430. el : '#valine-comments',
  431. verify : true,
  432. notify : true,
  433. appId : 'BmologYYnRqCv0SLHDeDdA17-gzGzoHsz',
  434. appKey : 'w9mVebFMdCmY6Nh9vfcBGaGt',
  435. placeholder: "Balabala.bibibibibibi.bopbopbibibop.barbarbababb.jaxjaxjax.dindididndi.lerblerb.pilipildde.",
  436. avatar : 'mm',
  437. meta : guest,
  438. pageSize : '10' || 10,
  439. visitor : false,
  440. lang : 'en' || 'zh-cn',
  441. path : location.pathname,
  442. recordIP : false,
  443. serverURLs : ''
  444. });
  445. }, window.Valine);
  446. });
  447. </script>
  448. </body>
  449. </html>