LCA - Lowest Common Ancestor

打印 上一主题 下一主题

主题 844|帖子 844|积分 2532

LCA - Lowest Common Ancestor

https://www.luogu.com.cn/problem/SP14932
题目描述

A tree is an undirected graph in which any two vertices are connected by exactly one simple path. In other words, any connected graph without cycles is a tree. - Wikipedia
The lowest common ancestor (LCA) is a concept in graph theory and computer science. Let T be a rooted tree with N nodes. The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself). - Wikipedia
Your task in this problem is to find the LCA of any two given nodes v and w in a given tree T.

For example the LCA of nodes 9 and 12 in this tree is the node number 3.
Input

The first line of input will be the number of test cases. Each test case will start with a number N the number of nodes in the tree, 1 <= N <= 1,000. Nodes are numbered from 1 to N. The next N lines each one will start with a number M the number of child nodes of the Nth node, 0 <= M <= 999 followed by M numbers the child nodes of the Nth node. The next line will be a number Q the number of queries you have to answer for the given tree T, 1 <= Q <= 1000. The next Q lines each one will have two number v and w in which you have to find the LCA of v and w in T, 1 <= v, w <= 1,000.
Input will guarantee that there is only one root and no cycles.
Output

For each test case print Q + 1 lines, The first line will have “Case C:” without quotes where C is the case number starting with 1. The next Q lines should be the LCA of the given v and w respectively.
Example

Input:

  1. 1
  2. 7
  3. 3 2 3 4
  4. 0
  5. 3 5 6 7
  6. 0
  7. 0
  8. 0
  9. 0
  10. 2
  11. 5 7
  12. 2 7
复制代码
Output:

  1. Case 1:
  2. 3
  3. 1
复制代码
输入格式


输出格式


代码

倍增算法

  1. #include <bits/stdc++.h>
  2. #define endl "\n"
  3. using namespace std;
  4. vector<int> dep;         // 存储深度
  5. vector<vector<int>> fa;  // 存储跳跃点
  6. vector<vector<int>> e;   // 存储边
  7. void dfs(int x, int y) {
  8.   for (int i = 1; i <= 9; i++) {
  9.     fa[x][i] = fa[fa[x][i - 1]][i - 1];
  10.   }
  11.   for (auto i : e[x]) {
  12.     dfs(i, x);
  13.   }
  14. }
  15. int lca(int u, int v) {
  16.   if (dep[u] < dep[v]) swap(u, v);
  17.   for (int i = 9; i >= 0; i--) {
  18.     if (dep[fa[u][i]] >= dep[v]) u = fa[u][i];
  19.   }
  20.   if (u == v) return v;
  21.   for (int i = 9; i >= 0; i--) {
  22.     if (fa[u][i] != fa[v][i]) {
  23.       u = fa[u][i];
  24.       v = fa[v][i];
  25.     }
  26.   }
  27.   return fa[u][0];
  28. }
  29. void solve() {
  30.   int N;  // 节点数
  31.   cin >> N;
  32.   dep    = vector<int>(N + 1);
  33.   fa     = vector<vector<int>>(N + 1, vector<int>(10, 0));
  34.   e      = vector<vector<int>>(N + 1);
  35.   dep[1] = 1;
  36.   for (int i = 1; i <= N; i++) {
  37.     int sonNum;  // 子节点数量
  38.     cin >> sonNum;
  39.     while (sonNum--) {
  40.       int sonNode;
  41.       cin >> sonNode;
  42.       e[i].push_back(sonNode);
  43.       fa[sonNode][0] = i;
  44.       dep[sonNode]   = dep[i] + 1;
  45.     }
  46.   }
  47.   dfs(1, 0);
  48.   int queryNum;
  49.   cin >> queryNum;  // 查询次数
  50.   while (queryNum--) {
  51.     int u, v;
  52.     cin >> u >> v;
  53.     cout << lca(u, v) << endl;
  54.   }
  55. }
  56. int main() {
  57.   ios::sync_with_stdio(false);
  58.   cin.tie(nullptr);
  59.   int T;
  60.   cin >> T;
  61.   for (int i = 1; i <= T; i++) {
  62.     cout << "Case " << i << ":" << endl;
  63.     solve();
  64.   }
  65.   return 0;
  66. }
复制代码
tarjan算法

  1. #include <bits/stdc++.h>
  2. #define endl "\n"
  3. using namespace std;
  4. vector<bool> vis;                      // 存储是否访问
  5. vector<int> fa;                        // 存储父节点
  6. vector<vector<int>> e;                 // 存储子节点
  7. vector<vector<pair<int, int>>> query;  // 需要查询的
  8. vector<int> ans;                       // 存储答案
  9. int find(int x) {
  10.   if (x == fa[x]) return x;
  11.   return fa[x] = find(fa[x]);
  12. }
  13. void tarjan(int x) {
  14.   vis[x] = true;
  15.   for (auto son : e[x]) {
  16.     if (!vis[son]) {
  17.       tarjan(son);
  18.       fa[son] = x;
  19.     }
  20.   }
  21.   for (auto q : query[x]) {
  22.     int y = q.first, id = q.second;
  23.     if (vis[y]) {
  24.       ans[id] = find(y);
  25.     }
  26.   }
  27. }
  28. void solve() {
  29.   int N;  // 节点数
  30.   cin >> N;
  31.   fa = vector<int>(N + 1);
  32.   e  = vector<vector<int>>(N + 1);
  33.   query.resize(N + 1);
  34.   vis = vector<bool>(N + 1, false);
  35.   for (int i = 1; i <= N; i++) {
  36.     fa[i] = i;
  37.   }
  38.   // 输入子节点
  39.   for (int i = 1; i <= N; i++) {
  40.     int sonNum;
  41.     cin >> sonNum;
  42.     while (sonNum--) {
  43.       int sonNode;
  44.       cin >> sonNode;
  45.       e[i].push_back(sonNode);
  46.     }
  47.   }
  48.   int queryNum;
  49.   cin >> queryNum;
  50.   ans = vector<int>(queryNum + 1);
  51.   for (int i = 1; i <= queryNum; i++) {
  52.     int u, v;
  53.     cin >> u >> v;
  54.     query[u].push_back({ v, i });
  55.     query[v].push_back({ u, i });
  56.   }
  57.   tarjan(1);
  58.   for (int i = 1; i <= queryNum; i++) {
  59.     cout << ans[i] << endl;
  60.   }
  61. }
  62. int main() {
  63.   ios::sync_with_stdio(false);
  64.   cin.tie(nullptr);
  65.   int T;
  66.   cin >> T;
  67.   for (int i = 1; i <= T; i++) {
  68.     cout << "Case " << i << ":" << endl;
  69.     solve();
  70.   }
  71.   return 0;
  72. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

大号在练葵花宝典

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表