For this Question: There is a tree given with N nodes.
You need to answer Q queries. In each query you are given a tree node. You need to choose some nodes such that any two nodes is at most D distance apart. Distance between the two nodes is the number of minimum number of edges on the path between the two nodes. You have to choose the node given in the query as one of its chosen node. You need to maximise the total distance between all the pair of selected nodes.First line contains two intengers N, D
Next N-1 lines contains two integers A, B (1 ≤ A,B ≤ N) which denotes an  edge between that.
Next line contains Q the number of queries
Next Q lines contains a single integer X (1 ≤ X ≤ N) the node that should be chosen
 
<b> Constraints: </b>
1 ≤ N ≤ 500000
1 ≤ D ≤ N -1
1 ≤ Q ≤ 10Output Q lines each line contains a single integer denoting the maximum total distance between all the pair of selected nodes.INPUT
15 3
5 1
3 2
13 3
2 4
3 5
7 6
3 7
3 8
14 9
5 10
1 11
13 12
1 14
14 15
10
1
2
5
6
7
8
10
12
13
15

OUTPUT
57
57
57
40
57
57
57
40
57
29
, I have written this Solution Code: #include <bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll unsigned long long
using namespace std;
inline int read() {
    char c = getchar();
    int x = 0;
    bool f = 0;

    for (; !isdigit(c); c = getchar())
        f ^= !(c ^ 45);

    for (; isdigit(c); c = getchar())
        x = (x << 1) + (x << 3) + (c ^ 48);

    if (f)
        x = -x;

    return x;
}

#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int, int>pii;
typedef vector<int>vi;

#define maxn 1000005
#define inf 0x3f3f3f3f

bool mbe;

struct info {
    int u, v, d, c;
    ll su, sv, res;
} emptyinfo;
bool isempty(info o) {
    return !o.u && !o.v;
}
info MR(info x, info y) {
    if (y.v == x.u)
        swap(y.u, y.v), swap(y.su, y.sv);

    if (y.u == x.v)
        swap(x.u, x.v), swap(x.su, x.sv);

    if (x.v == y.v)
        swap(y.u, y.v), swap(y.su, y.sv), swap(x.u, x.v), swap(x.su, x.sv);

    info z = x;
    z.c = x.c + y.c - 1;
    z.su = x.su + y.su;
    z.sv = x.sv + y.su + 1ll * (y.c - 1) * x.d;
    z.res = x.res + y.res + x.su * (y.c - 1) + y.su * (x.c - 1);
    return z;
}
info MC(info x, info y) {
    if (y.v == x.u)
        swap(y.u, y.v), swap(y.su, y.sv);

    if (y.u == x.v)
        swap(x.u, x.v), swap(x.su, x.sv);

    if (x.v == y.v)
        swap(y.u, y.v), swap(y.su, y.sv), swap(x.u, x.v), swap(x.su, x.sv);

    info z;
    z.u = x.v, z.v = y.v, z.d = x.d + y.d;
    z.c = x.c + y.c - 1;
    z.su = x.sv + y.su + 1ll * (y.c - 1) * x.d;
    z.sv = y.sv + x.su + 1ll * (x.c - 1) * y.d;
    z.res = x.res + y.res + x.su * (y.c - 1) + y.su * (x.c - 1);
    return z;
}

info R(info x, info y) {
    return isempty(x) ? y : (isempty(y) ? x : MR(x, y));
}
info C(info x, info y) {
    return isempty(x) ? y : (isempty(y) ? x : MC(x, y));
}

struct cluster {
    int u, v, sz, len, op;
    info w;
} t[maxn];
int cnt, up[maxn], ls[maxn], rs[maxn];

#define mxn 500005
int n, d, dd;
int fa[mxn], son[mxn], siz[mxn], dep[mxn], top[mxn], dfn[mxn], idx;
vi e[mxn];
void dfs1(int u) {
    siz[u] = 1, dep[u] = dep[fa[u]] + 1;

    if (fa[u]) {
        int i = u;
        t[i].sz = 1, t[i].u = fa[i], t[i].v = i, t[i].len = 1;
        t[i].w = (info) {
            u, fa[u], 1, 2, 1, 1, 1
        };
    }

    for (auto v : e[u]) {
        if (v == fa[u])
            continue;

        fa[v] = u, dfs1(v), siz[u] += siz[v];

        if (siz[v] > siz[son[u]])
            son[u] = v;
    }
}
void dfs2(int u, int tp) {
    top[u] = tp;
    dfn[u] = ++idx;

    if (son[u])
        dfs2(son[u], tp);

    for (auto v : e[u])
        if (v != fa[u] && v != son[u])
            dfs2(v, v);
}
int lca(int u, int v) {
    for (; top[u] != top[v]; u = fa[top[u]])
        if (dep[top[u]] < dep[top[v]])
            swap(u, v);

    return dep[u] < dep[v] ? u : v;
}
int dist(int u, int v) {
    return dep[u] + dep[v] - 2 * dep[lca(u, v)];
}
bool isanc(int u, int v) {
    return dfn[v] >= dfn[u] && dfn[v] <= dfn[u] + siz[u] - 1;
}

