跳到正文

剪贴板

剪贴板 35dtey01

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include <bits/stdc++.h>
using namespace std;

#define LL long long
#define F(i, x, y) for (int i = x; i <= y; ++i)
#define U(i, y, x) for (int i = y; i >= x; --i)
#define pbk push_back

namespace IO{
	template <typename T> inline void read(T &x) {
		x = 0; char ch = getchar(); bool w = 1;
		while (!isdigit(ch)) {
			if (ch == 0x2d) w = 0;
			ch = getchar();
		}
		while (isdigit(ch)) {
			x = (x << 1) + (x << 3) +(ch ^ 0x30);
			ch = getchar();
		} x = w ? x : -x;
	}
	template <typename T> inline void write(T x) {
		if (x < 0) putchar(0xa), x = -x;
		if (x > 9) write(x / 0xa);
		putchar(x % 0xa + 0x30);
	}
} using namespace IO;

#define N 1000005
const int INF = 1e7;

int n, q, k, h[N];

vector<int> e[N];

int fa[N][20], dep[N], dfn[N], tim;
inline void dfs1(int x, int father) {
	fa[x][0] = father; dep[x] = dep[father] + 1; dfn[x] = ++tim;
	F(i, 0, 18) fa[x][i + 1] = fa[fa[x][i]][i];
	F(i, 0, (int) e[x].size() - 1) {
		int y = e[x][i]; if (y == father) continue;
		dfs1(y, x);
	}
}
inline int lca(int x, int y) {
	if (dep[x] < dep[y]) swap(x, y);
	U(i, 19, 0) if (dep[fa[x][i]] >= dep[y]) x = fa[x][i];
	if (x == y) return x;
	U(i, 19, 0) if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
	return fa[x][0];
}

int sta[N], top;
int vis[N];

vector<int> E[N];

inline int cmp(const int x, const int y) {
	return dfn[x] < dfn[y];
}
inline void connect(int x, int y) {
	E[x].pbk(y);
}
inline void build() {
	sort(h + 1, h + k + 1, cmp); sta[top = 1] = 1;
	F(i, 1, k) {
		if (sta[top] == h[i]) continue;
		int lc = lca(sta[top], h[i]);
		if (lc != sta[top]) {
			while (dep[sta[top - 1]] > dep[lc]) {
				connect(sta[top - 1], sta[top]), --top;
			}
			if (sta[top - 1] == lc) connect(lc, sta[top]), --top;
			else connect(lc, sta[top]), sta[top] = lc;
		}
		sta[++top] = h[i];
	}
	F(i, 1, top - 1) connect(sta[i], sta[i + 1]);
}

int son[N], max1[N], max2[N], min1[N], min2[N];
LL ans1, ans2, ans3;
inline void dfs2(int x) {
	son[x] = 0, max1[x] = max2[x] = -INF, min1[x] = min2[x] = INF;
	if (vis[x]) min1[x] = max1[x] = 0, son[x] = 1;
	F(i, 0, (int) E[x].size() - 1) {
		int y = E[x][i];
		dfs2(y);
		LL dis = dep[y] - dep[x]; son[x]+= son[y];
		ans1+= (LL) son[y] * (LL) (k - son[y]) * dis;
		if (max1[y] + dis > max1[x]) max2[x] = max1[x], max1[x] = max1[y] + dis;
		else if (max1[y] + dis > max2[x]) max2[x] = max1[y] + dis;
		if (min1[y] + dis < min1[x]) min2[x] = min1[x], min1[x] = min1[y] + dis;
		else if (min1[y] + dis < min2[x]) min2[x] = min1[y] + dis;
	}
	ans2 = min(ans2, (LL) min1[x] + min2[x]);
	ans3 = max(ans3, (LL) max1[x] + max2[x]);
}

inline void cls(int x) {
	F(i, 0, (int) E[x].size() - 1) {
		cls(E[x][i]);
	} E[x].clear();
}
inline void clean() {
	F(i, 1, k) vis[h[i]] = 0;
	cls(1);
}

inline void print() {
	write(ans1), putchar(' ');
	write(ans2), putchar(' ');
	write(ans3), puts("");
}

void init() {
	read(n); int x, y;
	F(i, 2, n) read(x), read(y), e[x].pbk(y), e[y].pbk(x);
	dfs1(1, 0);
}
void work() {
	read(q); while (q--) {
		read(k); F(i, 1, k) read(h[i]), vis[h[i]] = 1;
		build(); ans1 = 0, ans2 = INF, ans3 = -INF;
		dfs2(1); print();
		clean();
	}
}

int main() {
	init(); work();
	return 0;
}