[Hiho 17] recent public ancestor three

Source: Internet
Author: User

Title Description

This is the use of online algorithms to solve this problem.

The nearest public ancestor of the two nodes is the lightest node on the path of the two nodes.

You can go through the deep search to turn the tree into a group: put it into an array each time it passes through a node, either from the parent node or from a child node. Also record the last occurrence of each node in the array.

Use the RMQ-ST algorithm to pre-calculate the least-depth node number in the 2^k length interval.

For each query, convert it to two interval segments to solve.

#include <iostream> #include <algorithm> #include <cstring> #include <vector> #define MAX 100005 #define Max_hash 200005using namespace Std;char names[max_hash][50];vector<int> sons[max_hash];int last_ Occurance[max_hash];int dpth[max_hash];int Data[max * 2][25];int idx, n, q;unsigned int bkdrhash (char *str) {unsigned I    NT seed = 131;    unsigned int hash = 0;    while (*STR) {hash = hash * seed + (*str++); } return (hash & 0x7FFFFFFF)% Max_hash;} int Get_index (char *str) {int res = Bkdrhash (str), while (Strlen (names[res])! = 0 && strcmp (str, names[res])) {res+ +;if (Res > Max_hash) Res-= Max_hash;} if (strlen (names[res]) = = 0) {strcpy (names[res], str);} return res;} void Dfs (int root_idx, int depth) {Dpth[root_idx] = depth;/*cout << names[root_idx] << depth << endl;*/d Ata[idx][0] = Root_idx;last_occurance[root_idx] = Idx;idx++;int Sons_len = Sons[root_idx].size (); for (int i = 0; I < so Ns_len; i++) {DFS (Sons[roOt_idx][i], depth + 1);d ata[idx][0] = root_idx;last_occurance[root_idx] = idx;idx++;}} void Pre_calc () {//for (int i = 0; i < idx; i++) {//cout << i << ":" << names[data[i][0]] << en Dl;//}int t = idx;int x = 0;while (t) {x++;t >>= 1;}  x--;for (int i = 1; i <= x; i++) {for (int j = 0; J <= Idx-(1 << i); j + +) {Data[j][i] = Dpth[data[j][i-1]] < Dpth[data[j + (1 << (i-1))][i-1]? DATA[J][I-1]: Data[j + (1 << (i-1))][i-1];//cout << J << '-' << j + (1 << i)-1 < < ': ' << names[data[j][i]] << Endl;}} int main () {char s1[50], s2[50];int i1, I2;int start;cin >> n;cin >> s1 >> s2;i1 = Get_index (s1); i2 = ge T_index (S2); Sons[i1].push_back (i2); start = I1;while (--n) {cin >> s1 >> s2;i1 = Get_index (s1); i2 = Get_index (s 2); Sons[i1].push_back (I2);} DFS (start, 0);p Re_calc (); Cin >> Q;while (q--) {cin >> s1 >> s2;i1 = Last_occurance[get_index (S1)];I2 = Last_occurance[get_index (s2)];if (I1 > I2) {int t = I1;i1 = I2;i2 = t;} int len = i2-i1 + 1;int x = 0;while (len) {X++;len >>= 1;} X--;len = 1 << x;cout << Names[dpth[data[i1][x]] < Dpth[data[i2-len + 1][x]]? Data[i1][x]: Data[i2-le n + 1][x]] << Endl;} return 0;}

[Hiho 17] Recent public ancestor three

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.