int rake(int a, int b) {
    if (!a || !b)
        return a | b;

    int c = ++cnt;
    up[a] = up[b] = c, ls[c] = a, rs[c] = b;
    t[c].sz = t[a].sz + t[b].sz, t[c].op = 0;
    t[c].len = t[a].len;
    t[c].w = R(t[a].w, t[b].w);
    t[c].u = t[a].u, t[c].v = t[a].v;
    return c;
}
int com(int a, int b) {
    if (!a || !b)
        return a | b;

    int c = ++cnt;
    up[a] = up[b] = c, ls[c] = a, rs[c] = b;
    t[c].sz = t[a].sz + t[b].sz, t[c].op = 1;
    t[c].w = C(t[a].w, t[b].w);
    t[c].len = t[a].len + t[b].len;
    vi o = {t[a].u, t[a].v, t[b].u, t[b].v};
    sort(o.begin(), o.end());
    t[c].u = t[a].u, t[c].v = t[b].v;
    return c;
}

struct cmp {
    bool operator()(int x, int y) {
        return t[x].sz > t[y].sz;
    }
};
int lid[mxn], zid[mxn], rt;
int sid[mxn], su[mxn], len;
int get(int l, int r) {
    if (l == r)
        return lid[sid[l]];

    int sum = 0, nw = 0, mid = 0;
    For(i, l, r)sum += t[lid[sid[i]]].sz;
    For(i, l, r - 1) {
        nw += t[lid[sid[i]]].sz;

        if (nw * 2 >= sum || i == r - 1) {
            mid = i;
            break;
        }
    }
    return com(get(l, mid), get(mid + 1, r));
}
void solve(int u) {
    priority_queue<int, vector<int>, cmp>q;

    for (auto v : e[u])
        if (v != fa[u] && v != son[u])
            solve(v), q.push(zid[v]);

    while (q.size() > 1) {
        int x = q.top();
        q.pop();
        int y = q.top();
        q.pop();
        q.push(rake(x, y));
    }

    if (q.size())
        lid[u] = q.top();

    if (u != 1)
        lid[u] = rake(u, lid[u]);

    if (son[u])
        solve(son[u]);

    if (u != top[u])
        return;

    len = 0;

    for (int x = u; x; x = son[x])
        sid[++len] = x;

    if (u != 1)
        zid[u] = get(1, len);
    else
        zid[u] = rake(lid[u], get(2, len));
}

vector<info>f[maxn][2], g[maxn][2];
bool pd(int x, int y) {
    return t[x].v == t[y].u || t[x].v == t[y].v;
}
int P(int x, int y) {
    return y ? t[x].v : t[x].u;
}
info F(int x, int y, int z) {
    if (z < 0)
        return emptyinfo;

    if (x <= n)
        return z < 0 ? emptyinfo : t[x].w;

    return f[x][y][min(max(z, 0), (int)f[x][y].size() - 1)];
}
info G(int x, int y, int z) {
    //  --z;
    if (z < 0)
        return emptyinfo;

    return g[x][y][min(max(z, 0), (int)g[x][y].size() - 1)];
}

bool med;

