#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int n, q;
struct Graph {
vector<pair<int, int>> e[MAXN];
int dfn[MAXN], idf[MAXN], dcnt;
int fa[MAXN][22];
int dep[MAXN];
long long dis[MAXN];
void add(int u, int v, int w) {
e[u].push_back({v, w});
e[v].push_back({u, w});
}
void dfs(int u, int pre) {
fa[u][0] = pre;
for (int i = 1; i <= 20; i++)
fa[u][i] = fa[fa[u][i - 1]][i - 1];
dep[u] = dep[pre] + 1;
dfn[u] = ++dcnt;
idf[dcnt] = u;
for (auto p : e[u]) if (p.first != pre) {
int v = p.first, w = p.second;
dis[v] = dis[u] + w;
dfs(v, u);
}
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
for (int i = 20; i >= 0; i--)
if (dep[fa[u][i]] >= dep[v]) u = fa[u][i];
if (u == v) return u;
for (int i = 20; i >= 0; i--)
if (fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i];
return fa[u][0];
}
long long dist(int u, int v) {
return dis[u] + dis[v] - 2 * dis[lca(u, v)];
}
} g;
struct SegmentTree {
struct Node {
int fst, lst;
long long sum;
} t[MAXN << 2];
#define lc (i << 1)
#define rc (i << 1 | 1)
void pushUp(int i) {
if (!t[lc].fst) {
t[i] = t[rc];
} else if (!t[rc].fst) {
t[i] = t[lc];
} else {
t[i].sum = t[lc].sum + t[rc].sum + g.dist(g.idf[t[lc].lst], g.idf[t[rc].fst]);
t[i].fst = t[lc].fst;
t[i].lst = t[rc].lst;
}
}
void set(int d, int v, int i = 1, int l = 1, int r = n) {
if (l == r) {
if (v == 0) {
t[i].fst = t[i].lst = 0;
} else {
t[i].fst = t[i].lst = l;
}
return;
}
int mid = (l + r) >> 1;
if (d <= mid) set(d, v, lc, l, mid);
else set(d, v, rc, mid + 1, r);
pushUp(i);
}
} st;
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int u, v, w; scanf("%d%d%d", &u, &v, &w);
g.add(u, v, w);
}
g.dfs(1, 0);
scanf("%d", &q);
while (q--) {
char op; scanf(" %c", &op);
if (op == '+') {
int u; scanf("%d", &u);
st.set(g.dfn[u], 1);
}
if (op == '-') {
int u; scanf("%d", &u);
st.set(g.dfn[u], 0);
}
if (op == '?') {
printf("%lld\n", (st.t[1].sum + g.dist(g.idf[st.t[1].fst], g.idf[st.t[1].lst])) / 2);
}
}
return 0;
}//6287838605153792925
1302. Deepest Leaves Sum | 1209. Remove All Adjacent Duplicates in String II |
994. Rotting Oranges | 983. Minimum Cost For Tickets |
973. K Closest Points to Origin | 969. Pancake Sorting |
967. Numbers With Same Consecutive Differences | 957. Prison Cells After N Days |
946. Validate Stack Sequences | 921. Minimum Add to Make Parentheses Valid |
881. Boats to Save People | 497. Random Point in Non-overlapping Rectangles |
528. Random Pick with Weight | 470. Implement Rand10() Using Rand7() |
866. Prime Palindrome | 1516A - Tit for Tat |
622. Design Circular Queue | 814. Binary Tree Pruning |
791. Custom Sort String | 787. Cheapest Flights Within K Stops |
779. K-th Symbol in Grammar | 701. Insert into a Binary Search Tree |
429. N-ary Tree Level Order Traversal | 739. Daily Temperatures |
647. Palindromic Substrings | 583. Delete Operation for Two Strings |
518. Coin Change 2 | 516. Longest Palindromic Subsequence |
468. Validate IP Address | 450. Delete Node in a BST |