[luoguP2912] [USACO08OCT]牧场散步Pasture Walking(lca)

传送门

水题。

直接倍增求lca。

x到y的距离为dis[x] + dis[y] - 2 * dis[lca(x, y)]

——代码

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #define MAXN 20002
 5
 6 using namespace std;
 7
 8 int n, q, cnt;
 9 int head[MAXN], to[MAXN], next[MAXN], val[MAXN], deep[MAXN], f[MAXN][21], dis[MAXN];
10
11 inline void add(int x, int y, int z)
12 {
13     to[cnt] = y;
14     val[cnt] = z;
15     next[cnt] = head[x];
16     head[x] = cnt++;
17 }
18
19 inline void dfs(int u)
20 {
21     int i, v;
22     deep[u] = deep[f[u][0]] + 1;
23     for(i = 0; f[u][i]; i++) f[u][i + 1] = f[f[u][i]][i];
24     for(i = head[u]; i != -1; i = next[i])
25     {
26         v = to[i];
27         if(!deep[v])
28         {
29             f[v][0] = u;
30             dis[v] = dis[u] + val[i];
31             dfs(v);
32         }
33     }
34 }
35
36 inline int lca(int x, int y)
37 {
38     int i;
39     if(deep[x] < deep[y]) swap(x, y);
40     for(i = 20; i >= 0; i--)
41         if(deep[f[x][i]] >= deep[y])
42             x = f[x][i];
43     if(x == y) return x;
44     for(i = 20; i >= 0; i--)
45         if(f[x][i] != f[y][i])
46             x = f[x][i], y = f[y][i];
47     return f[x][0];
48 }
49
50 int main()
51 {
52     int i, x, y, z;
53     scanf("%d %d", &n, &q);
54     memset(head, -1, sizeof(head));
55     for(i = 1; i < n; i++)
56     {
57         scanf("%d %d %d", &x, &y, &z);
58         add(x, y, z);
59         add(y, x, z);
60     }
61     deep[1] = 1;
62     dfs(1);
63     for(i = 1; i <= q; i++)
64     {
65         scanf("%d %d", &x, &y);
66         printf("%d\n", dis[x] + dis[y] - 2 * dis[lca(x, y)]);
67     }
68     return 0;
69 }

时间: 2024-08-07 17:01:54

[luoguP2912] [USACO08OCT]牧场散步Pasture Walking(lca)的相关文章

[USACO08OCT]牧场散步Pasture Walking (LCA) (乱搞)

题面传送门我太懒了所以吃掉题面 题解 可以发现如果两点不在一条链上的话,那么他们的最短路径一定会经过LCA. 所以可以维护一下每个点到树根的距离,然后大力前缀和乱搞就好了. #include <bits/stdc++.h> const int max_n=1e4+5; int N,M,cnt; int depth[max_n],father[15][max_n],lg2[max_n],first_edge[max_n],dis[max_n]; struct edge { int to,w,ne

[USACO08OCT]牧场散步Pasture Walking

[USACO08OCT]牧场散步Pasture Walking 题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures also conveniently numbered 1..N. Most conveniently of all, cow i is grazing in pasture i. Some pairs of pastures are connec

P2912 [USACO08OCT]牧场散步Pasture Walking

题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures also conveniently numbered 1..N. Most conveniently of all, cow i is grazing in pasture i. Some pairs of pastures are connected by one of N-1 bidirectional

【洛谷P2912】[USACO08OCT]牧场散步Pasture Walking

题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures also conveniently numbered 1..N. Most conveniently of all, cow i is grazing in pasture i. Some pairs of pastures are connected by one of N-1 bidirectional

洛谷 P2912 [USACO08OCT]牧场散步Pasture Walking

题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures also conveniently numbered 1..N. Most conveniently of all, cow i is grazing in pasture i. Some pairs of pastures are connected by one of N-1 bidirectional

[USACO08OCT] Pasture Walking

题目链接 LCA裸题 #include<cstdio> #define MAXN 1005 using namespace std; int N,Q,tot=0; struct size{ int v,w,next; }G[MAXN<<1]; int head[MAXN],anc[11][MAXN],dis[MAXN],d[MAXN]; inline void add(int u,int v,int w){ G[++tot].v=v;G[tot].w=w;G[tot].next=h

【Luogu】P2912牧场散步(TarjanLCA)

题目链接 老天--终于碰上一个除了模板之外的LCA题了 这道题用Tarjan来LCA.树上两个点的路径是唯一的,所以钦定一个根,两点间的路径就是两点到根的路径减去双倍的公共祖先到根的路径.大概很好理解. #include<cstdio> #include<cctype> #include<cstring> #include<iostream> using namespace std; inline long long read(){ long long nu

树讲解——牧场行走( lca )

大视野   1602: [Usaco2008 Oct]牧场行走 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1947  Solved: 1021[Submit][Status][Discuss] Description N头牛(2<=n<=1000)别人被标记为1到n,在同样被标记1到n的n块土地上吃草,第i头牛在第i块牧场吃草. 这n块土地被n-1条边连接. 奶牛可以在边上行走,第i条边连接第Ai,Bi块牧场,第i条边的长度是Li(1<=

[BZOJ1602] [Usaco2008 Oct] 牧场行走 (LCA)

Description N头牛(2<=n<=1000)别人被标记为1到n,在同样被标记1到n的n块土地上吃草,第i头牛在第i块牧场吃草. 这n块土地被n-1条边连接. 奶牛可以在边上行走,第i条边连接第Ai,Bi块牧场,第i条边的长度是Li(1<=Li<=10000). 这些边被安排成任意两头奶牛都可以通过这些边到达的情况,所以说这是一棵树. 这些奶牛是非常喜欢交际的,经常会去互相访问,他们想让你去帮助他们计算Q(1<=q<=1000)对奶牛之间的距离. Input *