void dfs3(int u) {
    if (u <= n)
        return;

    For(i, 0, 1)f[u][i].resize(min(t[u].sz + 1, dd + 2) - 1, emptyinfo);
    int ls =::ls[u], rs =::rs[u];
    dfs3(ls), dfs3(rs);
    int l = pd(ls, rs), r = pd(rs, ls), o = (t[u].v == P(ls, !l));
    int lim = f[u][0].size();

    if (t[u].op) {
        For(i, 0, lim - 1) {
            f[u][o][i] = C(F(ls, !l, i), F(rs, r, i - t[ls].len));
            f[u][!o][i] = C(F(rs, !r, i), F(ls, l, i - t[rs].len));
        }
    } else {
        For(i, 0, lim - 1) {
            f[u][o][i] = R(F(ls, !l, i), F(rs, r, i - t[ls].len));
            f[u][!o][i] = R(F(ls, l, i), F(rs, r, i));
        }
    }

    if (t[u].sz <= d / 2) {
        for (int x : {
                    ls, rs
                })
            For(y, 0, 1)
            f[x][y].clear(), f[x][y].shrink_to_fit();
    }
}
bool need[maxn];
void jump(int u) {
    while (up[u] && t[up[u]].sz <= d / 2)
        u = up[u];

    if (u != rt)
        need[u] = 1;
}
void dfs4(int u) {
    if (u <= n || t[u].sz <= d / 2)
        return;

    int ls =::ls[u], rs =::rs[u], l = pd(ls, rs), r = pd(rs, ls), o = (t[u].v == P(ls, !l));

    for (int x : {
                ls, rs
            })
        For(y, 0, 1)g[x][y].resize(dd + 2, emptyinfo);

    if (t[u].op) {
        For(i, 1, dd + 1) {
            g[ls][!l][i] = G(u, o, i);
            g[ls][l][i] = C(F(rs, r, i - 1), G(u, !o, i - t[rs].len));
            g[rs][!r][i] = G(u, !o, i);
            g[rs][r][i] = C(F(ls, l, i - 1), G(u, o, i - t[ls].len));
        }
    } else {
        For(i, 1, dd + 1) {
            g[ls][!l][i] = G(u, o, i);
            g[ls][l][i] = R(G(u, !o, i), F(rs, r, i - 1));
            g[rs][r][i] = R(R(F(ls, l, i - 1), G(u, !o, i)), G(u, o, i - t[ls].len));
        }
    }

    if (!need[u]) {
        For(o, 0, 1)
        g[u][o].clear(), g[u][o].shrink_to_fit();
    }

    dfs4(ls), dfs4(rs);
}

void init() {
    cnt = n;
    dfs1(1), dfs2(1, 1);
    solve(1);
    rt = zid[1];
    dfs3(rt);

    bool MED;
    //  printf("%.6lf %.6lf\n",(&mbe-&MED)/1024576.0,(&med-&MED)/1024576.0);
    For(i, 1, n) jump(i);
    For(i, 0, 1)g[rt][i].resize(dd + 1, emptyinfo);
    dfs4(rt);
    //  if(n>=400000)exit(0);

    For(i, 2, cnt) for (auto &tmp : g[i][0])
        tmp = C(tmp, t[i].w);
}
info ask(int u) {
    int x = u;

    if (u == 1)
        u = 2;

    if (d % 2 == 0) {
        while (up[u] && t[up[u]].sz <= d / 2)
            u = up[u];

        if (u == rt)
            return t[rt].w;

        return C(G(u, 0, dd - dist(x, t[u].u)), G(u, 1, dd - dist(x, t[u].v)));
    }

    while (up[u] && t[up[u]].sz <= d / 2)
        u = up[u];

    if (u == rt)
        return t[rt].w;

    // t[u].u is upon t[u].v
    //  cout<<"u: "<<x<<" "<<u<<" "<<t[up[u]].sz<<"\n";
    if (isanc(t[u].u, fa[x]) && isanc(x, t[u].v))
        return C(G(u, 0, d / 2 + 1 - dist(x, t[u].u)), G(u, 1, d / 2 - dist(x, t[u].v)));

    return C(G(u, 0, d / 2 + 1 - dist(x, t[u].u)), G(u, 1, d / 2 + 1 - dist(x, t[u].v)));
}

ll ans[mxn];
int D[mxn];
void getd(int u, int pa) {
    if (pa)
        D[u] = D[pa] + 1;

    for (int v : e[u])
        if (v != pa)
            getd(v, u);
}


signed main() {
    //  freopen("17.1.in","r",stdin);
    //  freopen("my.out","w",stdout);
    //  printf("%.6lf\n",(&mbe-&med)/1024576.0);
    n = read(), d = read();
    dd = d / 2;
    For(i, 2, n) {
        int u = read(), v = read();
        e[u].pb(v), e[v].pb(u);
    }
    init();
    For(i, 1 + d % 2, n) ans[i] = ask(i).res;
    int q = read();
    For(_, 1, q) {
        int u = read();

        if (d == 0) {
            puts("0");
            continue;
        }

        D[u] = 0, getd(u, 0);
        ll res = 0;

        if (d % 2 == 0) {

            For(i, 1, n) if (D[i] <= d / 2)
                res = max(res, ans[i]);
        } else {

            For(i, 2, n) if (D[i] <= d / 2 || D[fa[i]] <= d / 2)
                res = max(res, ans[i]);
        }

        printf("%lld\n", res);
    }
    return 0;
}, In this Programming Language: C++, Now tell me if this Code is compilable or not?