给你一个数,然后让你求这两个数之间的点的深度的k次方和.
很容易想到lca.因为lca可以说是求树上两个点的距离的好方法.而且lca还能遍历每一个点.
然后我们可以用一个数组pre来存储每一个点到深度的多少次方.
处理的时候在求深度的时候直接暴力求就行.
#include <bits/stdc++.h>
#define int long long
#define N 300010
#define M 1010
#define _ 0
using namespace std;
const int mod = 998244353;
int n, m, add_edge; bool vis[N];
int fa[N][25], deep[N], head[N << 1], pre[N][51];
struct node {
int next, to;
}edge[N << 1];
int read() {
int s = 0, f = 0; char ch = getchar();
while (!isdigit(ch)) f |= (ch == '-'), ch = getchar();
while (isdigit(ch)) s = s * 10 + (ch ^ 48), ch = getchar();
return f ? -s : s;
}
int q_pow(int a, int b) {
int ans = 1;
while (b) {
if (b & 1) ans = (ans * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return ans;
}
void add(int from, int to) {
edge[++add_edge].next = head[from];
edge[add_edge].to = to;
head[from] = add_edge;
}
void dfs(int x, int f) {
deep[x] = deep[f] + 1, fa[x][0] = f;
for (int i = 1; i <= 50; i++)
pre[x][i] = (pre[f][i] + q_pow(deep[x], i) + mod) % mod;
for (int i = head[x]; i; i = edge[i].next) {
int to = edge[i].to;
if (to == f) continue;
dfs(to, x);
}
}
int lca(int x, int y) {
if (deep[x] > deep[y]) swap(x, y);
for (int i = 20; i >= 0; i--)
if (deep[fa[y][i]] >= deep[x])
y = fa[y][i];
if (x == y) return x;
for (int i = 20; i >= 0; i--)
if (fa[x][i] != fa[y][i])
x = fa[x][i], y = fa[y][i];
return fa[x][0];
}
signed main() {
n = read();
for (int i = 1, x, y; i <= n - 1; i++) {
x = read(), y = read();
add(x, y), add(y, x);
}
deep[1] = -1, dfs(1, 1);
for (int j = 1; j <= 21; j++)
for (int i = 1; i <= n; i++)
fa[i][j] = fa[fa[i][j - 1]][j - 1];
m = read();
for (int i = 1, x, y, k; i <= m; i++) {
x = read(), y = read(), k = read();
int lc = lca(x, y);
int sum1 = (pre[x][k] + pre[y][k] + mod) % mod;
int sum2 = (pre[lc][k] + pre[fa[lc][0]][k] + mod) % mod;
printf("%lld\n", (sum1 - sum2 + mod) % mod);
}
return 0;
}
原文:https://www.cnblogs.com/zzz-hhh/p/11695438.html