{"id":137,"date":"2024-03-01T15:00:05","date_gmt":"2024-03-01T07:00:05","guid":{"rendered":"https:\/\/linxce.ink\/?p=137"},"modified":"2024-03-01T15:00:05","modified_gmt":"2024-03-01T07:00:05","slug":"%e6%a0%91%e9%93%be%e5%89%96%e5%88%86lca","status":"publish","type":"post","link":"https:\/\/linxce.ink\/index.php\/%e6%a0%91%e9%93%be%e5%89%96%e5%88%86lca\/%e5%85%a8%e9%83%a8\/acm\/","title":{"rendered":"\u6811\u94fe\u5256\u5206+LCA"},"content":{"rendered":"<h3>\u6811\u94fe\u5256\u5206\uff1f\u6811\u8bba\u7cd5\u624b\u7684\u7b2c\u4e00\u6b65\uff01<\/h3>\n<h4>\u65f6\u95f4\u590d\u6742\u5ea6O(0.37nlogn)<\/h4>\n<pre><code class=\"language-cc\" lang=\"cc\">vector&lt;int&gt; tr[N];\r\nint big[N], siz[N], top[N], fa[N], depth[N], idx;\r\nvoid dfs1(int &amp;u, int f) \/\/\u9884\u5904\u74061\r\n{\r\n    siz[u]++;\r\n    fa[u] = f;\r\n    depth[u] = depth[f] + 1;\r\n    for (auto v : tr[u])\r\n        if (v != f)\r\n        {\r\n            dfs1(v, u);\r\n            siz[u] += siz[v];\r\n            if (!big[u] || siz[big[u]] &lt; siz[v])\r\n                big[u] = v;\r\n        }\r\n}\r\nvoid dfs2(int &amp;u, int f, int &amp;t) \/\/\u9884\u5904\u74062\r\n{\r\n    top[u] = t; \/\/ \u8bb0\u5f55\u91cd\u94fe\u7684\u5934\r\n    if (big[u])\r\n        dfs2(big[u], u, t);\r\n    for (int v : tr[u])\r\n        if (v != f &amp;&amp; v != big[u])\r\n            dfs2(v, u, v);\r\n}\r\nint lca(int u, int v)\r\n{\r\n    while (top[u] != top[v])\r\n        depth[top[u]] &gt; depth[top[v]] ? u = fa[top[u]] : v = fa[top[v]];\r\n    return depth[u] &gt; depth[v] ? v : u;\r\n}\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6811\u94fe\u5256\u5206\uff1f\u6811\u8bba\u7cd5\u624b\u7684\u7b2c\u4e00\u6b65\uff01 \u65f6&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,18,19],"tags":[7,17,20],"class_list":["post-137","post","type-post","status-publish","format-standard","hentry","category-acm","category-18","category-19","tag-cpp","tag-17","tag-20"],"_links":{"self":[{"href":"https:\/\/linxce.ink\/index.php\/wp-json\/wp\/v2\/posts\/137","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linxce.ink\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linxce.ink\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linxce.ink\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linxce.ink\/index.php\/wp-json\/wp\/v2\/comments?post=137"}],"version-history":[{"count":1,"href":"https:\/\/linxce.ink\/index.php\/wp-json\/wp\/v2\/posts\/137\/revisions"}],"predecessor-version":[{"id":138,"href":"https:\/\/linxce.ink\/index.php\/wp-json\/wp\/v2\/posts\/137\/revisions\/138"}],"wp:attachment":[{"href":"https:\/\/linxce.ink\/index.php\/wp-json\/wp\/v2\/media?parent=137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linxce.ink\/index.php\/wp-json\/wp\/v2\/categories?post=137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linxce.ink\/index.php\/wp-json\/wp\/v2\/tags?post=137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}