最近公共祖先 朴素 离线 在线 算法合集

来源:互联网 发布:卫计委数据平台 编辑:程序博客网 时间:2024/05/07 21:04

描述

小Ho最近发现了一个神奇的网站!虽然还不够像58同城那样神奇,但这个网站仍然让小Ho乐在其中,但这是为什么呢?

“为什么呢?”小Hi如是问道,在他的观察中小Ho已经沉迷这个网站一周之久了,甚至连他心爱的树玩具都弃置一边。

“嘿嘿,小Hi,你快过来看!”小Ho招呼道。

“你看,在这个对话框里输入我的名字,在另一个对话框里,输入你的名字,再点这个查询按钮,就可以查出来……什么!我们居然有同一个祖祖祖祖祖爷爷?”

“诶,真是诶……这个网站有点厉害啊。”小Hi不由感叹道。

“是啊,这是什么算法啊,这么厉害!”小Ho也附和道。

“别2,我说的是他能弄到这些数据很厉害,而人类的繁殖树这种层数比较浅的树对这类算法的要求可是简单的不得了,你都能写出来呢!”小Hi道。

“啊?我也能写出来?可是……该从哪开始呢?”小Ho困惑了。

小Ho要面临的问题是这样的,假设现在他知道了N个人的信息——他们的父亲是谁,他需要对于小Hi的每一次提问——两个人的名字,告诉小Hi这两个人的是否存在同一个祖先,如果存在,那么他们的所有共同祖先中辈分最低的一个是谁?

输入

每个测试点(输入文件)有且仅有一组测试数据。

每组测试数据的第1行为一个整数N,意义如前文所述。

每组测试数据的第2~N+1行,每行分别描述一对父子关系,其中第i+1行为两个由大小写字母组成的字符串Father_i, Son_i,分别表示父亲的名字和儿子的名字。

每组测试数据的第N+2行为一个整数M,表示小Hi总共询问的次数。

每组测试数据的第N+3~N+M+2行,每行分别描述一个询问,其中第N+i+2行为两个由大小写字母组成的字符串Name1_i, Name2_i,分别表示小Hi询问中的两个名字。

对于100%的数据,满足N<=10^2,M<=10^2, 且数据中所有涉及的人物中不存在两个名字相同的人(即姓名唯一的确定了一个人)。

输出

对于每组测试数据,对于每个小Hi的询问,输出一行,表示查询的结果:如果根据已知信息,可以判定询问中的两个人存在共同的祖先,则输出他们的所有共同祖先中辈分最低的一个人的名字,否则输出-1。

样例输入
11JiaYan JiaDaihuaJiaDaihua JiaFuJiaDaihua JiaJingJiaJing JiaZhenJiaZhen JiaRongJiaYuan JiaDaishanJiaDaishan JiaSheJiaDaishan JiaZhengJiaShe JiaLianJiaZheng JiaZhuJiaZheng JiaBaoyu3JiaBaoyu JiaLianJiaBaoyu JiaZhengJiaBaoyu LinDaiyu
样例输出
JiaDaishanJiaZheng-1

1、朴素算法

#pragma comment(linker, "/STACK:102400000,102400000")#include <iostream>#include <vector>#include <string>#include <algorithm>#include <cstdio>#include <cstring>#include <ctime>#include <cstdlib>#include <set>#include <map>#include <cctype>#include <list>#include <cmath>#include <bitset>#include <queue>#include <stack>#include <sstream>#include <functional>#include <cassert>using namespace std;int RL(char _str[], int _maxlen = 1e8) {if (fgets(_str, _maxlen, stdin) == NULL) return -1;int i = 0;while (_str[i]) ++i;if (_str[i - 1] == '\n') _str[--i] = 0;return i;}inline int RI() {int temp;scanf("%d", &temp);return temp;}inline long long RLL() {long long temp;scanf("%I64d", &temp);return temp;}inline double RD() {double temp;scanf("%lf", &temp);return temp;}template <class T1, class T2>inline void Max(T1 &a, T2 b) { if (b > a) a = b; }template <class T1, class T2>inline void Min(T1 &a, T2 b) { if (b < a) a = b; }typedef pair<int, int> pii;typedef pair<double, double> pdd;typedef long long LL;const int inf = 0x3f3f3f3f;const LL INF = 0x3f3f3f3f3f3f3f3f;const double eps = 1e-8;const int mod = 100007;const int maxn = 100 + 9;int N, M;int main() {//std::ios::sync_with_stdio(0);//std::cin.tie(0);#ifdef NIGHT_13freopen("in.txt", "r", stdin);//freopen("myout.txt", "w", stdout);#endifint CAS = 0;int id = 1;map<string, string> par;scanf("%d", &N);while (N--) {char p1[100], p2[100];scanf("%s%s", p1, p2);par[p2] = p1;}scanf("%d", &M);while (M--) {char p1[100], p2[100];scanf("%s%s", p1, p2);string a(p1), b(p2);set<string> fa;fa.insert(a);while (par.find(a) != par.end()) {fa.insert(par[a]);a = par[a];}while (fa.find(b) == fa.end() && par.find(b) != par.end()) {b = par[b];}if (fa.find(b) != fa.end()) puts(b.c_str());else puts("-1");}return 0;}


2、离线算法(tarjan)

