HDU 4547 CD操作 (LCA最近公共祖先Tarjan模版)
CD操作
倍增法 https://i.cnblogs.com/EditPosts.aspx?postid=8605845
Time Limit : 10000/5000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 4 Accepted Submission(s) : 3
这里我们简化一下问题,假设只有一个根目录,CD操作也只有两种方式:
1.
CD 当前目录名\...\目标目录名 (中间可以包含若干目录,保证目标目录通过绝对路径可达)
2. CD ..
(返回当前目录的上级目录)
现在给出当前目录和一个目标目录,请问最少需要几次CD操作才能将当前目录变成目标目录?
B(目录名是只含有数字或字母,长度小于40的字符串),表示A的父目录是B。最后M行每行两个目录名A
B,表示询问将当前目录从A变成B最少要多少次CD操作。数据保证合法,一定存在一个根目录,每个目录都能从根目录访问到。
3 1
B A
C A
B C
3 2
B A
C B
A C
C A
1
2
- 从父目录到任意一个子目录的距离都是1
- 用一个flag记录一下询问是从左到右还是从右到左,有几种情况
- 左边是右边的父亲,距离为1
- 右边是左边的父亲,距离是dis[v]到lca(u,v)
- 其他情况+1
其他就是离线求Tarjan的lca了
#include<iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
using namespace std;
typedef long long ll;
#define inf 1000000
#define mem(a,b) memset(a,b,sizeof(a))
const int N=+;
const int M=*N;
int pre[N],first[N],first2[N],tot,tot2;
bool vis[N];//标记有没有询问
int n;
int fa[N],ans[N],cnt;
int vis2[N],dis[N];
map<string,int>m; struct edge
{
int v,next;
} e[M];
struct Query
{
int v,next,id,flag;//flag用来标记是从左到右还是从到左
} query[M]; void add(int u,int v)
{
e[tot].v=v;
e[tot].next =first[u];
first[u]=tot++;
}
void dfs(int u,int len)//建立深度
{
vis2[u]=;
dis[u]=len;
for(int i=first[u];~i;i=e[i].next)
{
int v=e[i].v;
if(!vis2[v])
dfs(v,len+);
}
} void add_query(int u,int v,int id,int flag)//建立询问的边
{
query[tot2].flag =flag;
query[tot2].id=id;
query[tot2].v=v;
query[tot2].next=first2[u];
first2[u]=tot2++;
} int find(int x)
{
return x==pre[x]?x:pre[x]=find(pre[x]);
} void lca(int u,int fa)//Targin算法
{
for(int i=first[u];~i;i=e[i].next)
{
int v=e[i].v;
if(v==fa) continue;
lca(v,u);
pre[v]=u;
}
vis[u]=;
for(int i=first2[u];~i;i=query[i].next)
{
int v=query[i].v;
if(vis[v])//那么find(v)就是最近公共祖先
{
int id=query[i].id;
int flag=query[i].flag ;
if(flag==)
{
if(u==find(v))
{
ans[id]=;//直接跃上找到父目录
}
else if(find(u)==v)
ans[id]=dis[u]-dis[find(v)];//向下走找到目标目录
else
ans[id]=dis[u]-dis[find(v)]+;//一个越到父目录,再向下找
}
else//
{
int u1=v;
int v1=u;
if(u1==find(v1))
ans[id]=;
else if(find(u1)==v1)
ans[id]=dis[u1]-dis[find(v)];
else
ans[id]=dis[u1]-dis[find(v)]+;
}
}
}
} void init()
{
mem(first,-);
mem(first2,-);
mem(vis,);
mem(vis2,);
mem(fa,-);
mem(ans,);
tot=;
tot2=;
cnt=;
m.clear();
for(int i=; i<=n; i++)
pre[i]=i;
} struct node
{
int u,v;
} zz[N]; int main()
{
int t,mm;
string s1,s2;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&mm);
init();
for(int i=; i<n; i++)
{
cin>>s1>>s2;
if(!m[s1]) m[s1]=cnt++;
if(!m[s2]) m[s2]=cnt++;
int a=m[s1],b=m[s2];
add(a,b);
add(b,a);
fa[a]=b;
}
int root=;
while(~fa[root]) root=fa[root];
dfs(root,);
for(int i=; i<=mm; i++)
{
cin>>s1>>s2;
int a=m[s1],b=m[s2];
zz[i].u=a,zz[i].v=b;
add_query(a,b,i,);
add_query(b,a,i,);
}
lca(root,root);
for(int i=; i<=mm; i++)
{
int u=zz[i].u,v=zz[i].v;
if(u==v)
puts("");
else
printf("%d\n",ans[i]);
}
}
return ;
}
HDU 4547 CD操作 (LCA最近公共祖先Tarjan模版)的更多相关文章
- LCA 最近公共祖先 tarjan离线 总结 结合3个例题
在网上找了一些对tarjan算法解释较好的文章 并加入了自己的理解 LCA(Least Common Ancestor),顾名思义,是指在一棵树中,距离两个点最近的两者的公共节点.也就是说,在两个点通 ...
- LCA 最近公共祖先 Tarjan(离线)算法的基本思路及其算法实现
首先是最近公共祖先的概念(什么是最近公共祖先?): 在一棵没有环的树上,每个节点肯定有其父亲节点和祖先节点,而最近公共祖先,就是两个节点在这棵树上深度最大的公共的祖先节点. 换句话说,就是两个点在这棵 ...
- LCA最近公共祖先——Tarjan模板
LCA(Lowest Common Ancestors),即最近公共祖先,是指在有根树中,找出某两个结点u和v最近的公共祖先. Tarjan是一种离线算法,时间复杂度O(n+Q),Q表示询问次数,其中 ...
- 【HDU 4547 CD操作】LCA问题 Tarjan算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4547 题意:模拟DOS下的cd命令,给出n个节点的目录树以及m次查询,每个查询包含一个当前目录cur和 ...
- HDU 4547 CD操作
传送门 没啥好说的.就是一个LCA. 不过就是有从根到子树里任意一个节点只需要一次操作,特判一下LCA是不是等于v.相等的话不用走.否则就是1次操作. 主要是想写一下倍增的板子. 倍增基于二进制.暴力 ...
- LCA最近公共祖先 Tarjan离线算法
学习博客: http://noalgo.info/476.html 讲的很清楚! 对于一颗树,dfs遍历时,先向下遍历,并且用并查集维护当前节点和父节点的集合.这样如果关于当前节点(A)的关联节点( ...
- LCA(最近公共祖先)专题(不定期更新)
Tarjan(离线)算法 思路: 1.任选一个点为根节点,从根节点开始. 2.遍历该点u所有子节点v,并标记这些子节点v已被访问过. 3.若是v还有子节点,返回2,否则下一步. 4.合并v到u上. 5 ...
- Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载)
Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载) 转载自:http://hi.baidu.com/lydrainbowcat/blog/item/2 ...
- lca 最近公共祖先
http://poj.org/problem?id=1330 #include<cstdio> #include<cstring> #include<algorithm& ...
随机推荐
- NumPy使用 Matplotlib 绘制直方图
NumPy - 使用 Matplotlib 绘制直方图 NumPy 有一个numpy.histogram()函数,它是数据的频率分布的图形表示. 水平尺寸相等的矩形对应于类间隔,称为bin,变量hei ...
- spring mvc: 页面重定向调整
我的项目名称是hello, 在src/main/java目录下面建了一个chapter2目录 有三个配置文件: web.xml, chapter2-servlet.xml, applicationCo ...
- nyoj-1316-二分
acm.nyist.edu.cn/JudgeOnline/problem.php?pid=1316题目链接 Interval 时间限制:2000 ms | 内存限制:65535 KB 难度:3 描 ...
- LeetCode OJ:Search a 2D Matrix(二维数组查找)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- Python基础学习(第6天)
1.zip函数 1)zip函数在只有一个参数时运作的方式. x = [1, 2, 3] x = zip(x) print x输出:[(1,), (2,), (3,)] 2)zip函数在没有参数时运作的 ...
- 2017.11.2 Talk to customers for an hour
yesterday::: Hi Huang, For the better performance of the test the Con 6 should be connected all the ...
- 【python】imp模块的使用
是import在程序中的使用 [一]函数load_source imp.load_source(moduleName, sourceFile) 使用: abc = imp.load_source('d ...
- QGrapicsScene类
概述 QgraphicsScene类为管理大量的2D图形item提供了一个管理界面,做为item的容器,它配合使用QgraphicsView使用来观察items,例如线,矩形,文本或者自定义的item ...
- asp.net 禁止回车输入
//只在输入框禁止输入回车 if(event.keyCode==13&&event.srcElement.type=="textarea") { ...
- PenMount Touch显示鼠标指针驱动安装
/******************************************************************************* * PenMount Touch显示鼠 ...