From 6a52a362a85d7930ce84d55076f65a98b028dae6 Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Fri, 22 Oct 2021 11:41:57 +0800 Subject: [PATCH] =?UTF-8?q?1172.=20=E7=A5=96=E5=AD=99=E8=AF=A2=E9=97=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acwing.com/problem/content/submission/code_detail/8350088/ --- AcWing/1172/1172.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 AcWing/1172/1172.cpp diff --git a/AcWing/1172/1172.cpp b/AcWing/1172/1172.cpp new file mode 100644 index 00000000..ca3682fc --- /dev/null +++ b/AcWing/1172/1172.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +int n, m, root, a, b, p, dep[40005], fa[40005][16]; +vector tr[40005]; + +void bfs(int root) { + memset(dep, 0x3f, sizeof(dep)); + dep[0] = 0; + dep[root] = 1; + queue q; + q.push(root); + while (!q.empty()) { + int t = 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 k = 1; k <= 15; k++) { + fa[i][k] = fa[fa[i][k - 1]][k - 1]; + } + } + } + } +} + +int lca(int a, int b) { + if (dep[a] < dep[b]) swap(a, b); + for (int k = 15; k >= 0; k--) { + if (dep[fa[a][k]] >= dep[b]) { + a = fa[a][k]; + } + } + if (a == b) return a; + for (int k = 15; k >= 0; k--) { + if (fa[a][k] != fa[b][k]) { + a = fa[a][k]; + b = fa[b][k]; + } + } + return fa[a][0]; +} + +int main() { + cin >> n; + for (int i = 0; i < n; i++) { + cin >> a >> b; + if (b == -1) { + root = a; + } else { + tr[a].push_back(b); + tr[b].push_back(a); + } + } + bfs(root); + cin >> m; + for (int i = 0; i < m; i++) { + cin >> a >> b; + p = lca(a, b); + if (p == a) { + cout << 1 << endl; + } else if (p == b) { + cout << 2 << endl; + } else { + cout << 0 << endl; + } + } + return 0; +}