A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:In the figure, each node is labeled with an integer from {1, 2,...,16}.
Node 8 is the root of the tree. Node x is an ancestor of node y if node x is in the path between the root and node y. For example, node 4 is an ancestor of node 16. Node 10 is also an ancestor of node 16. As a matter of fact, nodes 8, 4, 10, and 16 are the ancestors of node 16. Remember that a node is an ancestor of itself. Nodes 8, 4, 6, and 7 are the ancestors of node 7. A node x is called a common ancestor of two different nodes y and z if node x is an ancestor of node y and an ancestor of node z. Thus, nodes 8 and 4 are the common ancestors of nodes 16 and 7. A node x is called the nearest common ancestor of nodes y and z if x is a common ancestor of y and z and nearest to y and z among their common ancestors. Hence, the nearest common ancestor of nodes 16 and 7 is node 4. Node 4 is nearer to nodes 16 and 7 than node 8 is. For other examples, the nearest common ancestor of nodes 2 and 3 is node 10, the nearest common ancestor of nodes 6 and 13 is node 8, and the nearest common ancestor of nodes 4 and 12 is node 4. In the last example, if y is an ancestor of z, then the nearest common ancestor of y and z is y. Write a program that finds the nearest common ancestor of two distinct nodes in a tree. Input The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case starts with a line containing an integer N , the number of nodes in a tree, 2<=N<=10,000. The nodes are labeled with integers 1, 2,..., N. Each of the next N -1 lines contains a pair of integers that represent an edge --the first integer is the parent node of the second integer. Note that a tree with N nodes has exactly N - 1 edges. The last line of each test case contains two distinct integers whose nearest common ancestor is to be computed. Output Print exactly one line for each test case. The line should contain the integer that is the nearest common ancestor. Sample Input 2 16 1 14 8 5 10 16 5 9 4 6 8 4 4 10 1 13 6 15 10 11 6 7 10 2 16 3 8 1 16 12 16 7 5 2 3 3 4 3 1 1 5 3 5 Sample Output 4 3 Source |
题目类型:最近公共祖先
算法分析:使用一个parent数组来记录节点的父节点的标号,再使用一个cnt数组记录节点所在树的层数,对于输入中的两个节点,每次首先上寻所在树的层数大的节点的父节点,然后判断另一个节点的父节点是否是该节点。相同层数则上寻任意一个节点的父节点。直到找到满足条件的父节点并输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
#include <iostream> #include <fstream> using namespace std; const int maxn = 10000 + 6; int par[maxn]; int cnt[maxn]; int main() { // ifstream cin ("aaa.txt"); int t; int val_a, val_b; cin >> t; while (t--) { int n; cin >> n; int i, a, b; for (i = 0; i < maxn; i++) { par[i] = -1; cnt[i] = 1; } for (i = 0; i < n - 1; i++) { cin >> a >> b; par[b] = a; } cin >> val_a >> val_b; for (i = 0; i < maxn; i++) if (par[i] != -1) { int next = par[i]; while (next != -1) { cnt[i]++; next = par[next]; } } while (1) { if (cnt[val_a] == cnt[val_b]) { val_a = par[val_a]; } else { if (cnt[val_a] > cnt[val_b]) { if (par[val_a] == val_b) { cout << val_b << endl; break; } else val_a = par[val_a]; } else if (cnt[val_a] < cnt[val_b]) { if (par[val_b] == val_a) { cout << val_a << endl; break; } else val_b = par[val_b]; } } } } return 0; } 使用LCA求解 代码: #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r using namespace std; const int INF = 0x7FFFFFFF; const int MOD = 1000000000 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 10000 + 66; int head[maxn], ind[maxn], parent[maxn], ans[maxn]; int tot, vala, valb, n; bool visited[maxn]; struct Node { int to; int next; }edge[maxn]; void AddEdge (int a, int b) { edge[tot].to = b; edge[tot].next = head[a]; head[a] = tot++; } int UnFind (int val) { if (parent[val] == val) return val; return parent[val] = UnFind (parent[val]); } void Merge (int x, int y) { int a = UnFind (x); int b = UnFind (y); if (a == b) return ; parent[b] = a; } void Init () { tot = 0; memset (head, -1, sizeof (head)); memset (ind, 0, sizeof (ind)); memset (visited, 0, sizeof (visited)); for (int i = 1; i <= n; i++) parent[i] = i; } void LCA (int rt) { ans[rt] = rt; for(int i = head[rt]; i != -1; i = edge[i].next) { LCA (edge[i].to); Merge (rt, edge[i].to); ans[UnFind(rt)] = rt; //有可能rt并不是新树的根,所以要加这句 } visited[rt] = true; if (rt == vala) { if (visited[valb]) printf ("%d\n", ans[UnFind(valb)]); } else if (rt == valb) { if (visited[vala]) printf("%d\n", ans[UnFind(vala)]); } } int main() { // ifstream cin("aaa.txt"); int t; cin >> t; while (t--) { cin >> n; Init (); for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; AddEdge (u, v); ind[v]++; } cin >> vala >> valb; for (int i = 1; i <= n; i++) if (!ind[i]) LCA (i); } return 0; } |
- « 上一篇:poj1325
- poj1365:下一篇 »