intread() { int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0', c = getchar(); return x * f; } constint SN = 4e5 + 10; constint SM = 26; structSAM { int trans[SN][SM], link[SN], pre, scnt, len[SN], siz[SN]; SAM() { pre = scnt = 1; }
voidinsert(int id) { int cur = ++scnt; siz[cur] = 1; len[cur] = len[pre] + 1; int u;
for (u = pre; u && !trans[u][id]; u = link[u]) trans[u][id] = cur; pre = cur; if (!u) link[cur] = 1; else { int x = trans[u][id]; if (len[u] + 1 == len[x]) link[cur] = x; else { int nc = ++scnt; link[nc] = link[x]; len[nc] = len[u] + 1; memcpy(trans[nc], trans[x], sizeof(trans[x])); link[cur] = link[x] = nc; for (; u && trans[u][id] == x; u = link[u]) trans[u][id] = nc; } } } } sam; namespace Tree {
int st[N][22], dfn[N], dep[N], tot, lg2[N]; vector<int> G[N]; voiddfs(int x, int fa) { st[++tot][0] = x; dep[x] = dep[fa] + 1; dfn[x] = tot; for (int to : G[x]) { if (to == fa) continue; dfs(to, x); st[++tot][0] = x; } } intlower(int u, int v) { return (dep[u] < dep[v]) ? u : v; }
voidLca_init() { dfs(1, 0); for (int i = 2; i <= tot; i++) lg2[i] = lg2[i >> 1] + 1; for (int l = 1; (1 << l) <= tot; l++) { for (int i = 1; i + (1 << l) - 1 <= tot; i++) st[i][l] = lower(st[i][l - 1], st[i + (1 << (l - 1))][l - 1]); } } intlca(int u, int v) { u = dfn[u]; v = dfn[v]; if (u > v) swap(u, v); int i = lg2[v - u + 1], w = (1 << i); return lower(st[u][i], st[v - w + 1][i]); } intdis(int u, int v) { int lc = lca(u, v); return dep[u] + dep[v] - 2 * dep[lc]; } }; // namespace Tree usingnamespace Tree;
namespace XTree { stack<int> s; vector<int> tmp; int dp[N][2]; int b[N]; int vis1[N], vis0[N]; ll ans; vector<pii> g[N];
boolcmp(int x, int y) { return Tree::dfn[x] < Tree::dfn[y]; }
voidaddEdge(int u, int v) { int d = Tree::dis(u, v);