BZOJ 1832: [AHOI2008]聚会( LCA )

LCA模板题...不难发现一定是在某2个人的LCA处集合是最优的, 然后就3个LCA取个最小值就OK了. 距离就用深度去减一减就可以了. 时间复杂度O(N+MlogN) (树链剖分)

-----------------------------------------------------------------------------------

#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
 
using namespace std;
 
const int maxn = 500009;
 
int N, Q;
int sz[maxn], top[maxn], dep[maxn], fa[maxn], son[maxn], Top;
 
inline int read() {
char c = getchar();
for(; !isdigit(c); c = getchar());
int ret = 0;
for(; isdigit(c); c = getchar())
ret = ret * 10 + c - '0';
return ret;
}
 
struct edge {
int to;
edge* next;
} E[maxn << 1], *pt = E, *head[maxn];
 
inline void AddEdge(int u, int v) {
pt->to = v;
pt->next = head[u];
head[u] = pt++;
}
 
void DFS(int x) {
sz[x] = 1;
son[x] = -1;
for(edge* e = head[x]; e; e = e->next) {
if(e->to == fa[x]) continue;
fa[e->to] = x;
dep[e->to] = dep[x] + 1;
DFS(e->to);
sz[x] += sz[e->to];
if(son[x] == -1 || sz[son[x]] < sz[e->to])
son[x] = e->to;
}
}
 
void dfs(int x) {
top[x] = Top;
if(~son[x])
dfs(son[x]);
for(edge* e = head[x]; e; e = e->next)
if(e->to != son[x] && e->to != fa[x]) dfs(Top = e->to);
}
 
int LCA(int x, int y) {
for(; top[x] != top[y]; x = fa[top[x]])
if(dep[top[x]] < dep[top[y]]) swap(x, y);
return dep[x] < dep[y] ? x : y;
}
 
void Init() {
N = read(), Q = read();
for(int i = 1; i < N; i++) {
int u = read() - 1, v = read() - 1;
AddEdge(u, v);
AddEdge(v, u);
}
fa[0] = -1;
dep[0] = 0;
DFS(0);
dfs(Top = 0);
}
 
int Dist(int x, int y) {
if(top[x] != top[y]) {
int t = LCA(x, y);
if(t != x && t != y)
return dep[x] + dep[y] - 2 * dep[t];
}
int ret = dep[x] - dep[y];
return ret > 0 ? ret : -ret;
}
 
void Work() {
int p, v;
while(Q--) {
int x = read() - 1, y = read() - 1, z = read() - 1;
v = 1000000;
int P = LCA(x, y), V = dep[x] + dep[y] - dep[P] * 2;
if((V += Dist(P, z)) < v)
p = P, v = V;
P = LCA(x, z), V = dep[x] + dep[z] - dep[P] * 2;
if((V += Dist(P, y)) < v)
p = P, v = V;
P = LCA(y, z), V = dep[y] + dep[z] - dep[P] * 2;
if((V += Dist(P, x)) < v)
p = P, v = V;
printf("%d %d\n", ++p, v);
}
}
 
int main() {
Init();
Work();
return 0;
}

----------------------------------------------------------------------------------- 

1832: [AHOI2008]聚会

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 968  Solved: 381
[Submit][Status][Discuss]

Description

Y岛风景美丽宜人,气候温和,物产丰富。Y岛上有N个城市,有N-1条城市间的道路连接着它们。每一条道路都连接某两个城市。幸运的是,小可可通过这些道路可以走遍Y岛的所有城市。神奇的是,乘车经过每条道路所需要的费用都是一样的。小可可,小卡卡和小YY经常想聚会,每次聚会,他们都会选择一个城市,使得3个人到达这个城市的总费用最小。 由于他们计划中还会有很多次聚会,每次都选择一个地点是很烦人的事情,所以他们决定把这件事情交给你来完成。他们会提供给你地图以及若干次聚会前他们所处的位置,希望你为他们的每一次聚会选择一个合适的地点。

Input

第一行两个正整数,N和M。分别表示城市个数和聚会次数。后面有N-1行,每行用两个正整数A和B表示编号为A和编号为B的城市之间有一条路。城市的编号是从1到N的。再后面有M行,每行用三个正整数表示一次聚会的情况:小可可所在的城市编号,小卡卡所在的城市编号以及小YY所在的城市编号。

Output

一共有M行,每行两个数Pos和Cost,用一个空格隔开。表示第i次聚会的地点选择在编号为Pos的城市,总共的费用是经过Cost条道路所花费的费用。

Sample Input

6 4
1 2
2 3
2 4
4 5
5 6
4 5 6
6 3 1
2 4 4
6 6 6

Sample Output

5 2
2 5
4 1
6 0

数据范围:
100%的数据中,N<=500000,M<=500000。
40%的数据中N<=2000,M<=2000。

HINT

Source

 

posted @ 2016-01-11 14:15  JSZX11556  阅读(293)  评论(0编辑  收藏  举报