To tell the truth as long as understand the meaning of the title of this topic is quite water. . . (I almost kept a sample ..
Specific weight of each point is the first point of the square are included in the answer (because each point can be considered a path ..)
Then the question is intended to simulate the line.
I \ (dfs \) is a point each, so a path between two points is counted twice. So divided by \ (2 \) .
nmd This mod is really annoying, but still found. Answers on the path to the modulo separately from, and answers can not be calculated with a single point, i.e., can not be written
printf("%lld\n", ((ans / 2) + sum + mod) % mod);
But should be written
printf("%lld\n", (ans * (mod / 2 + 1) + sum + mod) % mod);
Or take the inverse 2 also, to write fast power. .
printf("%lld\n", (ans * ksm(2,mod-2,mod) + sum + mod) % mod);
I started I thought it through. .
The woman opened orz God
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll N = 1e5 + 10;
const ll mod = 998244353;
ll head[N], Next[N<<1], ver[N<<1], tot = 1;
ll val[N];
template<class I>
inline void rd(I &x) {
ll f = 1;char c = getchar();
for(;c < '0' || c > '9';c = getchar()) if(c == '-') f = -1;
for(x = 0;c >= '0' && c <= '9';x = (x << 3) + (x << 1) + (c & 15), c = getchar());
x *= f;
}
void add(ll x, ll y) {
ver[++tot] = y;
Next[tot] = head[x];
head[x] = tot;
}
ll fa[N][12];
queue<ll> q;
ll d[N];
ll ans;
void dfs(ll x, ll fa, ll maxx, ll minn) {
maxx = max(maxx, val[x]);
minn = min(minn, val[x]);
ans = (ans % mod + maxx * minn % mod + mod) % mod;
for (ll i = head[x];i;i = Next[i]) {
ll y = ver[i];
if(y == fa) continue;
dfs(y,x,maxx,minn);
}
}
ll n;
ll sum;
int main() {
rd(n);
for (ll i = 1;i <= n;i++) {
rd(val[i]);
sum = (sum % mod + val[i] * val[i] % mod + mod) % mod;
}
for (ll i = 1;i < n;i++) {
ll x, y;
rd(x), rd(y);
add(x,y);
add(y,x);
}
for (ll x = 1;x <= n;x++)
for (ll i = head[x];i;i = Next[i]) {
ll y = ver[i];
dfs(y,x,val[x],val[x]);
}
printf("%lld\n", (ans * (mod / 2 + 1) + sum + mod) % mod);
return 0;
}
Father with nice trees