{"id":837,"date":"2023-11-09T21:07:45","date_gmt":"2023-11-09T13:07:45","guid":{"rendered":"http:\/\/ggapa.net:81\/?p=837"},"modified":"2023-11-09T23:45:50","modified_gmt":"2023-11-09T15:45:50","slug":"lca-%e9%97%ae%e9%a2%98%e7%9a%84%e4%b8%8d%e5%90%8c%e8%a7%a3%e6%b3%95","status":"publish","type":"post","link":"http:\/\/ggapa.net:81\/2023\/11\/09\/lca-%e9%97%ae%e9%a2%98%e7%9a%84%e4%b8%8d%e5%90%8c%e8%a7%a3%e6%b3%95\/","title":{"rendered":"LCA \u95ee\u9898\u7684\u4e0d\u540c\u89e3\u6cd5"},"content":{"rendered":"
\u4e3a\u4e86\u5f3a\u5316\u7b14\u8005\u5bf9LCA\u7684\u7406\u89e3\uff0c\u6545\u4f5c\u6b64\u6587\u3002<\/p>\n
\u8003\u8651\u6811\u4e0a\u7684\u4e24\u4e2a\u8282\u70b9 $u$, $v$ \u548c\u5176\u7956\u5148 $d$\uff0c\u6211\u4eec\u4e4b\u6240\u4ee5\u4f7f\u7528\u6b27\u62c9\u5e8f\u6c42\u89e3 LCA \u662f\u56e0\u4e3a\u5728\u6b27\u62c9\u5e8f\u4e2d $d$ \u4e00\u5b9a\u5728 $u,v$ \u4e4b\u95f4\u51fa\u73b0\u3002\u4f46\u5bf9\u4e8e DFS \u5e8f\u6765\u8bf4\uff0c$d$ \u4e00\u5b9a\u5728 $u,v$ \u4e4b\u524d\u51fa\u73b0\u3002<\/p>\n
\u4ee4 $u$ \u7684 DFN \u5c0f\u4e8e $v$\uff0c\u4e14 $u\\ne v$ \u3002<\/p>\n
\u5f53 $u\uff0cv$ \u6ca1\u6709\u7956\u5148\u5173\u7cfb\u65f6\u3002\u5728 DFN \u4e2d\u6700\u5148\u51fa\u73b0\u7684\u662f $d$ \uff0c\u5176\u6b21\u662f $u$\uff0c\u6700\u540e\u662f $v$ \u3002\u8bbe $v'$ \u662f $d$ \u7684\u513f\u5b50\u3002\u6839\u636e DFS \u7684\u7279\u6027\u53ef\u77e5\uff0c$v'$ \u5728 $u\\sim v$ \u7684 DFN \u5e8f\u4e4b\u95f4\u3002<\/p>\n
\u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ea\u9700\u8981\u6c42\u51fa $u \\sim v$ \u7684 DFS \u5e8f\u4e4b\u95f4\u6df1\u5ea6\u6700\u5c0f\u7684\u4e00\u4e2a\u8282\u70b9\uff0c\u90a3\u4e48\u4ed6\u7684\u7236\u4eb2\u4fbf\u662f\u6211\u4eec\u82e6\u82e6\u5bfb\u627e\u7684 LCA\u3002<\/p>\n
\u5982\u4f55\u4fdd\u8bc1\u8fd9\u4e2a\u65b9\u6cd5\u7684\u6b63\u786e\u6027\uff1f\u5728 $u\\sim v$ \u7684 DFS \u5e8f\u4e4b\u95f4\uff0c$d$ \u4e00\u5b9a\u4e0d\u4f1a\u5b58\u5728\uff0c\u800c $d$ \u7684\u513f\u5b50\u4e00\u5b9a\u5b58\u5728\u3002<\/p>\n
\u5f53 $u, v$ \u5b58\u5728\u7956\u5148\u5173\u7cfb\uff0c\u8fd9\u66f4\u5bb9\u6613\u5224\u65ad\u4e86\uff0c\u6839\u636e\u5b9a\u4e49\uff0c $u$ \u4e00\u5b9a\u662f $v$ \u7684\u7956\u5148\u3002\u67e5\u8be2\u7684\u5e8f\u5217\u7531 $[dfn_u,dfn_v]$ \u53d8\u6210 $[dfn_u+1,dfn_v]$ \u3002\u4e3a\u4ec0\u4e48\u8981 "$+1$"\uff1f\u6839\u636e\u4e0a\u4e00\u79cd\u60c5\u51b5\u53ef\u77e5\uff0c\u6b64\u65f6\u7684\u7956\u5148\u662f $u$ \uff0c\u4f46\u6b64\u65f6\u6df1\u5ea6\u6700\u5c0f\u7684\u8282\u70b9\u4e00\u5b9a\u662f $u$\uff0c\u5982\u679c\u5728\u8fd9\u65f6\u53d6\u5b83\u7684\u7236\u4eb2\uff0c\u5f88\u660e\u663e\u4e0d\u662f\u6211\u4eec\u8981\u627e\u7684\u6b63\u786e\u7b54\u6848\u3002<\/p>\n
\u5f88\u663e\u7136\uff0c\u5f53$u\uff0cv$ \u6ca1\u6709\u7956\u5148\u5173\u7cfb\u65f6\uff0c $u$ \u4e00\u5b9a\u4e0d\u7b49\u4e8e $d'$ \uff0c\u6240\u4ee5\u521a\u521a\u63d0\u51fa\u7684\u7ed3\u8bba\u9002\u7528 $u$ \u7684 DFN \u5c0f\u4e8e $v$\uff0c\u4e14 $u\\ne v$ \u6240\u6709\u7684\u60c5\u51b5\u3002<\/p>\n
\u82e5 $u=v$ \u65f6\uff0c\u5b83\u4eec\u7684 LCA \u4e00\u5b9a\u662f $u$ \uff0c\u8fd9\u662f\u552f\u4e00\u4e00\u79cd\u9700\u8981\u7279\u5224\u7684\u60c5\u51b5\u3002<\/p>\n
\u7efc\u4e0a\u6240\u8ff0\uff0c\u82e5 $u\\ne v$ \uff0c$u,v$ \u7684 LCA \u662f $[dfn_u,dfn_v]$ \u4e2d\u6df1\u5ea6\u6700\u5c0f\u7684\u70b9\u7684\u7236\u4eb2\uff0c\u82e5 $u=v$ \u65f6\uff0c$u,v$ \u7684 LCA \u662f $u$\u3002<\/p>\n
\u53ef\u4ee5\u901a\u8fc7 ST \u8868\u6765\u67e5\u8be2 $[dfn_u,dfn_v]$ \u4e2d\u6df1\u5ea6\u6700\u5c0f\u7684\u70b9\u3002\u65f6\u95f4\u590d\u6742\u5ea6 $O(nlogn)$ \u3002<\/p>\n
\u4ee5\u4e0b\u662f\u6a21\u677f\u9898\u76ee P3379 <\/a> \u7684\u4ee3\u7801\uff1a<\/p>\n \u5bf9\u4e8e\u4e00\u4e2a\u7236\u8282\u70b9\u7684\u6240\u6709\u513f\u5b50\u6765\u8bf4\uff0c\u5728\u5b83\u4eec\u4e4b\u95f4\u5b50\u6811\u5927\u5c0f\u6700\u5927\u7684\u4fbf\u662f\u91cd\u513f\u5b50<\/strong>($hson$)\uff0c\u5176\u4f59\u7684\u513f\u5b50\u662f\u8f7b\u513f\u5b50<\/strong>\u3002\u6211\u4eec\u53ef\u4ee5\u5bf9\u4e00\u68f5\u6811\u4e0a\u6240\u6709\u7684\u975e\u53f6\u5b50\u8282\u70b9\u8fdb\u884c\u64cd\u4f5c\uff0c\u6c42\u51fa\u4ed6\u4eec\u7684\u91cd\u513f\u5b50\u3002\u4ece\u4e00\u4e2a\u8282\u70b9 $u$ \u51fa\u53d1\uff0c\u6bcf\u6b21\u9012\u5f52\u5230\u4e0b\u4e00\u4e2a\u91cd\u513f\u5b50\u76f4\u5230\u53f6\u5b50\u8282\u70b9\u672a\u77e5\uff0c\u53ef\u5f97\u4e00\u4e2a\u9012\u5f52\u51fd\u6570 $f(x)=f(hson_x)$ \u3002\u5728\u9012\u5f52\u7684\u8fc7\u7a0b\u4e2d\u8bbf\u95ee\u5230\u7684\u8282\u70b9\u53ef\u4ee5\u8fde\u6210\u4e00\u6761\u94fe\uff0c\u8fd9\u6761\u94fe\u4fbf\u662f\u91cd\u94fe\u3002\u6bcf\u6761\u91cd\u94fe\u7684\u51fa\u53d1\u70b9\u88ab\u79f0\u4e3a $top$ \u3002<\/p>\n \u8003\u8651\u6811\u4e0a\u7684\u4e24\u4e2a\u8282\u70b9 $u$, $v$ \u548c\u5176\u7956\u5148 $d$\uff0c<\/p>\n \u4e3a\u4ec0\u4e48\u4e0d\u80fd\u540c\u65f6\u5c06 $u, v$ \u4e0a\u8df3\uff1f\u4ee4 $u$ \u7684\u6df1\u5ea6\u5927\u4e8e $v$ \u56e0\u4e3a\u6709\u65f6\u6df1\u5ea6\u8f83\u5927\u7684\u8282\u70b9\u7684 $top$ \u4fbf\u662f $v$ \u6240\u5728\u7684\u91cd\u94fe\uff0c\u6bcf\u8df3\u4e00\u6b21\u90fd\u4f1a\u4f7f\u5b83\u79bb\u5f00\u5f53\u524d\u7684\u91cd\u94fe\u3002\u8fd9\u65f6 $v$ \u5f80\u4e0a\u8df3\u5c31\u4e0d\u80fd\u8fbe\u6210\u6761\u4ef6\uff0c\u6548\u7387\u4f4e\u4e0b\u3002<\/p>\n \u65f6\u95f4\u590d\u6742\u5ea6 $O(n\\log n)$<\/p>\n \u9700\u8981\u8fdb\u884c\u4e24\u6b21\u9012\u5f52\uff0c\u7b2c\u4e00\u6b21\u9012\u5f52\u6c42\u51fa\u6bcf\u4e2a\u975e\u53f6\u5b50\u8282\u70b9\u7684\u91cd\u513f\u5b50\uff1b\u7b2c\u4e8c\u6b21\u5faa\u73af\u6c42\u51fa $top$\uff0c\u4e5f\u5c31\u662f\u91cd\u94fe\u3002<\/p>\n\/\/ P3379 \u3010\u6a21\u677f\u3011\u6700\u8fd1\u516c\u5171\u7956\u5148\uff08LCA\uff09\n\/\/ https:\/\/www.luogu.com.cn\/problem\/P3379\n\/\/ 2023-11-09 21:40\n#include <iostream>\n#include <vector>\n#include <cmath>\nusing namespace std;\nconst int maxn = 500000 + 5;\n\nint n, m, s;\nvector<int> G[maxn];\nint dfn[maxn], mi[20][maxn];\n\nint get(int x, int y) {\n return dfn[x] < dfn[y] ? x : y;\n}\n\nvoid dfs(int x, int f) {\n dfn[x] = ++dfn[0]; mi[0][dfn[x]] = f;\n for(auto to : G[x]) if(to != f) dfs(to, x);\n}\n\nint lca(int x, int y) {\n if(x == y) return x;\n if((x = dfn[x]) > (y = dfn[y])) swap(x, y);\n int d = log2(y - x++);\n return get(mi[d][x], mi[d][y - (1 << d) + 1]);\n}\n\nint main() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n \/\/ freopen("AKIOI.in", "r", stdin);\n \/\/ freopen("AKIOI.out", "w", stdout);\n cin >> n >> m >> s;\n for(int i = 1, u, v; i < n; i++) {\n cin >> u >> v;\n G[u].push_back(v); G[v].push_back(u);\n }\n dfs(s, 0);\n for(int i = 1; i <= log2(n); i++) for(int j = 1; j + (1 << i) - 1 <= n; j++) \n mi[i][j] = get(mi[i - 1][j], mi[i - 1][j + (1 << i - 1)]);\n while(m--) {\n int x, y;\n cin >> x >> y;\n cout << lca(x, y) << "\\n";\n }\n return 0;\n}\n\n\/*\n\u7f16\u7a0b\u8bed\u8a00\nC++14 (GCC 9)\n\u4ee3\u7801\u957f\u5ea6\n1.25KB\n\u7528\u65f6\n1.64s\n\u5185\u5b58\n77.61MB\nGGapa\n\n\u6240\u5c5e\u9898\u76ee\nP3379 \u3010\u6a21\u677f\u3011\u6700\u8fd1\u516c\u5171\u7956\u5148\uff08LCA\uff09\n\u8bc4\u6d4b\u72b6\u6001\nAccepted\n\u8bc4\u6d4b\u5206\u6570\n100\n\u63d0\u4ea4\u65f6\u95f4\n2023-11-09 22:02:41\n*\/<\/code><\/pre>\n
2 \u6811\u94fe\u5256\u5206 LCA<\/h1>\n
2.1 \u7b97\u6cd5\u4ecb\u7ecd<\/h2>\n
\n
2.2 \u7b97\u6cd5\u5b9e\u73b0<\/h2>\n