[HEOI2014]大工程 虚树

本文深入探讨了在图论中,如何通过优化算法解决k个节点间道路总和、最短及最长路径的问题。利用深度优先搜索、动态规划等技术,实现对复杂网络的有效分析与路径优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description
给一张图,每次给k个节点,每次询问k个点之间道路的总和,k个点之间的最短路径,最长路径。


Sample Input
10
2 1
3 2
4 1
5 2
6 4
7 5
8 6
9 7
10 9
5
2
5 4
2
10 4
2
5 2
2
6 1
2
6 1


Sample Output
3 3 3
6 6 6
1 1 1
2 2 2
2 2 2


写这题完全是为了存个板子。。。


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long LL;
int _min(int x, int y) {return x < y ? x : y;}
int _max(int x, int y) {return x > y ? x : y;}
int read() {
	int s = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
	return s * f;
}

struct edge {
	int x, y, c, next;
} e[2100000]; int len, last[1100000];
int id, dep[1100000], dfn[1100000], tot[1100000], fa[21][1100000];
int tp, p1, p, pt, ans2, sta[1100000], f[2][1100000];
bool v[1100000];
int a[1100000];
LL ans1;

bool cmp(int a, int b) {return dfn[a] < dfn[b];}

void ins(int x, int y, int c) {
	e[++len].x = x; e[len].y = y; e[len].c = c;
	e[len].next = last[x]; last[x] = len;
}

void bt(int x) {
	dfn[x] = ++id;
	for(int i = 1; (1 << i) <= dep[x]; i++) fa[i][x] = fa[i - 1][fa[i - 1][x]];
	for(int k = last[x]; k; k = e[k].next) {
		int y = e[k].y;
		if(y != fa[0][x]) fa[0][y] = x, dep[y] = dep[x]+ 1, bt(y);
	}
}

int LCA(int x, int y) {
	if(dep[x] > dep[y]) swap(x, y);
	for(int i = 20; i >= 0; i--) if(dep[y] - dep[x] >= (1 << i)){
		y = fa[i][y];
	} if(x == y) return x;
	for(int i = 20; i >= 0; i--) if(fa[i][x] != fa[i][y]){
		x = fa[i][x], y = fa[i][y];
	} return fa[0][x];
}

void dfs1(int x, int fa, int dis) {
	if(dis > p) p = dis, p1 = x; pt += (v[x] == 1);
	for(int k = last[x]; k; k = e[k].next) {
		int y = e[k].y;
		if(y != fa) dfs1(y, x, dis + e[k].c);
	}
}

void dfs2(int x, int fa) {
	tot[x] = (v[x] == 1);
	if(v[x]) f[0][x] = 0;
	for(int k = last[x]; k; k = e[k].next) {
		int y = e[k].y;
		if(y != fa) {
			dfs2(y, x);
			if(f[0][y] + e[k].c < f[0][x]) {
				f[1][x] = f[0][x];
				f[0][x] = f[0][y] + e[k].c;
			} else if(f[0][y] + e[k].c < f[1][x]) f[1][x] = f[0][y] + e[k].c;
			if(f[1][y] + e[k].c < f[1][x]) f[1][x] = f[1][y] + e[k].c;
			tot[x] += tot[y];
			ans1 += (LL)tot[y] * (pt - tot[y]) * e[k].c;
		}
	} ans2 = _min(ans2, f[0][x] + f[1][x]);
}

void clear(int x, int fa) {
	tot[x] = 0; v[x] = 0; f[0][x] = f[1][x] = 999999999;
	for(int k = last[x]; k; k = e[k].next) {
		int y = e[k].y;
		if(fa != y) clear(y, x);
	} last[x] = 0;
}

int main() {
	int n = read();
	for(int i = 1; i < n; i++) {
		int x = read(), y = read();
		ins(x, y, 0), ins(y, x, 0);
	} bt(1);
	len = 0; memset(last, 0, sizeof(last));
	memset(f, 63, sizeof(f));
	int q = read();
	for(int i = 1; i <= q; i++) {
		int k = read(), ans3; ans1 = 0; ans2 = 999999999;
		for(int j = 1; j <= k; j++) a[j] = read(), v[a[j]] = 1;
		sort(a + 1, a + k + 1, cmp); tp = len = 0;
		sta[++tp] = a[1];
		for(int j = 2; j <= k; j++) {
			int lca = LCA(a[j], sta[tp]);
			if(lca == sta[tp]) {
				if(sta[tp] != a[j]) sta[++tp] = a[j];
				continue;
			}
			while(tp > 1 && dep[sta[tp - 1]] >= dep[lca]) ins(sta[tp - 1], sta[tp], dep[sta[tp]] - dep[sta[tp - 1]]), ins(sta[tp], sta[tp - 1], dep[sta[tp]] - dep[sta[tp - 1]]), --tp;
			if(sta[tp] != lca) ins(lca, sta[tp], dep[sta[tp]] - dep[lca]), ins(sta[tp], lca, dep[sta[tp]] - dep[lca]), sta[tp] = lca;
			sta[++tp] = a[j];
		}
		int rt = sta[1];
		while(tp > 1) ins(sta[tp - 1], sta[tp], dep[sta[tp]] - dep[sta[tp - 1]]), ins(sta[tp], sta[tp - 1], dep[sta[tp]] - dep[sta[tp - 1]]), --tp;
		p = pt = 0; dfs1(rt, 0, 0);
		p = pt = 0; dfs1(p1, 0, 0); ans3 = p;
		p = 0; dfs2(rt, 0);
		printf("%lld %d %d\n", ans1, ans2, ans3);
		clear(rt, 0);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值