[hihocoder#1381] Little Y ' s Tree

Source: Internet
Author: User

[hihocoder#1381] Little Y ' s Tree

Question Description

Small Y has a tree of n nodes, and each edge has a positive edge.

Little J has a Q query, each time little J will delete the tree in the K-side, the tree is divided into k+1 connected blocks. Little J wants to know the distance of the furthest point in each connected block.

The inquiry here is independent of each other, that is, each time in the small y of the original tree to operate.

Input

The first line is an integer n, and the next n-1 line is three integers per line u,v,w, where line I represents the edge edge of WI, connected to the Ui,vi two points.

The next line is an integer q, which indicates that there is a Q group asking.

For each set of queries, the first line is a positive integer k, followed by a row of k different integers between 1 and n-1, representing the number of deleted edges.

1<=n,q,σk<=105, 1<=w<=109

Output

A total of Q lines, one integer per line, indicates the answer to the question.

Input example

5 1 2 2 2 3 3 2 4 4 4 5 2 3 4 1 2 3 4 1 4 2 2 3

Output example

0 7 4

Data size and conventions

See " Input "

Exercises

We will create a virtual tree for each deletion of a node with a large depth of all edges as a key point. Then we find that we can maintain the connectivity of the interval, and after all the nodes are sorted from small to large in the DFS sequence, merge the connected information with the segment tree. For two connected blocks A and B, if the diameter of a is (a, b) and the diameter of B is (c, D), then the diameter of a U B is (A, b), (c, D), (A, C), (A, D), (b, C), (b, D) Six cases, we go to a maximum value. For a virtual tree, we follow the depth from large to small to query the corresponding interval of the connection block diameter, the cumulative answer, and then hit the deletion mark, and finally remember to restore the deletion mark.

Yes, this question to O (1) to seek LCA.

#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cctype > #include <algorithm>using namespace Std;int read () {int x = 0, f = 1; char c = GetChar (); while (!isdigit (c)) {if ( c = = '-') f =-1; c = GetChar (); }while (IsDigit (c)) {x = x * + C-' 0 '; c = GetChar ();} return x * f;} #define MAXN 100010#define maxm 200010#define maxlog 20#define oo 2147483647#define LL long longint N, M, Head[maxn], Next [MAXM], TO[MAXM], dist[maxm];struct Edge {int A, b, C; Edge () {}edge (int _1, int _2, int _3): A (_1), B (_2), C (_3) {}} es[maxn];void Addedge (int A, int b, int c) {To[++m] = b; di ST[M] = c; NEXT[M] = Head[a]; Head[a] = M;swap (A, b); TO[++M] = b; DIST[M] = c; NEXT[M] = Head[a]; Head[a] = M;return;} int DEP[MAXN], LIST[MAXM], cl, POS[MAXN], ORD[MAXN], CLO, ORDR[MAXN], POS[MAXN]; LL dep[maxn];void Build (int u, int fa) {LIST[++CL] = u; pos[u] = cl; Ord[u] = ++clo; Pos[clo] = u;for (int e = Head[u], e; e = next[e]) if (to[e]! = FA) {Dep[to[e]] = Dep[u] + 1;dep[to[e]] = Dep[u] + dist[e];build (to[e], u); list[++cl] = u;} Ordr[u] = Clo;return;} int LCA[MAXLOG][MAXM], log[maxm];void rmq_init () {log[1] = 0;for (int i = 2; I <= cl; i++) Log[i] = log[i>>1] + 1; for (int i = 1; I <= cl; i++) Lca[0][i] = list[i];for (int j = 1; (1 << j) <= CL; J + +) for (int i = 1; i + (1 << J)-1 <= cl; i++) {int a = Lca[j-1][i], B = lca[j-1][i+ (1<<j-1)];if (Dep[a] &l T DEP[B]) lca[j][i] = A;else Lca[j][i] = b;} return;} int LCA (int a, int b) {int l = min (Pos[a], pos[b]), r = Max (Pos[a], pos[b]); int t = Log[r-l+1];a = Lca[t][l]; b = Lca[t][r -(1<<t) +1];return Dep[a] < Dep[b]? A:B;} LL Calc (int a, int b) {return dep[a] + dep[b]-(Dep[lca (A, b)] << 1);} struct Node {bool Hasv;int A, B; LL Len; Node () {}node (int _1, int _2, LL _3, bool _4): A (_1), B (_2), Len (_3), HASV (_4) {}} Ns[maxn<<2];void maintain (node&am P O, node LC, node rc) {O.len = -1;if (!LC.HASV &&!rc.hasv) {o.len = O.HASV = 0; return; }if (!LC.HASV) {o = Rc;return;} if (!RC.HASV) {o = Lc;return;} O.HASV = 1;if (O.len < LC. Len) O.len = LC. Len, O.A = LC. A, O.B = LC. B;if (O.len < RC. Len) O.len = RC. Len, O.A = RC. A, O.B = RC. B LL d = Calc (LC. A, RC. A); if (O.len < D) O.len = d, O.A = LC. A, O.B = RC. A;d = Calc (LC. A, RC. B); if (O.len < D) O.len = d, O.A = LC. A, O.B = RC. B;d = Calc (LC. B, RC. A); if (O.len < D) O.len = d, O.A = LC. B, O.B = RC. A;d = Calc (LC. B, RC. B); if (O.len < D) O.len = d, O.A = LC. B, O.B = RC. B;return;} void build (int L, int R, int o) {if (L = = R) Ns[o] = Node (Pos[l], Pos[r], 0, 1), else {int M = L + R >> 1, LC = O < < 1, rc = LC | 1;build (L, M, LC); Build (M+1, R, RC), maintain (Ns[o], NS[LC], NS[RC]); NS[O].HASV = 1;} printf ("seg[%d,%d]:%d%d%lld\n", L, R, Ns[o]. A, Ns[o]. B, Ns[o]. Len); return;} void Update (int L, int R, int o, int ql, int qr, bool v) {if (QL <= L && R <= qr) NS[O].HASV = v;else {int M = L + R >> 1, LC = O << 1, rc = LC | 1;if (QL <= M) update (L,M, LC, QL, QR, v), if (QR > M) Update (m+1, R, RC, QL, QR, V), maintain (Ns[o], NS[LC], NS[RC]);} return;} Node query (int L, int R, int o, int ql, int qr) {if (QL <= L && r <= qr) return ns[o];int M = L + R >> 1, LC = O << 1, rc = LC | 1; Node ans ( -1,-1,-1, 0); if (QL <= M) {Node tmp = query (L, M, LC, QL, QR), TT (0, 0,-1, 1); Maintain (TT, TMP, ans); HASV) ans = tt;} if (qr > M) {Node tmp = query (m+1, R, RC, QL, QR), TT (0, 0,-1, 1); Maintain (TT, TMP, ANS); if (TMP.HASV) ans = tt;} printf ("[%d]%d]%d%d%d%lld (%d) \nans:%lld\n", L, R, O, QL, QR, Ns[o]. Len, NS[O].HASV, ans. Len); return ans;} BOOL CMP (int a, int b) {return pos[a] < pos[b];} int PSI[MAXN], CPI, PS[MAXN], CP, Vis[maxn];bool flg[maxn];struct vtree {int m, HEAD[MAXN], NEXT[MAXM], TO[MAXM]; LL DIST[MAXM], ans;void init () {ans = m = 0;return;} void Addedge (int a, int b, LL c) {//printf ("Add2:%d%d%lld\n", A, B, c); TO[++M] = b; DIST[M] = c; NEXT[M] = Head[a]; Head[a] = M;swap (A, b); TO[++M] = b;DIST[M] = c; NEXT[M] = Head[a]; Head[a] = M;return;}  void Dfs (int u, int fa) {for (int e = head[u]; e; e = next[e]) if (to[e]! = FA) Dfs (to[e], u), if (Flg[u]) {Node tmp = query (1, N, 1, Ord[u], ordr[u]); Flg[u] = 0;if (TMP.HASV) ans + tmp. Len;//printf ("at%d tmp:%lld [%d,%d]\n", U, tmp.) Len, Ord[u], ordr[u]); Update (1, N, 1, Ord[u], Ordr[u], 0);} return;} void dfs2 (int u, int fa) {for (int e = head[u]; e; e = next[e]) if (to[e]! = FA) DFS2 (To[e], u); update (1, N, 1, ord[u], ordr[ U], 1); Head[u] = 0;return;}} Vt;int Main () {n = read (); for (int i = 1; i < n; i++) {int a = read (), B = Read (), C = Read (); Addedge (A, B, c); Es[i] = Edge (A, B, c);} Build (1, 0); Rmq_init ();//for (int i = 1; I <= cl; i++) printf ("%d%c", Lca[0][i], I < CL? ': ' \ n ');//for (int i = 1; I <= n; i++) printf ("%d%c", Pos[i], I < n? ': ' \ n '); build (1, N, 1); int q = read ();//for (int i = 1; I <= n; i++) printf ("%d%c", Ord[i], I < n? ': ' \ n '), while (q--) {int CPI = read (); CP = 0;for (int i = 1; I <= CPI; i++) {int e = read (), u = dep[es[e].a] < dep[es[e].b]? ES[E].B:ES[E].A;PS[++CP] = Psi[i] = U;VIS[PS[CP]] = q + 1;FL G[PS[CP]] = 1;} if (vis[1]! = q + 1) PS[++CP] = 1, flg[1] = 1;sort (psi + 1, PSI + CPI + 1, CMP); for (int i = 1; i < CPI; i++) {int c = LC A (Psi[i], psi[i+1]); if (vis[c]! = q + 1) vis[c] = q + 1, PS[++CP] = c;} Sort (PS + 1, PS + CP + 1, CMP);//printf ("Mother_fuck:"); for (int i = 1; I <= CP; i++) printf ("%d%c", Ps[i], I < CP? ': ' \ n '); Vt.init (); for (int i = 1; i < CP; i++) {int a = Ps[i], B = ps[i+1], C = LCA (A, b); Vt. Addedge (b, C, dep[b]-dep[c]);} Vt.dfs (1, 0); VT.DFS2 (1, 0);p rintf ("%lld\n", Vt.ans);} return 0;} /*131 2 11 3 12 4 12 5 12 6 13 7 13 8 13 9 17 12 112 13 19 10 110 11 1*/

[hihocoder#1381] Little Y ' s Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.