记录编号 365369 评测结果 AAAAAAAAAA
题目名称 2115.[SPOJ 2666] QTREE4 最终得分 100
用户昵称 ‎MistyEye 是否通过 通过
代码语言 C++ 运行时间 2.613 s
提交时间 2017-01-20 11:53:31 内存使用 65.35 MiB
显示代码纯文本
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
using namespace std;
const int maxn = 100005, maxdep = 32, inf = 0x3f3f3f3f;
struct Edge {
	int next, to, dis;
	Edge(int a=0, int b=0, int c=0) :
		next(a), to(b), dis(c) {}
} e[maxn*6];
int head[maxn<<1], tot = 1, rhead[maxn<<1];
void Add(int u, int v, int w, int *head) 
	{ e[++tot] = Edge(head[u], v, w); head[u] = tot; }
void bAdd(int u, int v, int w, int *head) 
	{ Add(u, v, w, head); Add(v, u, w, head); }

int N, pt, size[maxn<<1], F, Size, mide;
bool vis[maxn*6];
int dis[maxn<<1][maxdep], ihp[maxn][maxdep], White, white[maxn];

struct heap {
	priority_queue<int> q, d;
	inline void refresh() 
	 { while(!d.empty() && q.top()==d.top()) q.pop(), d.pop();}
	void push(int x) { q.push(x); }
	void erase(int x) { d.push(x); }
	int top() { refresh(); return q.top(); }
	void pop() { refresh(); q.pop(); }
	int size() { return q.size()-d.size(); }
	bool empty() { return !size(); }
}; //传说中可删除的堆
heap Hp, hp[maxn*6];

inline int Dis(int m) { 
	if(hp[m].empty() || hp[m^1].empty()) return 0;
	return max(0, hp[m].top()+e[m].dis+hp[m^1].top()); 
} //只有两个都不空才行,因为路径必须经过中间的边 
//显然要对0取max,也可以在输出答案时判
void change(int x) {
	white[x] ^= 1; 
	if(white[x]) ++White;
	else --White;
	for(int i=1; ihp[x][i]; ++i) { 
		//在每一层更新
		int p = ihp[x][i];
		Hp.erase(Dis(p));
		if(white[x]) hp[p].push(dis[x][i]);
		else hp[p].erase(dis[x][i]);
		Hp.push(Dis(p));
	}
}
int getmide(int x, int fa, int res, int p) {
	size[x] = 1; 
	if(p && x<=N) {
		hp[p].push(dis[x][res]);
		ihp[x][res] = p;
	} //只有真正的点才可以扔进去
	for(int i=head[x]; i; i=e[i].next) {
		int to = e[i].to;
		if(vis[i] || to==fa) continue;
		if(p) dis[to][res] = dis[x][res]+e[i].dis;
		size[x] += getmide(to, x, res, p);
		int f = max(size[to], Size-size[to]);
		if(f<=F) F = f, mide = i;
	}
	return size[x];
}
void build(int m, int res) {
	int u = e[m].to, v = e[m^1].to;
	vis[m] = vis[m^1] = 1;
	F = Size = size[u]; getmide(u, -1, res, m); 
	if(size[u]>1) build(mide, res+1); 
	F = Size = size[v];	getmide(v, -1, res, m^1); 
	if(size[v]>1) build(mide, res+1);
	Hp.push(Dis(m)); //初始化
}
void rebuild(int,int);
int main() {
	freopen("QTREE4.in","r",stdin);
	freopen("QTREE4.out","w",stdout);
	scanf("%d", &N);
	for(int i=1,a,b,c; i<N; ++i) {
		scanf("%d%d%d", &a, &b, &c);
		bAdd(a, b, c, head);
	}

	pt = N, rebuild(1, -1);
	memcpy(head, rhead, sizeof head);

	White = N;
	for(int i=1; i<=N; ++i) white[i] = 1;
	F = Size = pt; getmide(1, -1, 0, 0); 
	build(mide, 1);

	int Q,v; char ch[2]; 
	scanf("%d", &Q);
	while(Q--) {
		scanf("%s", ch);
		if(ch[0]=='C') {
			scanf("%d", &v);
			change(v);
		} else {
			if(White==0) puts("They have disappeared.");
			else if(White==1) puts("0");
			else printf("%d\n", Hp.top());
		}
	}
	return 0;
}
queue<pair<int,int> > q;
void rebuild(int x, int fa) {
	for(int i=head[x]; i; i=e[i].next) 
		if(e[i].to!=fa) rebuild(e[i].to, x);
	for(int i=head[x]; i; i=e[i].next) 
		if(e[i].to!=fa) q.push(make_pair(e[i].to, e[i].dis));
	int cnt = q.size();
	pair<int,int> a,b;
	while(cnt>2) {
		a = q.front(); q.pop();
		b = q.front(); q.pop();
		++pt;
		bAdd(pt, a.first, a.second, rhead);
		bAdd(pt, b.first, b.second, rhead);
		q.push(make_pair(pt, 0));
		--cnt;
	}
	while(!q.empty()) {
		a = q.front(); q.pop();
		bAdd(x, a.first, a.second, rhead);
	}
}