You are given a tree (an acyclic undirected connected graph) with N nodes, and nodes numbered 1,2,3...,N. Each edge has an integer value assigned to it(note that the value can be negative). Each node has a color, white or black. We define dist(a, b) as the sum of the value of the edges on the path from node a to node b.

All the nodes are white initially.

We will ask you to perfrom some instructions of the following form:

  • C a : change the color of node a.(from black to white or from white to black)
  • A : ask for the maximum dist(a, b), both of node a and node b must be white(a can be equal to b). Obviously, as long as there is a white node, the result will alway be non negative.

Input

  • In the first line there is an integer N (N <= 100000)
  • In the next N-1 lines, the i-th line describes the i-th edge: a line with three integers a b c denotes an edge between a, b of value c (-1000 <= c <= 1000)
  • In the next line, there is an integer Q denotes the number of instructions (Q <= 100000)
  • In the next Q lines, each line contains an instruction "C a" or "A"

Output

For each "A" operation, write one integer representing its result. If there is no white node in the tree, you should write "They have disappeared.".

Example

Input:
3
1 2 1
1 3 1
7
A
C 1
A
C 2
A
C 3
A Output:
2
2
0
They have disappeared.

Some new test data cases were added on Apr.29.2008, all the solutions have been rejudged.

树 Link cut tree

查动态点分治时候在别的博客看到一个LCT做Qtree的专题,心生敬仰于是学(chao)了一发。

结论是相比之下还是点分治好写啊,这个状态更新神烦,如果不标准代码比对,我估计得调几个小时吧……

--------

大致就是,把边权迁移到子结点上,先建好LCT。Splay树是二叉树,那么就把当前未激活的边全都扔到set里记录区间答案,然后像平衡树/线段树维护最大子串和那样,记录上边来的最长链,下边来的最长链,左边来的最长链,右边来的最长链……,以及当前结点保存的边长等信息。如果链是从原树的祖先方向来的,维护时候要加那个被迁移的边权,如果是从子孙方向来的,因为下面加过了所以不用再加……

之后花式更新即可。

那个边扔到set里的操作,有人说是传说中的虚边……ダメだ  知らないだ……