#pragma comment(linker, "/STACK:102400000,102400000")#include <iostream>#include <vector>#include <string>#include <algorithm>#include <cstdio>#include <cstring>#include <ctime>#include <cstdlib>#include <set>#include <map>#include <cctype>#include <list>#include <cmath>#include <bitset>#include <queue>#include <stack>#include <sstream>#include <functional>#include <cassert>using namespace std;int RL(char _str[], int _maxlen = 1e8) {if (fgets(_str, _maxlen, stdin) == NULL) return -1;int i = 0;while (_str[i]) ++i;if (_str[i - 1] == '\n') _str[--i] = 0;return i;}inline int RI() {int temp;scanf("%d", &temp);return temp;}inline long long RLL() {long long temp;scanf("%I64d", &temp);return temp;}inline double RD() {double temp;scanf("%lf", &temp);return temp;}template <class T1, class T2>inline void Max(T1 &a, T2 b) { if (b > a) a = b; }template <class T1, class T2>inline void Min(T1 &a, T2 b) { if (b < a) a = b; }typedef pair<int, int> pii;typedef pair<double, double> pdd;typedef long long LL;const int inf = 0x3f3f3f3f;const LL INF = 0x3f3f3f3f3f3f3f3f;const double eps = 1e-8;const int mod = 100007;const int maxn = 1e5 + 9;int N, M, par[maxn];map<string, int> id;string name[maxn];vector<int> G[maxn];vector<pii> Q[maxn];int lca[maxn];int Find(int x) {if (par[x] == x) return x;return par[x] = Find(par[x]);}void LCA_Tarjan(int x, int p) {par[x] = x;for (int i = 0; i < G[x].size(); ++i) {if (G[x][i] != p) LCA_Tarjan(G[x][i], x);}for (int i = 0; i < Q[x].size(); ++i) {if (par[Q[x][i].first]) lca[Q[x][i].second] = Find(Q[x][i].first);}par[x] = p;}int main() {//std::ios::sync_with_stdio(0);//std::cin.tie(0);#ifdef NIGHT_13freopen("in.txt", "r", stdin);//freopen("myout.txt", "w", stdout);#endifint CAS = 0;int _id = 0;scanf("%d", &N);while (N--) {char p1[100], p2[100];scanf("%s%s", p1, p2);int ida, idb;if (!(ida = id[p1])) { ida = id[p1] = ++_id; name[_id] = p1; }if (!(idb = id[p2])) { idb = id[p2] = ++_id; name[_id] = p2; }G[ida].push_back(idb);G[idb].push_back(ida);}scanf("%d", &M);for (int i = 1; i <= M; ++i) {char p1[100], p2[100];scanf("%s%s", p1, p2);int a = id[p1], b = id[p2];Q[a].push_back(pii(b, i));Q[b].push_back(pii(a, i));}LCA_Tarjan(1, 0);for (int i = 1; i <= M; ++i) {puts(name[lca[i]].c_str());}return 0;}


3、在线算法(RMQ-ST)

#pragma comment(linker, "/STACK:102400000,102400000")#include <iostream>#include <vector>#include <string>#include <algorithm>#include <cstdio>#include <cstring>#include <ctime>#include <cstdlib>#include <set>#include <map>#include <cctype>#include <list>#include <cmath>#include <bitset>#include <queue>#include <stack>#include <sstream>#include <functional>#include <cassert>using namespace std;int RL(char _str[], int _maxlen = 1e8) {if (fgets(_str, _maxlen, stdin) == NULL) return -1;int i = 0;while (_str[i]) ++i;if (_str[i - 1] == '\n') _str[--i] = 0;return i;}inline int RI() {int temp;scanf("%d", &temp);return temp;}inline long long RLL() {long long temp;scanf("%I64d", &temp);return temp;}inline double RD() {double temp;scanf("%lf", &temp);return temp;}template <class T1, class T2>inline void Max(T1 &a, T2 b) { if (b > a) a = b; }template <class T1, class T2>inline void Min(T1 &a, T2 b) { if (b < a) a = b; }typedef pair<int, int> pii;typedef pair<double, double> pdd;typedef long long LL;const int inf = 0x3f3f3f3f;const LL INF = 0x3f3f3f3f3f3f3f3f;const double eps = 1e-8;const int mod = 100007;const int maxn = 1e5 + 9;int N, M, a[maxn*3], dp[20][maxn*3];map<string, int> id;string name[maxn];vector<int> G[maxn];int ix;void ToArr(int x, int p) {a[ix++] = x;for (int i = 0; i < G[x].size(); ++i) {if (G[x][i] != p) {ToArr(G[x][i], x);a[ix++] = x;}}}void InitRMQ() {ToArr(1, 0);for (int i = 0; i < ix; ++i) dp[0][i] = a[i];for (int i = 1; 1 << i <= ix; ++i) {for (int j = 0; j + (1 << i) <= ix; ++j) {dp[i][j] = min(dp[i - 1][j], dp[i - 1][j + (1 << (i - 1))]);}}}int RMQ(int l, int r) {if (l > r) swap(l, r);int k = log(r - l + 1) / log(2);return min(dp[k][l], dp[k][r - (1 << k) + 1]);}int main() {//std::ios::sync_with_stdio(0);//std::cin.tie(0);#ifdef NIGHT_13freopen("in.txt", "r", stdin);//freopen("myout.txt", "w", stdout);#endifint CAS = 0;int _id = 0;scanf("%d", &N);while (N--) {char p1[100], p2[100];scanf("%s%s", p1, p2);int ida, idb;if (!(ida = id[p1])) { ida = id[p1] = ++_id; name[_id] = p1; }if (!(idb = id[p2])) { idb = id[p2] = ++_id; name[_id] = p2; }G[ida].push_back(idb);G[idb].push_back(ida);}InitRMQ();scanf("%d", &M);for (int i = 1; i <= M; ++i) {char p1[100], p2[100];scanf("%s%s", p1, p2);int pa = id[p1], pb = id[p2];int l = ix, r = ix;while (a[l] != pa) --l;while (a[r] != pb) --r;printf("%s\n", name[RMQ(l,r)].c_str());}return 0;}


0 0