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
| #include <bits/stdc++.h>
#define ll long long
int n, m; ll res; int dep[N], fa[N][34], base[N], up[N], down[N];
inline void dfs(int x) { int i, y; for (i = 1; i <= 30; ++ i) { if (fa[x][i - 1]) fa[x][i] = fa[fa[x][i - 1]][i - 1]; else break; } for (i = head[x]; i; i = nex[i]) { y = ver[i]; if (y == fa[x][0]) continue; fa[y][0] = x; dep[y] = dep[x] + 1; dfs(y); } }
inline int lca(int x, int y) { if (dep[x] < dep[y]) swap(x, y); int delta = dep[x] - dep[y], i; for (i = 30; i >= 0; -- i) { if (delta & (1 << i)) { x = fa[x][i]; } } if (x == y) return x; for (i = 30; i >= 0; -- i) { if (fa[x][i] != fa[y][i]) { x = fa[x][i], y = fa[y][i]; } } return fa[x][0]; }
inline void Gres(int x, int yhm_fa) { int i, y; for (i = head[x]; i; i = nex[i]) { y = ver[i]; if (y == yhm_fa) continue; Gres(y, x); up[x] += up[y]; down[x] += down[y]; if (len[i] == 1) res = (res + (1ll * base[down[y]] - 1 + mod) % mod) % mod; else if (len[i ^ 1] == 1) res = ((res + 1ll * base[up[y]] - 1 + mod) % mod) % mod; } return; }
signed main() { int i, x, y, z; n = read(); for (i = 1; i < n; ++ i) { x = read(), y = read(), z = read(); if (! z) add_edge(x, y, z), add_edge(y, x, z); else add_edge(x, y, 0), add_edge(y, x, 1); } m = read(); dfs(1), base[0] = 1; for (i = 1; i <= m; ++ i) base[i] = (1ll * base[i - 1] * 2) % mod;
x = 1; for (i = 1; i <= m; ++ i) { y = read(); int LCA = lca(x, y); ++ up[x], -- up[LCA]; ++ down[y], -- down[LCA]; x = y; }
Gres(1, 0);
put(res); return 0; }
|