点分树 WA 80pts 求助!
查看原帖
点分树 WA 80pts 求助!
681036
OldDriverTree楼主2024/3/26 23:16

rt,样例能过,提交记录

#include<bits/stdc++.h>
#define P pair<int,int>
#define v e.first
#define w e.second
using namespace std;
const int N=1e5+1;
int a[N],fa[N],siz[N],top[N];
int n,m,cnt,Fa[N],dep[N],dis[N];
vector<int> G[N];
vector<P> g[N];
bool del[N];

struct Heap {
	priority_queue<int> p,q; int siz;
	void push(int x) { q.push(x),siz++; } void pop(int x) { p.push(x),siz--; }
	int top() { while (!p.empty()&&p.top()==q.top() ) p.pop(),q.pop(); return q.top(); }
	int get() { if (siz<2) return INT_MIN; int x=top(); q.pop(),siz--; int y=top(); push(x); return x+y; }
}A[N],B[N],anS;

void dfs1(int u) {
	siz[u]=1; for (auto e:g[u]) if (v^fa[u])
	fa[v]=u,dis[v]=dis[u]+w,dep[v]=dep[u]+1,dfs1(v),siz[u]+=siz[v];
}
void dfs2(int u,int son=0) {
	for (auto e:g[u]) if (v^fa[u]&&siz[v]>siz[son]) son=v;
	for (auto e:g[u]) if (v^fa[u]&&v^son) top[v]=v,dfs2(v);
	if (son) top[son]=top[u],dfs2(son);
}
void get_size(int u,int fa) {
	siz[u]=1; for (auto e:g[u]) if (v^fa&&!del[v])
	get_size(v,u),siz[u]+=siz[v];
}
void get_root(int u,int fa,int Siz,int &root) {
	int sz=Siz-siz[u]; for (auto e:g[u]) if (v^fa&&!del[v])
	get_root(v,u,Siz,root),sz=max(sz,siz[v]); if (sz<<1<=Siz) root=u;
}
int build(int u) {
	get_size(u,0),get_root(u,0,siz[u],u),del[u]=true;
	for (auto e:g[u]) if (!del[v]) G[u].push_back(build(v) ); return u;
}
int Dis(int x,int y)
{
	int a=x,b=y;
	while (top[x]^top[y]) {
		if (dep[top[x] ]<dep[top[y] ]) swap(x,y);
		x=fa[top[x] ];
	}
	int LCA=dep[x]<dep[y]?x:y;
	return dis[a]+dis[b]-dis[LCA]*2;
}
void Dfs(int u,int rt) {
	for (int to:G[u]) Dfs(to,rt);
	A[rt].push(Dis(u,Fa[rt]) );
}
void dfs(int u) {
	for (int to:G[u]) Fa[to]=u,dfs(to);
	if (Fa[u]) Dfs(u,u); B[u].push(0);
}
void change(int x)
{
	int u=x;
	while (Fa[u]) {
		int d=Dis(x,Fa[u]);
		anS.pop(B[Fa[u] ].get() );
		if (A[u].siz) B[Fa[u] ].pop(A[u].top() );
		a[x]?A[u].push(d):A[u].pop(d);
		if (A[u].siz) B[Fa[u] ].push(A[u].top() );
		anS.push(B[Fa[u] ].get() ),u=Fa[u];
	}
	anS.pop(B[u].get() );
	if (a[x]) a[x]=0,cnt++,B[x].push(0);
	else a[x]=1,cnt--,B[x].pop(0);
	anS.push(B[u].get() );
}
int main()
{
	cin.tie(0),cin>>n,cnt=n;
	for (int i=1;i<n;i++) {
		int x,y,z; cin>>x>>y>>z;
		g[x].push_back({y,z}),g[y].push_back({x,z});
	}
	dfs1(1),top[1]=1,dfs2(1),dfs(build(1) ),cin>>m;
	for (int i=1;i<=n;i++) for (int j:G[i]) B[i].push(A[j].top() );
	for (int i=1;i<=n;i++) anS.push(B[i].get() );
	while (m--) {
		char c; int x; cin>>c;
		if (c=='C') cin>>x,change(x);
		else {
			if (!cnt) puts("They have disappeared.");
			else if (cnt>1) printf("%d\n",max(anS.top(),0) );
			else puts("0");
		}
	}
	return 0;
}
2024/3/26 23:16
加载中...