1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
| #include <iostream> #include <algorithm> #define maxn 500010 #define ll long long #define INF 1000000000000000000 using namespace std;
const int p = 998244353;
int n, m, seed, a[maxn];
struct Rand{ unsigned int n,seed; Rand(unsigned int n,unsigned int seed) :n(n),seed(seed){} int get(long long lastans){ seed ^= seed << 13; seed ^= seed >> 17; seed ^= seed << 5; return (seed^lastans)%n+1; } };
struct Matrix { const static int n = 2; ll a[2][2];
Matrix() { fill(a[0], a[0] + 4, -INF); }
void setone() { for (int i = 0; i < n; ++i) a[i][i] = 0; }
void set(ll x, ll y, ll z, ll w) { a[0][0] = x; a[0][1] = y; a[1][0] = z; a[1][1] = w; }
friend Matrix operator * (const Matrix &u, const Matrix &v) { Matrix w; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) w.a[i][j] = max(u.a[i][1] + v.a[1][j], u.a[i][0] + v.a[0][j]); return w; } } emptyM;
struct Edge { int to, next; } e[maxn * 2]; int c1, head[maxn]; inline void add_edge(int u, int v) { e[c1].to = v; e[c1].next = head[u]; head[u] = c1++; }
int R[maxn]; bool vis[maxn]; struct Calc_sz { int f[maxn], sz[maxn]; int sum, rt, maxdp;
void init() { f[rt = 0] = 1e9; }
void dfs_sz(int u, int fa) { sz[u] = 1; for (int i = head[u]; ~i; i = e[i].next) { int v = e[i].to; if (v == fa || vis[v]) continue; dfs_sz(v, u); sz[u] += sz[v]; } }
void dfs(int u, int fa) { f[u] = 0; for (int i = head[u]; ~i; i = e[i].next) { int v = e[i].to; if (v == fa || vis[v]) continue; dfs(v, u); f[u] = max(f[u], sz[v]); } f[u] = max(f[u], sum - sz[u]); if (f[u] < f[rt]) rt = u; }
inline int get_rt(int u) { rt = maxdp = 0; dfs_sz(u, 0); sum = sz[u]; dfs(u, 0); return rt; } } _;
Matrix f[maxn][21]; void dfs(int u, int fa, int d) { f[u][d].set(0, 0, a[u], -INF); f[u][d] = f[fa][d] * f[u][d]; for (int i = head[u]; ~i; i = e[i].next) { int v = e[i].to; if (v == fa || vis[v]) continue; dfs(v, u, d); } }
int id[2 * maxn], in[maxn], dep[maxn]; void divide(int u, int d) { static int cnt = 0; id[++cnt] = u; in[u] = cnt; vis[u] = 1; dep[u] = d; f[u][d].set(0, -INF, -INF, 0); for (int i = head[u]; ~i; i = e[i].next) { int v = e[i].to; if (vis[v]) continue; dfs(v, u, d); divide(_.get_rt(v), d + 1); id[++cnt] = u; } f[u][d].set(0, 0, a[u], -INF); }
inline int st_min(int l, int r) { return in[l] < in[r] ? l : r; }
int st[maxn * 2][21], Log[maxn * 2]; void init_st(int n) { Log[0] = -1; for (int i = 1; i <= n; ++i) Log[i] = Log[i >> 1] + 1; for (int i = 1; i <= n; ++i) st[i][0] = id[i]; for (int j = 1; j <= 20; ++j) for (int i = 1; i + (1 << j) - 1 <= n; ++i) st[i][j] = st_min(st[i][j - 1], st[i + (1 << j - 1)][j - 1]); }
inline int get_lca(int l, int r) { l = in[l]; r = in[r]; if (l > r) swap(l, r); int k = Log[r - l + 1]; return st_min(st[l][k], st[r - (1 << k) + 1][k]); }
ll query(int x, int y) { int lca = get_lca(x, y), d = dep[lca]; if (dep[x] > dep[y]) swap(x, y); if (lca == x) return max(max(f[y][d].a[0][0], f[y][d].a[1][0]), a[x] + f[y][d].a[0][0]); return max(max(f[x][d].a[0][0], f[x][d].a[1][0]) + max(f[y][d].a[0][0], f[y][d].a[1][0]), a[lca] + f[x][d].a[0][0] + f[y][d].a[0][0]); }
int main() { fill(head, head + maxn, -1); ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> n >> m >> seed; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 2, x; i <= n; ++i) cin >> x, add_edge(x, i), add_edge(i, x); _.init(); divide(_.get_rt(1), 1); init_st(2 * n - 1); ll lans = 0, ans = 0; Rand rand(n, seed); for (int i = 1; i <= m; ++i) { int u = rand.get(lans); int v = rand.get(lans); int x = rand.get(lans); lans = query(u, v); ans = (ans + lans % p * x) % p; } cout << ans << "\n"; return 0; }
|