n*(logn)^2卡常预警。据说是因为树浅时set多但LCT操作少,而树深时LCT操作多但set小,均摊下来不会超时。

  ↑不会算复杂度的我只能把这当成玄学

 /*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<set>
using namespace std;
const int INF=1e8;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int fir(multiset<int> &s){return s.size()? *s.rbegin():-INF;}
int sec(multiset<int> &s){return s.size()>? *(++s.rbegin()):-INF;}
struct edge{int v,nxt,w;}e[mxn<<];
int hd[mxn],mct=;
void add_edge(int u,int v,int w){
e[++mct].v=v;e[mct].nxt=hd[u];e[mct].w=w;hd[u]=mct;return;
}
int col[mxn],w[mxn];
struct LCT{
multiset<int>chain[mxn],pt[mxn];
int ch[mxn][],fa[mxn];
int len[mxn],L[mxn],R[mxn],mians[mxn],sum[mxn],ans;
bool rev[mxn];
void init(int n){for(int i=;i<=n;i++)L[i]=R[i]=mians[i]=-INF;}
inline bool isroot(int x){return ((ch[fa[x]][]!=x) && (ch[fa[x]][]!=x));}
void pushup(int x){
int lc=ch[x][],rc=ch[x][];
sum[x]=sum[lc]+sum[rc]+len[x];
int cmi=max(w[x],fir(chain[x]));
// printf("tst %d: lc:%d rc:%d sum:%d cmi:%d\n",x,lc,rc,sum[x],cmi);
// printf(" L rc:%d R lc:%d\n",L[rc],R[lc]);
int La=max(cmi,R[lc]+len[x]);//左子树(深度更浅的地方)
int Ra=max(cmi,L[rc]);//右子树
L[x]=max(L[lc],sum[lc]+len[x]+Ra);
R[x]=max(R[rc],sum[rc]+La);
// printf(" L:%d R:%d\n",L[x],R[x]);
mians[x]=max(mians[lc],mians[rc]);
mians[x]=max(mians[x],max(R[lc]+len[x]+Ra,L[rc]+La));
mians[x]=max(mians[x],fir(pt[x]));
mians[x]=max(mians[x],fir(chain[x])+sec(chain[x]));
if(w[x]==)mians[x]=max(w[x],max(mians[x],fir(chain[x])));
return;
}
void rotate(int x){
int y=fa[x],z=fa[y],lc,rc;
if(ch[y][]==x)lc=;else lc=;rc=lc^;
if(!isroot(y))ch[z][ch[z][]==y]=x;
fa[x]=z;fa[y]=x;
ch[y][lc]=ch[x][rc];fa[ch[x][rc]]=y;
ch[x][rc]=y;
pushup(y);
return;
}
// int st[mxn],top;
void Splay(int x){
while(!isroot(x)){
int y=fa[x],z=fa[y];
if(!isroot(y)){
if((ch[z][]==y)^(ch[y][]==x))rotate(x);
else rotate(y);
}
rotate(x);
}
pushup(x);
}
void access(int x){
int y=;
for(;x;x=fa[x]){
Splay(x);
if(ch[x][]){
pt[x].insert(mians[ch[x][]]);
chain[x].insert(L[ch[x][]]);
}
if(y){
pt[x].erase(pt[x].find(mians[y]));
chain[x].erase(chain[x].find(L[y]));
}
ch[x][]=y;
pushup(x);
y=x;
}
}
void change(int x){
access(x);Splay(x);
col[x]^=;if(!col[x])w[x]=;else w[x]=-INF;
pushup(x);
ans=mians[x];
}
/* void query(int x){
access(x);Splay(x);
pushup(x);
ans=mians[x];
}*/
}LT;
void DFS(int u,int fa){
for(int i=hd[u];i;i=e[i].nxt){
if(e[i].v==fa)continue;int v=e[i].v;
LT.fa[v]=u;
LT.len[v]=e[i].w;
DFS(v,u);
LT.chain[u].insert(LT.L[v]);
LT.pt[u].insert(LT.mians[v]);
}
LT.pushup(u);
return;
}
int n,Q;
int main(){
int i,j,u,v,vl;
n=read();
for(i=;i<n;i++){
u=read();v=read();vl=read();
add_edge(u,v,vl);
add_edge(v,u,vl);
}
LT.init(n);
// for(i=1;i<=n;i++)col[i]=0;
DFS(,);
LT.ans=LT.mians[];
// printf("LT:%d\n",LT.ans);
Q=read();char op[];
while(Q--){
scanf("%s",op);
if(op[]=='C')v=read(),LT.change(v);
else{
// LT.query(v);
if(LT.ans<)printf("They have disappeared.\n");
else printf("%d\n",LT.ans);
}
}
return ;
}

