[SDOI2015]异象石

传送!


这道题比较有思维难度。
首先考虑这么个事:树上有\(m\)个被标记的点,求使这些点连通的边集的最小总长度。
做法是把这些点按dfs序排序,然后求dfs序相邻的两个点的距离(包括第\(m\)个点和第1个点)。这些距离之和就是答案的二倍!


知道了这个后,这道题就好办了:对于加入或删除一个点,我们用set维护以dfs序为关键字的序列并及时改变答案即可。
用set的时候记得特判,有些小细节不太好写。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<queue>
#include<set>
#include<assert.h>
#include<ctime>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
#define forE(i, x, y) for(int i = head[x], y; ~i && (y = e[i].to); i = e[i].nxt)
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e5 + 5;
const int N = 18;
In ll read()
{
	ll ans = 0;
	char ch = getchar(), las = ' ';
	while(!isdigit(ch)) las = ch, ch = getchar();
	while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
	if(las == '-') ans = -ans;
	return ans;
}
In void write(ll x)
{
	if(x < 0) x = -x, putchar('-');
	if(x >= 10) write(x / 10);
	putchar(x % 10 + '0');
}
In void MYFILE()
{
#ifndef mrclr
	freopen(".in", "r", stdin);
	freopen(".out", "w", stdout);
#endif
}

char op[2];
int n, m;
struct Edge
{
	int nxt, to, w;
}e[maxn << 1];
int head[maxn], ecnt = -1;
In void addEdge(int x, int y, int w)
{
	e[++ecnt] = (Edge){head[x], y, w};
	head[x] = ecnt;
}

ll sum[maxn];
int dfsx[maxn], pos[maxn], cnt = 0;
int dep[maxn], fa[N + 2][maxn];
In void dfs(int now, int _f)
{
	dfsx[now] = ++cnt; pos[cnt] = now;
	for(int i = 1; (1 << i) <= dep[now]; ++i)
		fa[i][now] = fa[i - 1][fa[i - 1][now]];
	forE(i, now, v)
	{
		if(v == _f) continue;
		dep[v] = dep[now] + 1, fa[0][v] = now;
		sum[v] = sum[now] + e[i].w;
		dfs(v, now);
	}
}
In int lca(int x, int y)
{
	if(dep[x] < dep[y]) swap(x, y);
	for(int i = N; i >= 0; --i)
		if(dep[fa[i][x]] >= dep[y]) x = fa[i][x];
	if(x == y) return x;
	for(int i = N; i >= 0; --i)
		if(fa[i][x] ^ fa[i][y]) x = fa[i][x], y = fa[i][y];
	return fa[0][x];
} 

#define sint set<int>::iterator
set<int> s;
ll ans = 0;

In int pre(int x)
{
	sint it = s.lower_bound(dfsx[x]);
	return it == s.begin() ? pos[*(--s.end())] : pos[*(--it)];
}
In int nxt(int x)
{
	sint it = s.lower_bound(dfsx[x]);
	++it;
	return it == s.end()? pos[*s.begin()] : pos[*it];
}

In ll len(int x, int y)
{
	int z = lca(x, y);
	ll tp = sum[x] + sum[y] - (sum[z] << 1);
	return tp;
}

In void update(int x, bool flg)
{	
	if(flg) s.insert(dfsx[x]);
	int a = pre(x), b = nxt(x);
	if(!flg) s.erase(dfsx[x]);
	if(!a) return;
	ll ab = len(a, b), ax = len(a, x), xb = len(x, b);
	if(flg) ans = ans - ab + ax + xb;
	else ans = ans - ax - xb + ab;
}

int main()
{
//	MYFILE();
	Mem(head, -1), ecnt = -1;
	n = read();
	for(int i = 1; i < n; ++i)
	{
		int x = read(), y = read(), w = read();
		addEdge(x, y, w), addEdge(y, x, w);
	}
	dep[1] = 1, dfs(1, 0);
	m = read();
	for(int i = 1; i <= m; ++i)
	{
		scanf("%s", op);;
		if(op[0] == '+') update(read(), 1);
		else if(op[0] == '-') update(read(), 0);
		else write(ans >> 1), enter;
	}
	return 0;	
}
posted @ 2020-10-03 15:33  mrclr  阅读(191)  评论(0编辑  收藏  举报