给出一棵树,对于每一个询问,给出2个节点,输出2个节点的距离。

输入中有字母,那个是没有用的,不用管。

思路:

0.选择编号为1的节点作为树的root

(注意:有些题的边是单向的,这时候我们要根据节点的入度来确定root,

    双向的话一般可以随意选择一个节点作为root)

1.dfs1,求出dep和pa[i][0]

2.初始化数组pa

3.节点(u,v)的权值为w

 把本来是边的权值w赋给u,v中dep较大的节点,

 cost[i]表示节点i的权值为cost[i]

 先初始化:cost[root]=0

4.dfs2,求每一个节点到root的距离dis[i]

5.查询:dis(u,v)=dis[u]+dis[v]-2*dis[lca(u,v)];

 #include<cstdio>
#include<cstring> using namespace std; const int maxn=+;
const int inf=0x3f3f3f3f; inline int swap(int &x,int &y)
{
x^=y;
y^=x;
x^=y;
} struct Edge
{
int to,next;
};
Edge edge[maxn<<];
int head[maxn];
int tot=;
int cost[maxn];
int dis[maxn];
int pa[maxn][];
int dep[maxn];
int e[maxn][]; void init()
{
memset(head,-,sizeof head);
tot=;
memset(dep,,sizeof dep);
memset(pa,-,sizeof pa);
} void addedge(int u,int v)
{
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
} void dfs1(int u)
{
for(int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(!dep[v])
{
dep[v]=dep[u]+;
pa[v][]=u;
dfs1(v);
}
}
} void init_pa(int n)
{
for(int j=;(<<j)<=n;j++)
{
for(int i=;i<=n;i++)
{
if(pa[i][j-]!=-)
pa[i][j]=pa[pa[i][j-]][j-];
}
}
} void dfs2(int u,int pre)
{
for(int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(v==pre)
continue;
dis[v]=dis[u]+cost[v];
dfs2(v,u);
}
} int query(int a,int b,int n)
{
int init_a=a;
int init_b=b;
if(dep[a]<dep[b])
swap(a,b);
int cnt;
for(cnt=;dep[a]-(<<cnt)>=;cnt++)
;
cnt--;
for(int j=cnt;j>=;j--)
{
if(dep[a]-(<<j)>=dep[b])
a=pa[a][j];
}
if(a==b)
return dis[init_a]+dis[init_b]-*dis[a];
for(int i=cnt;i>=;i--)
{
if(pa[a][i]!=-&&pa[a][i]!=pa[b][i])
{
a=pa[a][i];
b=pa[b][i];
}
}
return dis[init_a]+dis[init_b]-*dis[pa[a][]];
} void solve(int n)
{
dep[]=;
dfs1();
init_pa(n);
cost[]=;
dis[]=;
for(int i=;i<=n;i++)
{
if(dep[e[i][]]>dep[e[i][]])
swap(e[i][],e[i][]);
cost[e[i][]]=e[i][];
}
dfs2(,-); int m;
scanf("%d",&m);
for(int i=;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
printf("%d\n",query(u,v,n));
}
return ;
} int main()
{
int n;
while(~scanf("%d",&n))
{
init();
int m;
scanf("%d",&m);
for(int i=;i<=m;i++)
{
int u,v,w;
char ch;
scanf("%d %d %d %c",&e[i][],&e[i][],&e[i][],&ch);
addedge(e[i][],e[i][]);
addedge(e[i][],e[i][]);
}
solve(n);
}
return ;
}

POJ 1986 DIstance Query LCA水题的更多相关文章

  1. POJ 1986 - Distance Queries - [LCA模板题][Tarjan-LCA算法]

    题目链接:http://poj.org/problem?id=1986 Description Farmer John's cows refused to run in his marathon si ...

  2. POJ.1986 Distance Queries ( LCA 倍增 )

    POJ.1986 Distance Queries ( LCA 倍增 ) 题意分析 给出一个N个点,M条边的信息(u,v,w),表示树上u-v有一条边,边权为w,接下来有k个询问,每个询问为(a,b) ...

  3. POJ 1986 Distance Queries LCA两点距离树

    标题来源:POJ 1986 Distance Queries 意甲冠军:给你一棵树 q第二次查询 每次你问两个点之间的距离 思路:对于2点 u v dis(u,v) = dis(root,u) + d ...

  4. poj 1986 Distance Queries LCA

    题目链接:http://poj.org/problem?id=1986 Farmer John's cows refused to run in his marathon since he chose ...

  5. POJ 1986 Distance Queries(LCA Tarjan法)

    Distance Queries [题目链接]Distance Queries [题目类型]LCA Tarjan法 &题意: 输入n和m,表示n个点m条边,下面m行是边的信息,两端点和权,后面 ...

  6. POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)

    POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description F ...

  7. poj 3080 Blue Jeans(水题 暴搜)

    题目:http://poj.org/problem?id=3080 水题,暴搜 #include <iostream> #include<cstdio> #include< ...

  8. poj 1986 Distance Queries 带权lca 模版题

    Distance Queries   Description Farmer John's cows refused to run in his marathon since he chose a pa ...

  9. POJ 1986 Distance Queries(Tarjan离线法求LCA)

    Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 12846   Accepted: 4552 ...

随机推荐

  1. hbase(ERROR: org.apache.hadoop.hbase.ipc.ServerNotRunningYetException: Server is not running yet)

    今天启动clouder manager集群时候hbase list出现 (ERROR: org.apache.hadoop.hbase.ipc.ServerNotRunningYetException ...

  2. HTML初讲

    整理老师所讲: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...

  3. 简单三步-实现dede站内搜索功能

    第一步:找到对应的搜索模板的代码 我们都知道,dede有自带的搜索功能,我们只要找到对应的模板,然后把我们想要的代码拿出来就行了.具体如下: 首先进入templets-->default--&g ...

  4. 记事本写JAVA程序

    编写程序源码: 1.新建记事本程序,修改文件名称为HelloWorld.java 打开编辑以下内容,保存. public class HelloWorld { public static void m ...

  5. HDU-4405 Aeroplane chess(概率DP求期望)

    题目大意:一个跳棋游戏,每置一次骰子前进相应的步数.但是有的点可以不用置骰子直接前进,求置骰子次数的平均值. 题目分析:状态很容易定义:dp(i)表示在第 i 个点出发需要置骰子的次数平均值.则状态转 ...

  6. Java——正则表达式(字符串操作)

     public class Test1 { /* * 正则表达式:对字符串的常见操作: * 1.匹配: *  其实是用的就是string类中的matches(匹配)方法. * 2.切割 *  其实 ...

  7. 利用CSS的@font-face属性 在网页中嵌入字体

    字体使用是网页设计中不可或缺的一部分.网页是文字的载体,我们希望在网页中使用某一特定字体,但是该字体并非主流操作系统的内置字体,这样用户在浏览页面的时候就有可能看不到真实的设计. 美工设计师最常做的办 ...

  8. Ubuntu 14.04 关于 TensorFlow 环境的配置

    Ubuntu 14.04 关于 TensorFlow 环境的配置   本教程截图于 TensorFlow 官方文档中文版  https://github.com/jikexueyuanwiki/ten ...

  9. Agent admitted failure to sign using the key

    SSH生成id_rsa, id_rsa.pub后,连接服务器却报: Agent admitted failure to sign using the key 错误. 解决方法: 在当前用户下执行命令: ...

  10. shell之任务控制

    http://linux.cn/article-2680-1.html 你的 shell 会留有一张当前作业的表单,称为作业表.当你键入命令时,shell 会给它分配一个 jobID(也称作 JOBS ...