From f38e430d75e5979fc758815607496ff40893da3b Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Fri, 28 Oct 2022 20:46:59 +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/91945740 --- Luogu/P3379/P3379.cpp | 91 ++++++++++++++++++++++++++++--------------- 1 file changed, 59 insertions(+), 32 deletions(-) diff --git a/Luogu/P3379/P3379.cpp b/Luogu/P3379/P3379.cpp index 09a305ce..6ea26669 100644 --- a/Luogu/P3379/P3379.cpp +++ b/Luogu/P3379/P3379.cpp @@ -1,60 +1,87 @@ -#include +#include +#include +#include +#include -using namespace std; +using std::cin; +using std::cout; +const char endl = '\n'; -int n, m, s, root, a, b, dep[500005], fa[500005][20]; -vector tr[500005]; +const int N = 5e5 + 5; +const int INF = 0x3f3f3f3f; -void bfs(int root) { - memset(dep, 0x3f, sizeof(dep)); +int n, m, s, dep[N], fa[N][25]; +std::vector g[N]; + +void bfs(int s) { + std::fill_n(dep, N, INF); + + std::queue q; + q.emplace(s); dep[0] = 0; - dep[root] = 1; - queue q; - q.push(root); + dep[s] = 1; + while (!q.empty()) { - int t = q.front(); + int u = q.front(); q.pop(); - for (int i : tr[t]) { - if (dep[i] > dep[t] + 1) { - dep[i] = dep[t] + 1; - q.push(i); - fa[i][0] = t; + + for (int v : g[u]) { + if (dep[v] > dep[u] + 1) { + dep[v] = dep[u] + 1; + q.emplace(v); + fa[v][0] = u; + for (int k = 1; k <= 19; k++) { - fa[i][k] = fa[fa[i][k - 1]][k - 1]; + fa[v][k] = fa[fa[v][k - 1]][k - 1]; } } } } } -int lca(int a, int b) { - if (dep[a] < dep[b]) swap(a, b); +int lca(int x, int y) { + if (dep[x] < dep[y]) std::swap(x, y); + for (int k = 19; k >= 0; k--) { - if (dep[fa[a][k]] >= dep[b]) { - a = fa[a][k]; + if (dep[fa[x][k]] >= dep[y]) { + x = fa[x][k]; } } - if (a == b) return a; + + if (x == y) return x; + for (int k = 19; k >= 0; k--) { - if (fa[a][k] != fa[b][k]) { - a = fa[a][k]; - b = fa[b][k]; + if (fa[x][k] != fa[y][k]) { + x = fa[x][k]; + y = fa[y][k]; } } - return fa[a][0]; + + return fa[x][0]; } int main() { - cin >> n >> m >> root; - for (int i = 1; i < n; i++) { - cin >> a >> b; - tr[a].push_back(b); - tr[b].push_back(a); + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m >> s; + + for (int i = 1, u, v; i < n; i++) { + cin >> u >> v; + + g[u].emplace_back(v); + g[v].emplace_back(u); } - bfs(root); - for (int i = 0; i < m; i++) { + + bfs(s); + + while (m--) { + int a, b; + cin >> a >> b; + cout << lca(a, b) << endl; } + return 0; }