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. react组件间的传值方法

    关于react的几个网站: http://react.css88.com/ 小书:http://huziketang.mangojuice.top/books/react/ http://www.re ...

  2. 问题:Could not install packages due to an EnvironmentError: [Errno 13] Permission denied:

    1.安装django 执行pip3 install --user django 2.解决方法:加--user   执行pip3 install --user django

  3. mybatis的优缺点及应用场合

    mybatis框架的优点 与jdbc相比,减少了50%以上的代码量 mybatis是最简单的持久化框架,小巧简单且易学 mybatis想到灵活,不会对应用程序或者数据库的现有设计强加任何影响,SQL写 ...

  4. Python之路-基础数据类型之字符串

    字符串类型 字符串是不可变的数据类型 索引(下标) 我们在日常生活中会遇到很多类似的情况,例如吃饭排队叫号,在学校时会有学号,工作时会有工号,这些就是一种能保证唯一准确的手段,在计算机中也是一样,它就 ...

  5. spring-data-mongodb

    [引入maven依赖] <!-- mongodb spring --> <dependency>     <groupId>org.springframework. ...

  6. Sliding Window POJ - 2823

    Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is m ...

  7. 用 Tensorflow 建立 CNN

    稍稍乱入的CNN,本文依然是学习周莫烦视频的笔记. 还有 google 在 udacity 上的 CNN 教程. CNN(Convolutional Neural Networks) 卷积神经网络简单 ...

  8. 重写BaseAdapter实现ListView

    public class BaseAdapterActivity extends BaseActivity { private ListView base_adapter_listView; priv ...

  9. POJ 3041 Asteroids (二分图最小点覆盖集)

    Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24789   Accepted: 13439 Descr ...

  10. Android 停止调试程序

    现在我知道怎么停掉debug的Android程序了,很简单,进入ddms界面,对着你的进程,kill.