[LOJ#10132]异象石

Description
Adera 是 Microsoft 应用商店中的一款解谜游戏。
异象石是进入 Adera 中异时空的引导物,在 Adera 的异时空中有一张地图。这张地图上
有 N 个点,有 N-1 条双向边把它们连通起来。起初地图上没有任何异象石,在接下来的 M
个时刻中,每个时刻会发生以下三种类型的事件之一:

  1. 地图的某个点上出现了异象石(已经出现的不会再次出现);
  2. 地图某个点上的异象石被摧毁(不会摧毁没有异象石的点);
  3. 向玩家询问使所有异象石所在的点连通的边集的总长度最小是多少。
    请你作为玩家回答这些问题。

Input
第一行有一个整数 N,表示点的个数。
接下来 N-1 行每行三个整数 x,y,z,表示点 x 和 y 之间有一条长度为 z 的双向边。
第 N+1 行有一个正整数 M。
接下来 M 行每行是一个事件,事件是以下三种格式之一:

  • x 表示点 x 上出现了异象石
  • x 表示点 x 上的异象石被摧毁
    ? 表示询问使当前所有异象石所在的点连通所需的边集的总长度最小是多少。
    1 ≤ n, m ≤ 105, 1 ≤ x, y ≤ n, x ≠ y, 1 ≤ z ≤ 10^9

Output
对于每个 ? 事件,输出一个整数表示答案。

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

  • 3
  • 1
    ?
  • 6
    ?
  • 5
    ?
  • 6
  • 3
    ?

Sample Output
5
14
17
10


首先求出dfs序,然后答案就是dfs相邻两点之间距离之和/2,注意开始点和结束点之间也要加上其路径之和。可以画个图理解一下。注意Ans不能每次都重新求,要在加点或删点的时候更新

/*program from Wolfycz*/
#include<set>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline int read(){
	int x=0,f=1;char ch=getchar();
	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')    f=-1;
	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<1)+(x<<3)+ch-'0';
	return x*f;
}
inline void print(int x){
	if (x>=10)	print(x/10);
	putchar(x%10+'0');
}
const int N=1e5;
set<int>st;
int dfn[N+10],ID[N+10],cnt;
ll dis[N+10];
struct S1{
	int pre[(N<<1)+10],now[N+10],child[(N<<1)+10],val[(N<<1)+10],tot;
	int deep[N+10],fa[N+10],Rem[N+10],size[N+10],top[N+10];
	void join(int x,int y,int z){pre[++tot]=now[x],now[x]=tot,child[tot]=y,val[tot]=z;}
	void insert(int x,int y,int z){join(x,y,z),join(y,x,z);}
	void build(int x,int Deep){
		size[x]=1,deep[x]=Deep;
		for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
			if (son==fa[x])	continue;
			fa[son]=x,dis[son]=dis[x]+val[p];
			build(son,Deep+1);
			size[x]+=size[son];
			if (size[son]>size[Rem[x]])	Rem[x]=son;
		}
	}
	void dfs(int x){
		if (!x)	return;
		dfn[ID[x]=++cnt]=x;
		top[x]=Rem[fa[x]]==x?top[fa[x]]:x;
		dfs(Rem[x]);
		for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
			if (son==fa[x]||son==Rem[x])	continue;
			dfs(son);
		}
	}
	int LCA(int x,int y){
		while (top[x]!=top[y]){
			if (deep[top[x]]<deep[top[y]])	swap(x,y);
			x=fa[top[x]];
		}
		return deep[x]<deep[y]?x:y;
	}
}T;
ll query(int x,int T1,int T2){
	set<int>::iterator pre,suc,it=st.find(ID[x]);
	pre=(it==st.begin())?st.end():it;	pre--;
	suc=it,suc++;
	if (suc==st.end())	suc=st.begin();
	int i=dfn[*it],p=dfn[*pre],s=dfn[*suc];
	ll res1=1ll*T1*(dis[p]+dis[s]-2*dis[T.LCA(p,s)]);
	ll res2=1ll*T2*(dis[p]+dis[s]+2*dis[i]-2*(dis[T.LCA(p,i)]+dis[T.LCA(i,s)]));
	return res1+res2;
}
char s[5];
int main(){
	int n=read();
	for (int i=1;i<n;i++){
		int x=read(),y=read(),z=read();
		T.insert(x,y,z);
	}
	T.build(1,1),T.dfs(1);
	ll Ans=0; int m=read();
	for (int i=1;i<=m;i++){
		scanf("%s",s);
		if (s[0]=='+'){
			int x=read();
			st.insert(ID[x]);
			Ans+=query(x,-1,1);
		}
		if (s[0]=='-'){
			int x=read();
			Ans+=query(x,1,-1);
			st.erase(ID[x]);
		}
		if (s[0]=='?')	printf("%lld
",Ans>>1);
	}
	return 0;
}
原文地址:https://www.cnblogs.com/Wolfycz/p/9746657.html