NOIP2015 运输计划 LCA+树上差分+二分

题目大意:给定一棵树,再给定k条路径,求把一条边权改为0后使这些路径的最长距离最小。

题解:首先可以想到用二分(谁说想得到的!!!),在枚举出来的长度mid上找到长度大于mid的路径,检查把这些路径中最长的公共边置为0之后的最长链。若长度还是大于mid,则该长度不合法。实际编码中,可以用树上差分来解决边覆盖的问题。
我用倍增写的LCA,如果换成树链剖分或者Tarjan,RMQ什么的会更快~

#include
#include
#include
using namespace std;
const int MAXN = 300001;

int n, m, mid;
int fir[MAXN], nxt[MAXN << 1], to[MAXN << 1], len[MAXN << 1], cnt;
int dep[MAXN], dis[MAXN], fa[20][MAXN];
int tag[MAXN], size[MAXN], d1, Num;

struct Edge{
	int fr, to, len;
}e[MAXN];

inline int read(){
	int k = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9'){if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9'){k = (k << 3) + (k << 1) + (ch^'0'); ch = getchar();}
	return k * f;
}

inline void add_edge(int a, int b, int l){
	len[cnt] = l, to[cnt] = b; nxt[cnt] = fir[a]; fir[a] = cnt++;
}

void dfs1(int u, int f){
	dep[u] = dep[f] + 1; fa[0][u] = f;
	for(int i = 1; (1 << i) <= dep[u]; i++)
		fa[i][u] = fa[i - 1][fa[i - 1][u]];
	for(int i = fir[u]; i != -1; i = nxt[i])
		if(to[i] != f) dis[to[i]] = dis[u] + len[i], dfs1(to[i], u);
}

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

inline bool cmp(Edge a, Edge b){
	return a.len < b.len;
}

void dfs2(int u, int f){ //这里的f记录的是上一条边的编号 
	size[u] = tag[u];//printf("u = %d\n", u);
	for(int i = fir[u]; i != -1; i = nxt[i]){
		if(i == (f ^ 1)) continue; //无向边成对存储 
		int v = to[i];
//		if(v == f) continue;
		dfs2(v, i);
		size[u] += size[v];
	}
	if(u != 1 && size[u] == Num){ //如果被Num条边覆盖
		d1 = max(d1, len[f]);
	}
}

bool check(){
	int l = 0, r = m, Mid1;
	while(l < r){
		Mid1 = (l + r) >> 1;
		if(e[Mid1].len <= mid) l = Mid1 + 1;
		else r = Mid1;
	}
//	printf("l = %d, r = %d\n", l, r);
	Num = m - l + 1;
//	printf("mid = %d, Num = %d\n", mid, Num);

	memset(tag, 0, sizeof(tag)); //tag一定要清零!!! 
	for(int i = l; i <= m; i++){
		int u = e[i].fr, v = e[i].to;
//		printf("lca = %d\n", lca(u, v));
		tag[u]++, tag[v]++, tag[lca(u, v)] -= 2;
	}
	d1 = 0;
	
	dfs2(1, -1);
//	printf("d1 = %d\n", d1);
	if(e[m].len - d1 > mid) return false;
	return true;
}

int main(){
	freopen("in.txt", "r", stdin);
	memset(fir, -1, sizeof(fir));
	n = read(), m = read();
	int maxlen = 0;
	for(int i = 1; i < n; i++){
		int a = read(), b = read(), l = read();
		add_edge(a, b, l), add_edge(b, a, l);
		maxlen = max(maxlen, l);
	}
	dep[0] = -1;
	dfs1(1, 0);
	
	int l, r = 0;
	
	for(int i = 1; i <= m; i++){
		e[i].fr = read(), e[i].to = read();
		e[i].len = dis[e[i].fr] + dis[e[i].to] - (dis[lca(e[i].fr, e[i].to)] << 1);
		r = max(r, e[i].len);
	}
	l = r - maxlen;
//	sort(e + 1, e + m + 1, cmp);
//	for(int i = 1; i <= m; i++)
//		printf("%d ", e[i].len);
//	printf("\n");
	while(l < r){
//		printf("l = %d, r = %d\n", l, r);
		mid = (l + r) >> 1;
		if(check()) r = mid;
		else l = mid + 1;
	}
	printf("%d", l);
	return 0;
}

占坑:在luogu上过了,本校OJ上T了一个点,明天再写Tarjan吧。。

你可能感兴趣的:(LCA,图论,二分,树上差分)