给出一棵树,对于每一个询问,给出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. error_log() 范例

    <?php// 如果无法连接到数据库,发送通知到服务器日志if (!Ora_Logon($username, $password)) {    error_log("Oracle da ...

  2. POJ1149 PIGS (网络流)

                                                                             PIGS Time Limit: 1000MS   M ...

  3. Activity——思维导图

  4. P364 实战练习(多线程)

    尝试定义一个继承Thread类的类,并覆盖run( )方法,在run( )方法中每隔1000毫秒打印一句话. 编写代码如下: 编写PractiseThread类: package org.hanqi. ...

  5. P168 实战练习(构造方法)

    尝试编写一个矩形类,将长宽做为矩形类的属性,在构造方法中将长宽初始化,定义一个成员方法求此矩形的面积. 编写代码如下: 创建Rectangular类,则相关代码为: package org.hanqi ...

  6. HDU-5783 Divide the Sequence(贪心)

    题目大意:给一个整数序列,将其划分成若干个子连续序列,使其每个子序列的前缀和不为负.求最大的划分个数. 题目分析:从后往做累加计算,如果不为负,则计数加一,累加和清0.否则,一直往前扫描.如果最终的和 ...

  7. IE 下加载jQuery

    转:http://www.iitshare.com/ie8-not-use-native-json.html 解决在IE8中无法使用原生JSON的问题   起因 在项目中要将页面上的js对象传给后台, ...

  8. Codeforces Round #339 Div.2 B - Gena's Code

    It's the year 4527 and the tanks game that we all know and love still exists. There also exists Grea ...

  9. 3D知识补充

    Light Mapping = Dark Mapping (光照映射.黑暗映射) 本质上也是多贴一张图,他是做相乘操作.第2张纹理通常中间亮,外面暗.如果是简单的 Modulate,那么实际上所有像素 ...

  10. Linux实时监控工具Nmon使用

    官网:http://nmon.sourceforge.net/pmwiki.php?n=Main.HomePage 下载:http://sourceforge.net/projects/nmon/fi ...