#include #include #include #include using namespace std; using pii = pair; struct Req { int ind, t, x, y; vector alph; }; int pair_cnt; vector> pair_map; int N, M, Q; vector edges; vector> g; vector a; vector ans, lnks, lnks_z, lnk_jump, d, par, par_z; vector> top_links, bot_links, bu_links; vector dsu; int get_dsu_parent(int x) { if (dsu[x] == dsu[dsu[x]]) { return dsu[x]; } return dsu[x] = get_dsu_parent(dsu[x]); } int get_top_link(int edge_ind, int pair_ind) { if (top_links[edge_ind][pair_ind] == edge_ind) { return edge_ind; } return get_top_link(top_links[edge_ind][pair_ind], pair_ind); } int get_edge_ind(int x, int y) { pii edge(x, y); int l = 0, r = M - 1; while (l <= r) { int mid = (l + r) >> 1; if (edges[mid] == edge) { return mid; } if (edges[mid] < edge) { l = mid + 1; } else { r = mid - 1; } } return -1; } void precalc() { pair_map.assign(26, vector(26, -1)); pair_cnt = 0; for (int z1 = 0; z1 < 26; z1++) { for (int z2 = z1 + 1; z2 < 26; z2++) { pair_map[z1][z2] = pair_cnt++; pair_map[z2][z1] = pair_cnt++; } } } void add_edge(int x, int y, int z) { edges.push_back({x, y}); g[x].push_back({y, z}); } int dfs_lnks(int x, int p) { par[x] = p; lnk_jump[x] = x; if (x == p) { d[x] = 0; } else { d[x] = d[p] + 1; } lnks[x] = -1; lnks_z[x] = -1; int best = 0, total = 0; for (auto [y, z] : g[x]) { if (y == p) { par_z[x] = z; continue; } int cur = dfs_lnks(y, x); total += cur; if (cur > best) { best = cur; lnk_jump[x] = lnk_jump[y]; lnks[x] = y; lnks_z[x] = z; } } return total + 1; } void run_precalc_top(int x, int p) { if (x != p) { int edge_ind = get_edge_ind(x, p); if (p == par[p]) { top_links[edge_ind].assign(pair_cnt, edge_ind); } else { int par_edge_ind = get_edge_ind(p, par[p]); top_links[edge_ind] = top_links[par_edge_ind]; } if (par[p] != p) { int ppz = par_z[p]; for (auto [y, z] : g[p]) { if (y == par[p] || y == x) { continue; } top_links[edge_ind][pair_map[z][ppz]] = edge_ind; } } } for (int i = 0; i < (int)g[x].size(); i++) { int y = g[x][i].first; if (y == p) { continue; } run_precalc_top(y, x); } } void run_precalc_bot(int x, int p) { for (auto [y, _] : g[x]) { if (y != p) { run_precalc_bot(y, x); } } int ly = lnks[x], lz = lnks_z[x]; if (ly != -1) { bot_links[x] = bot_links[ly]; for (auto [y, z] : g[x]) { if (y == ly || y == p) { continue; } bot_links[x][pair_map[z][lz]] = x; } } else { bot_links[x].assign(pair_cnt, x); } } void binary_uplifting() { int sz = (int)g.size(); int maxp = 1; while ((1 << maxp) < sz) { ++maxp; } ++maxp; bu_links.assign(maxp, vector(sz)); for (int i = 0; i < sz; i++) { bu_links[0][i] = par[i]; } for (int p = 1; p < maxp; p++) { for (int i = 0; i < sz; i++) { bu_links[p][i] = bu_links[p - 1][bu_links[p - 1][i]]; } } } void init() { int x = 0, y = 0, z = 0; char c = 0; g.clear(); edges.clear(); edges.reserve(N << 2); cin >> N; g.resize(N); for (int i = 1; i < N; i++) { cin >> x >> y >> c; --x, --y; z = (int)(c - 'A'); add_edge(x, y, z); add_edge(y, x, z); } int t = 0; string s; vector alph; cin >> Q; a.resize(Q); for (int i = 0; i < Q; i++) { Req &r = a[i]; cin >> t; --t; r.ind = i; r.t = t; if (t) { cin >> x >> y >> s; --x; alph.resize(s.length()); for (int i = 0; i < (int)s.length(); i++) { alph[i] = (int)(s[i] - 'A'); } r.x = x; r.y = y; r.alph = alph; } else { y = (int)(g.size()); g.push_back({}); cin >> x >> c; --x; z = (int)(c - 'A'); add_edge(x, y, z); add_edge(y, x, z); r.x = x; r.y = y; } } sort(edges.begin(), edges.end()); M = edges.size(); top_links.resize(M); dsu.resize(g.size()); for (int i = 0; i < (int)g.size(); i++) { dsu[i] = i; } par.resize(g.size()); par_z.resize(g.size()); d.resize(g.size()); lnks.resize(g.size()); lnks_z.resize(g.size()); lnk_jump.resize(g.size()); bot_links.resize(g.size()); dfs_lnks(0, 0); binary_uplifting(); run_precalc_top(0, 0); run_precalc_bot(0, 0); } int get_uplift(int x, int k) { for (int p = (int)bu_links.size() - 1; p >= 0; --p) { if (k & (1 << p)) { x = bu_links[p][x]; } } return x; } int go_up(int x, int &k, const vector &stop_vector) { int p = par[x]; if (x == p) { return x; } int edge_ind = get_edge_ind(x, p), y = 0; for (int _y : stop_vector) { int ny = edges[get_top_link(edge_ind, _y)].second; if (d[y] < d[ny]) { y = ny; } } int dif = d[x] - d[y]; if (k < dif) { y = get_uplift(x, k); k = 0; } else { k -= dif; } return y; } int go_down(int x, int &k, const vector &stop_vector, const vector &rev_alph) { while (k) { if (lnks[x] == -1) { break; } int y = lnk_jump[x]; for (int _y : stop_vector) { int ny = bot_links[x][_y]; if (d[y] > d[ny]) { y = ny; } } y = get_dsu_parent(y); int dif = d[y] - d[x]; if (k < dif) { y = get_uplift(y, dif - k); k = 0; } else { k -= dif; } if (k) { int ny = -1, nz = 26; for (auto [yy, _zz] : g[y]) { if (yy == par[y] || yy != dsu[yy]) { continue; } int zz = rev_alph[_zz]; if (nz > zz) { nz = zz; ny = yy; } } if (ny == -1) { x = y; break; } --k; y = ny; } x = y; } return x; } int get_res(int x, int k, const vector &alph) { if (!k) { return x; } vector stop_vector; for (int i = 0; i < (int)alph.size(); i++) { for (int j = i + 1; j < (int)alph.size(); j++) { stop_vector.push_back(pair_map[alph[i]][alph[j]]); } } vector rev_alph(26, -1); for (int i = 0; i < (int)alph.size(); i++) { rev_alph[alph[i]] = i; } if (x != par[x]) { int up_dir = 1; for (auto [y, z] : g[x]) { if (y == par[x] || y != dsu[y]) { continue; } if (rev_alph[par_z[x]] > rev_alph[z]) { up_dir = 0; break; } } if (up_dir) { int pk = k, px = x; x = go_up(x, k, stop_vector); if (k) { int nx = get_uplift(px, pk - k - 1); int y = -1, z = 26; for (auto [ny, _nz] : g[x]) { if (ny == par[x] || ny == nx || ny != dsu[ny]) { continue; } int nz = rev_alph[_nz]; if (nz < z) { y = ny; z = nz; } } if (y == -1) { return x; } x = y; --k; } } } if (k) { x = go_down(x, k, stop_vector, rev_alph); } return x; } void update_precalc_top(int x) { int p = par[x]; if (x == p || p == par[p]) { return; } int par_edge_ind = get_edge_ind(p, par[p]); int ppz = par_z[p], z = -1; for (auto [y, _z] : g[p]) { if (y != x) { continue; } z = _z; break; } for (auto [y, _z] : g[p]) { if (y == par[p] || y != dsu[y]) { continue; } int edge_ind = get_edge_ind(y, p); int pair_ind = pair_map[z][ppz]; top_links[edge_ind][pair_ind] = get_top_link(par_edge_ind, pair_ind); } } void solve() { ans.assign(Q, -1); for (int i = Q - 1; i >= 0; --i) { const Req &req = a[i]; if (req.t) { ans[req.ind] = get_res(req.x, req.y, req.alph); } else { dsu[req.y] = get_dsu_parent(par[req.y]); update_precalc_top(req.y); } } for (int i = 0; i < Q; i++) { if (ans[i] != -1) { cout << " " << ans[i] + 1; } } cout << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); precalc(); int T; cin >> T; for (int t = 1; t <= T; t++) { init(); cout << "Case #" << t << ":"; solve(); } return 0; }