[Hiho 15] Recent public Ancestor II

Source: Internet
Author: User

Title Description

This time the offline algorithm was used to solve the problem of the recent public ancestor.

The off-line algorithm can complete all queries once DFS is processed, so the queries need to be stored all together.

The specific DFS process is:

All nodes are initially marked as white, and the first time it passes through the node, it is dyed gray and dyed black the second time it passes through the node (that is, when it leaves the node).

In a state of Dfs, white represents an unreachable node, and black means that the node has been accessed to the root of the entire subtree, and the gray represents the subtree that is accessing the node as the root.

All queries related to that node can be queried at each node, and each query corresponds to another node x (possibly itself):

If x is white, the description has not been accessed, the information is not complete, and then processed;

If X is gray, indicating that the current node is an element in the X subtree, then their nearest public ancestor is X;

If x is black, you can look for a recent gray ancestor node y of x, the current node is an element in the subtree of Y, and the nearest public ancestor of X and the current node is Y.

How do I query all queries related to a particular node? You can use vectors to save them.

How to find a nearest gray ancestor node of x? You can use and check the set.

With this approach, each query is actually processed once (white, black/gray) or two times (gray, black). The complexity is O (m) (m is the query amount).

#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 root[max_ Hash];int color[max_hash];int query[max][2];vector<int> i_query[max_hash];int ans[MAX];int n;unsigned int    Bkdrhash (char *str) {unsigned int 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;} int get_root (int i) {return color[i] = = 1? i:root[i] = Get_root (Root[i]);}  void Dfs (int root_idx) {Color[root_idx] = 1;int len = i_query[root_idx].size (); for (int i = 0; i < len; i++) {int Q_idx = I_query[root_idx][i];int another = root_idx = = Query[q_idx][0]? QUERY[Q_IDX][1]: query[q_idx][0];if (color[another] = = 1) {Ans[q_idx] = another;} else if (color[another] = = 2 && ans[q_idx] = = 0) {Ans[q_idx] = Get_root (another);}} int son_len = Sons[root_idx].size (); for (int i = 0; i < Son_len; i++) {DFS (sons[root_idx][i]);} COLOR[ROOT_IDX] = 2;} 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); Root[i2] = I1;start = I1;while (--n) {cin >> s1 >> s2;i1 = Get_index (S1); i2 = Get_index (s2); Sons[i1].push_back (I2); Root[i2] = I1;} CIN >> n;for (int i = 0; i < n; i++) {cin >> s1 >> s2;i1 = Get_index (s1); i2 = Get_index (s2); query[i][ 0] = i1;query[i][1] = I2;i_query[i1].push_back (i); I_query[i2].push_back (i);} DFS (start); for (int i = 0; i < n; i++) {cout << names[ans[i]] << Endl;} return 0;}

[Hiho 15] Recent public ancestor two

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.