以下是我今天解题的题解报告:
[1]点的距离
题目描述
给定一棵有n个结点的树,Q个询问,每次询问点x到点y亮点之间的距离
输入
第一行一个n,表示有n个节。
接下来有n-1行,每行2个整数x,y表示x,y之间有一条连边。
然后一个整数Q,表示有Q次询问,接下来Q行每行2个整数x,y表示询问x到y的距离。
输出
输出Q行,每行表示每个询问的结果
样例输入
6
1 2
1 3
2 4
2 5
3 6
2
2 6
5 6
样例输出
3
4
思路:
这题就是一个模板题,由于n和m都是20000的数据范围(不知道具体范围,题里也没说,就随便蒙了一个),无法暴力求解,而lca单次查询是logn效率,可以实现,
在查找到公共节点后dist[x, y] = dep[x] + dep[y] – 2 * dep[lca]
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 2e5+10;
int cs[maxn],f[maxn],st[25][maxn],dep[maxn];
int First[maxn],Next[maxn*2],v[maxn*2],num_edge;
//建边
void ins(int x , int y )
{
v[++ num_edge] = y;
Next[num_edge] = First[x];
First[x] = num_edge;
}
int n,root;
void dfs(int x,int fa)
{
f[x] = fa;
for(int i = First[x]; i != -1 ; i = Next[i])
{
int to = v[i];
if(to == fa)
continue;
dep[to] = dep[x] + 1;
dfs(to,x);
}
}
int get_lca(int x, int y)
{
if(dep[x] < dep[y]) swap(x,y);
for(int i = 20 ; i >= 0 ; i--)
{
if(dep[st[i][x]] >= dep[y])
{
x = st[i][x];
}
}
if(x == y) return x;
for(int i = 20 ; i >= 0; i--)
{
if(st[i][x] != st[i][y])
{
x = st[i][x];
y = st[i][y];
}
}
return f[x];
}
int main(){
while(~scanf("%d",&n))
{
memset(First,-1,sizeof(First));//初始化链式前向星
num_edge = 0;
for(int i = 1 ; i < n; i++)
{
int x,y;
scanf("%d %d",&x,&y);
ins(x,y),ins(y,x);//加边
}
dep[1] = 1;
dfs(1,0);
//建立st表
for(int i = 1 ; i <= n; i++)
{
st[0][i] = f[i];
}
for(int i = 1; i <= 20 ; i++)
{
for(int j = 1; j <= n; j++)
{
st[i][j] = st[i-1][st[i-1][j]];
}
}
int Q;
scanf("%d",&Q);
while(Q--)
{
int x,y;
scanf("%d %d",&x,&y);
int temp = get_lca(x,y);
printf("%d\n",dep[x] + dep[y] - 2 * dep[temp]);//输出~
}
}
}
[2]聚会
题目描述
Y岛风景美丽宜人,气候温和,物产丰富。Y岛上有N个城市,有N-1条城市间的道路连接着它们。每一条道路都连接某两个城市。幸运的是,小可可通过这些道路可以走遍Y岛的所有城市。神奇的是,乘车经过每条道路所需要的费用都是一样的。小可可,小卡卡和小YY经常想聚会,每次聚会,他们都会选择一个城市,使得3个人到达这个城市的总费用最小。 由于他们计划中还会有很多次聚会,每次都选择一个地点是很烦人的事情,所以他们决定把这件事情交给你来完成。他们会提供给你地图以及若干次聚会前他们所处的位置,希望你为他们的每一次聚会选择一个合适的地点。
输入
第一行两个正整数,N和M。分别表示城市个数和聚会次数。后面有N-1行,每行用两个正整数A和B表示编号为A和编号为B的城市之间有一条路。城市的编号是从1到N的。再后面有M行,每行用三个正整数表示一次聚会的情况:小可可所在的城市编号,小卡卡所在的城市编号以及小YY所在的城市编号。
输出
一共有M行,每行两个数Pos和Cost,用一个空格隔开。表示第i次聚会的地点选择在编号为Pos的城市,总共的费用是经过Cost条道路所花费的费用。
样例输入
6 4
1 2
2 3
2 4
4 5
5 6
4 5 6
6 3 1
2 4 4
6 6 6
样例输出
5 2
2 5
4 1
6 0
思路:
参考学长的思路,三点的两两间的lca只有两个,并且重复的那个lca必然是深度小的(画图可以很快证明得到,如果有三个lca的话会和lca的定义产生矛盾),并且答案就是深度大的那个lca(也可以画图得到因为lca(max)和lca(min)中间会有一段距离,深度大的lca不会重复走这段路,所以花钱少)
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 5e5+10;
int cs[maxn],f[maxn],st[22][maxn],dep[maxn];
int First[maxn],Next[maxn*2],v[maxn*2],num_edge;
void ins(int x , int y )
{
v[++ num_edge] = y;
Next[num_edge] = First[x];
First[x] = num_edge;
}
int n,root,Q;
void dfs(int x,int fa)
{
f[x] = fa;
for(int i = First[x]; i != -1 ; i = Next[i])
{
int to = v[i];
if(to == fa)
continue;
dep[to] = dep[x] + 1;
dfs(to,x);
}
}
int get_lca(int x, int y)
{
if(dep[x] < dep[y]) swap(x,y);
for(int i = 20 ; i >= 0 ; i--)
{
if(dep[st[i][x]] >= dep[y])
{
x = st[i][x];
}
}
if(x == y) return x;
for(int i = 20 ; i >= 0; i--)
{
if(st[i][x] != st[i][y])
{
x = st[i][x];
y = st[i][y];
}
}
return f[x];
}
int main(){
while(~scanf("%d %d", &n, &Q))
{
memset(First, -1, sizeof(First));
num_edge = 0;
for(int i = 1; i < n; i ++)
{
int x, y;
scanf("%d %d", &x, &y);
ins(x, y), ins(y, x);
}
dep[1] = 1;
dfs(1, 0);
for(int i = 1; i <= n; i ++)
st[0][i] = f[i];
for(int i = 1; i <= 20; i ++)
{
for(int j = 1; j <= n; j ++)
st[i][j] = st[i - 1][st[i - 1][j]];
}
while(Q --)
{
int x, y, z;
scanf("%d %d %d", &x, &y, &z);
int t1 = get_lca(x, y), t2 = get_lca(x, z), t3 = get_lca(y, z);
int pos;
if(t1 == t2)
pos = t3;
else if(t1 == t3)
pos = t2;
else
pos = t1;
printf("%d %d\n", pos, dep[x] + dep[y] + dep[z] - dep[t1] - dep[t2] - dep[t3]);
}
}
return 0;
}