您的位置:首页 > 其它

最近公共祖先LCA离线算法

2014-10-26 22:25 197 查看

    引用 hihocoder 题

   离线处理询问,在线算法也可以解决这个,不过是先建立树。

 而离线算法则是染色节点,加并查集实现。

链接:http://hihocoder.com/problemset/problem/1067

上面有详细的解释。

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<map>
#include<vector>
using namespace std;
const int max_n=5+1e5;
map<string,int > Namenum;
map<int,string> numName;
vector<int> G[max_n];
vector<int> Q[max_n];
string q1[max_n],q2[max_n],ans[max_n];
int fa[max_n];
int cnt=0;
int getnum(string s)
{
if(Namenum[s]==0)
{
Namenum[s]=(++cnt);
numName[cnt]=s;
}
return Namenum[s];
}
int Find(int u){
return fa[u]==u?u:fa[u]=Find(fa[u]);
}
void LCA(int u,int ac)
{
fa[u]=u;
for(int i=0;i<G[u].size();i++)
LCA(G[u][i],u);
for(int i=0;i<Q[u].size();i++)
{
int index=Q[u][i];
int w=Namenum[q1[index]]==u?Namenum[q2[index]]:Namenum[q1[index]];
if(fa[w]==-1) continue;
ans[index]=numName[Find(fa[w])];
}
fa[u]=Find(fa[ac]);
}
int main()
{
//freopen("in.txt","r",stdin);
int n,m;
cin>>n;
string u,v;
// Namenum.clear(),numName.clear();
for(int i=0;i<n;i++)
{
cin>>u>>v;
int x=getnum(u),y=getnum(v);
G[x].push_back(y);
}
cin>>m;
for(int i=0;i<m;i++)
{
cin>>q1[i]>>q2[i];
Q[Namenum[q1[i]]].push_back(i);
Q[Namenum[q2[i]]].push_back(i);
}
memset(fa,-1,sizeof(fa));
fa[0]=0;
LCA(1,0);
for(int i=0;i<m;i++)
cout<<ans[i]<<endl;
return 0;
}


另外一种写法:
  代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>

using namespace std;

const int N = 100005;
const int M = 200005;

int n, m;
int tot, head
, to
, next
;
int tot1, head1
, to1[M], id[M], next1[M];
int fa
, ans
;
char name[100005][100], name1[100], name2[100];
bool visit
;
map <string, int> ns;

void addEdge(const int& u, const int& v) {
to[tot] = v;
next[tot] = head[u];
head[u] = tot++;
}

void addEdge1(const int& u, const int& v, const int& i) {
to1[tot1] = v;
id[tot1] = i;
next1[tot1] = head1[u];
head1[u] = tot1++;
}

void addDoubleEdge(const int& u, const int& v, const int& i) {
addEdge1(u, v, i), addEdge1(v, u, i);
}

int find(const int& u) {
return fa[u] < 0 ? u : (fa[u] = find(fa[u]));
}

void tarjan(const int& u) {
visit[u] = true;
fa[u] = -1;
for (int i = head[u]; i != -1; i = next[i]) {
int v = to[i];
tarjan(v);
fa[v] = u;
}

for (int i = head1[u]; i != -1; i = next1[i]) {
int v = to1[i];
if (visit[v])
ans[id[i]] = find(v);
}
}

int main()
{
while (scanf("%d", &n) != EOF) {
memset(head, -1, sizeof(head));
memset(head1, -1, sizeof(head1));
tot = tot1 = 0;
ns.clear();
int cnt = 0;
for (int i = 0; i < n; ++i) {
scanf("%s %s", name1, name2);
string fa(name1), son(name2);
if (ns.find(fa) == ns.end())
strcpy(name[cnt], name1), ns[fa] = cnt++;
if (ns.find(son) == ns.end())
strcpy(name[cnt], name2), ns[son] = cnt++;
addEdge(ns[fa], ns[son]);
}
scanf("%d", &m);
for (int i = 0; i < m; ++i) {
scanf("%s %s", name1, name2);
string fa(name1), son(name2);
addDoubleEdge(ns[fa], ns[son], i);
}
memset(visit, false, sizeof(visit));
tarjan(0);
for (int i = 0; i < m; ++i)
printf("%s\n", name[ans[i]]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  LCA tarjin 公共祖先