From ef8275fc7ae6952af59ab932f8e59371deab4726 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sun, 20 Nov 2022 09:14:14 +0800 Subject: [PATCH] =?UTF-8?q?P3379=20=E3=80=90=E6=A8=A1=E6=9D=BF=E3=80=91?= =?UTF-8?q?=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1=E7=A5=96=E5=85=88=EF=BC=88?= =?UTF-8?q?LCA=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/94730020 --- Luogu/P3379/P3379.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Luogu/P3379/P3379.cpp b/Luogu/P3379/P3379.cpp index 6ea26669..8f45d9b7 100644 --- a/Luogu/P3379/P3379.cpp +++ b/Luogu/P3379/P3379.cpp @@ -10,13 +10,14 @@ const char endl = '\n'; const int N = 5e5 + 5; const int INF = 0x3f3f3f3f; -int n, m, s, dep[N], fa[N][25]; +int n, m, s, dep[N], fa[N][std::__lg(N) + 1]; std::vector g[N]; void bfs(int s) { std::fill_n(dep, N, INF); std::queue q; + q.emplace(s); dep[0] = 0; dep[s] = 1; @@ -31,7 +32,7 @@ void bfs(int s) { q.emplace(v); fa[v][0] = u; - for (int k = 1; k <= 19; k++) { + for (int k = 1; k <= std::__lg(n); k++) { fa[v][k] = fa[fa[v][k - 1]][k - 1]; } } @@ -42,7 +43,7 @@ void bfs(int s) { int lca(int x, int y) { if (dep[x] < dep[y]) std::swap(x, y); - for (int k = 19; k >= 0; k--) { + for (int k = std::__lg(n); k >= 0; k--) { if (dep[fa[x][k]] >= dep[y]) { x = fa[x][k]; } @@ -50,7 +51,7 @@ int lca(int x, int y) { if (x == y) return x; - for (int k = 19; k >= 0; k--) { + for (int k = std::__lg(n); k >= 0; k--) { if (fa[x][k] != fa[y][k]) { x = fa[x][k]; y = fa[y][k];