SPOJ QTREE4 SPOJ Query on a tree IV的更多相关文章

  1. 【SPOJ QTREE4】Query on a tree IV(树链剖分)

    Description 给出一棵边带权(\(c\))的节点数量为 \(n\) 的树,初始树上所有节点都是白色.有两种操作: C x,改变节点 \(x\) 的颜色,即白变黑,黑变白. A,询问树中最远的 ...

  2. SPOJ VJudge QTREE - Query on a tree

    Query on a tree Time Limit: 851MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Submi ...

  3. SPOJ QTREE4 - Query on a tree IV

    You are given a tree (an acyclic undirected connected graph) with N nodes, and nodes numbered 1,2,3. ...

  4. SPOJ QTREE4 - Query on a tree IV 树分治

    题意: 给出一棵边带权的树,初始树上所有节点都是白色. 有两种操作: C x,改变节点x的颜色,即白变黑,黑变白 A,询问树中最远的两个白色节点的距离,这两个白色节点可以重合(此时距离为0). 分析: ...

  5. SPOJ QTREE4 Query on a tree IV ——动态点分治

    [题目分析] 同bzoj1095 然后WA掉了. 发现有负权边,只好把rmq的方式改掉. 然后T了. 需要进行底(ka)层(chang)优(shu)化. 然后还是T 下午又交就A了. [代码] #in ...

  6. Query on a tree IV SPOJ - QTREE4

    https://vjudge.net/problem/SPOJ-QTREE4 点分就没有一道不卡常的? 卡常记录: 1.把multiset换成手写的带删除堆(套用pq)(作用很大) 2.把带删除堆里面 ...

  7. SPOJ - QTREE4 Query on a tree IV 边分治

    题目传送门 题意:有一棵数,每个节点有颜色,黑色或者白色,树边有边权,现在有2个操作,1修改某个点的颜色, 2询问2个白点的之前的路径权值最大和是多少. 题解: 边分治思路. 1.重构图. 因为边分治 ...

  8. 2019.02.16 spoj Query on a tree IV(链分治)

    传送门 题意简述: 捉迷藏强化版(带有边权,可以为负数) 思路:好吧这次我们不用点分树,我们用听起来更屌的链分治. 直接把树剖成若干条重链,这样保证从任意一个点跳到根节点是不会跳超过logloglog ...

  9. 【SPOJ】375. Query on a tree(树链剖分)

    http://www.spoj.com/problems/QTREE/ 这是按边分类的. 调试调到吐,对拍都查不出来,后来改了下造数据的,拍出来了.囧啊啊啊啊啊啊 时间都花在调试上了,打hld只用了半 ...

随机推荐

  1. BZOJ2118: 墨墨的等式(最短路 数论)

    题意 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存在非负整数解. So ...

  2. 洛谷P3371单源最短路径Dijkstra版(链式前向星处理)

    首先讲解一下链式前向星是什么.简单的来说就是用一个数组(用结构体来表示多个量)来存一张图,每一条边的出结点的编号都指向这条边同一出结点的另一个编号(怎么这么的绕) 如下面的程序就是存链式前向星.(不用 ...

  3. JsRender (js模板引擎)

    最近学习了一下Jsrender模板渲染工具,非常不错,功能比较强大,官网说他是“简单直观 功能强大 可扩展的 快如闪电”确实如此.总结一下!! jsRender 三个最重要的概念:模板.容器和数据. ...

  4. 删除Zend Studio项目

    导入了过大的项目,导致很卡,且Close Project和Delete操作不了,一直无响应. 调整项目目录下的隐藏文件夹,删除对应项目: E:\www\.metadata\.plugins\org.e ...

  5. Linux下启动、停止xampp命令

    启动xampp: /opt/lampp/./lampp start 停止xampp: /opt/lampp/./lampp stop 卸载xampp: rm -rf /opt/lampp

  6. 2019年Vue学习路线图

    https://juejin.im/entry/5c108864f265da61726555ed 官网: https://cn.vuejs.org/index.html js引入地址 https:// ...

  7. A Bug's Life(削弱版食物链)

    Description Background  Professor Hopper is researching the sexual behavior of a rare species of bug ...

  8. 使用virtualbox安装的Ubuntu,窗口分辨率过小,使用增强工具完成和vmtools一样的功能。

    今天用VirtualBox成功装上Ubuntu10.04之后发现了一个问题:默认情况下 ubuntu 的分辨率最高只能设到800*600.但是对于自己的大显示器,在分辨率800*600的ubuntu窗 ...

  9. OpenCV学习笔记(五) 文件存取

    转自输入输出XML和YAML文件 To be filled

  10. linux学习(三) -- lnmp环境切换php版本,并安装相应redis扩展

    原创文章,转载请注明出处   我想配置的环境是ubuntu+nginx+mysql+php+redis,其中php装两个版本,php7和php56 ubuntu+nginx+mysql+php的环境配 ...