1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <stack> #define INF (100000000000000000ll) #define MAXN (1000000 + 5) #define MAXL (20 + 5) #define LL long long #define pii pair<int, int> using namespace std; struct edg { int from, to, next, cost; } b[MAXN], e[MAXN << 1]; const int lgn = 23; int gb[MAXN], ge[MAXN], cntb, cnte, n, q, deep[MAXN], h[MAXN], d[MAXN][MAXL], cnta, size[MAXN], tn; LL cdeep[MAXN], maxdeep[MAXN], max2deep[MAXN], mindeep[MAXN], min2deep[MAXN]; pii son[MAXN]; bool ish[MAXN]; void adn(int* gx, edg* bx, int& cntx, int from, int to, int cost) { bx[++cntx].next = gx[from]; bx[cntx].from = from, bx[cntx].to = to, bx[cntx].cost = cost; gx[from] = cntx; } void init(int dq) { son[dq].first = ++cnta; deep[dq] = deep[d[dq][0]] + 1; for (int i = 1; i <= lgn; i++) d[dq][i] = d[d[dq][i - 1]][i - 1]; for (int i = ge[dq]; i; i = e[i].next) if (e[i].to != d[dq][0]) { cdeep[e[i].to] = cdeep[dq] + e[i].cost; d[e[i].to][0] = dq; init(e[i].to); } son[dq].second = cnta; } int lca(int x, int y) { if (deep[x] < deep[y]) swap(x, y); for (int i = lgn; i >= 0; i--) if (deep[d[x][i]] >= deep[y]) x = d[x][i]; if (x == y) return x; for (int i = lgn; i >= 0; i--) if (d[x][i] != d[y][i]) x = d[x][i], y = d[y][i]; return d[x][0]; } LL sumc, maxc, minc; stack<int> gary; void dfs(int dq) { size[dq] = ish[dq]; maxdeep[dq] = max2deep[dq] = 0; mindeep[dq] = min2deep[dq] = INF; for (int i = gb[dq]; i; i = b[i].next) { dfs(b[i].to); sumc += (LL)b[i].cost * (tn - size[b[i].to]) * size[b[i].to], size[dq] += size[b[i].to]; if (maxdeep[b[i].to] + b[i].cost > maxdeep[dq]) max2deep[dq] = maxdeep[dq], maxdeep[dq] = maxdeep[b[i].to] + b[i].cost; else if (maxdeep[b[i].to] + b[i].cost > max2deep[dq]) max2deep[dq] = maxdeep[b[i].to] + b[i].cost; if (mindeep[b[i].to] + b[i].cost < mindeep[dq]) min2deep[dq] = mindeep[dq], mindeep[dq] = mindeep[b[i].to] + b[i].cost; else if (mindeep[b[i].to] + b[i].cost < min2deep[dq]) min2deep[dq] = mindeep[b[i].to] + b[i].cost; } if (ish[dq]) { minc = min(minc, mindeep[dq]); min2deep[dq] = mindeep[dq] = 0; maxc = max(maxc, maxdeep[dq]); if (maxdeep[dq] && max2deep[dq]) maxc = max(maxc, maxdeep[dq] + max2deep[dq]); } else { if (mindeep[dq] < INF && min2deep[dq] < INF) minc = min(minc, mindeep[dq] + min2deep[dq]); if (maxdeep[dq] && max2deep[dq]) maxc = max(maxc, maxdeep[dq] + max2deep[dq]); } } bool cmp(int x, int y) { return son[x].first < son[y].first; } void solve() { cntb = 0; sort(h + 1, h + h[0] + 1, cmp); for (int i = h[0]; i > 1; i--) h[++h[0]] = lca(h[i], h[i - 1]); h[++h[0]] = 1; sort(h + 1, h + h[0] + 1, cmp); h[0] = unique(h + 1, h + h[0] + 1) - h - 1; while (!gary.empty()) gary.pop(); for (int i = 1; i <= h[0]; i++) { while (!gary.empty() && son[gary.top()].second < son[h[i]].first) gary.pop(); if (!gary.empty()) adn(gb, b, cntb, gary.top(), h[i], cdeep[h[i]] - cdeep[gary.top()]); gary.push(h[i]); } sumc = 0, maxc = 0, minc = INF; dfs(1); printf("%lld %lld %lld\n", sumc, minc, maxc); for (int i = 1; i <= h[0]; i++) ish[h[i]] = gb[h[i]] = size[h[i]] = 0; } int main() { scanf("%d", &n); for (int i = 1, srx, sry, src = 1; i < n; i++) scanf("%d%d", &srx, &sry), adn(ge, e, cnte, srx, sry, src), adn(ge, e, cnte, sry, srx, src); d[1][0] = 1; init(1); scanf("%d", &q); memset(ish, false, sizeof(ish)); for (int i = 1; i <= q; i++) { scanf("%d", &h[0]); tn = h[0]; for (int j = 1; j <= h[0]; j++) scanf("%d", &h[j]), ish[h[j]] = true; solve(); } return 0; }
|