SPOJ QTREE Query on a tree V
You are given a tree (an acyclic undirected connected graph) with N nodes. The tree nodes are numbered from 1 to N. We define dist(a, b) as the number of edges on the path from node a to node b.
Each node has a color, white or black. All the nodes are black initially.
We will ask you to perfrom some instructions of the following form:
- 0 i : change the color of i-th node(from black to white, or from white to black).
- 1 v : ask for the minimum dist(u, v), node u must be white(u can be equal tov). Obviously, as long as node v is white, the result will always be 0.
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 two integers a b denotes an edge between a and b.
- 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 "0 i" or "1 v"
Output
For each "1 v" operation, print one integer representing its result. If there is no white node in the tree, you should write "-1".
Example
Input:
10
1 2
1 3
2 4
1 5
1 6
4 7
7 8
5 9
1 10
10
0 6
0 6
0 6
1 3
0 1
0 1
1 3
1 10
1 4
1 6 Output:
2
2
2
3
0
改变某点颜色,或者询问离某点最近的白点离询问点的距离
LCT
在LCT上,把未激活的树枝全丢到set里。
花式更新状态,记录链左端点的最近白点距离,右端点的最近白点距离。注意更新左边来(深度更浅处)的答案时,要加上下放到当前点的边权。
统计答案时,access(x),使得x在当前链的最右端,Splay(x)将x提为根,那么结点上记录的“离右端点最近的白点距离”就是答案了。
说着简单,调了三四个小时,也是绝望(然而只是绝望的开始)
/*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
using namespace std;
const int INF=1e7;
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.begin():INF;}
struct edge{
int v,nxt;
}e[mxn<<];
int hd[mxn],mct=;
void add_edge(int u,int v){
e[++mct].v=v;e[mct].nxt=hd[u];hd[u]=mct;return;
}
//
struct node{int ch[],fa;}t[mxn];
multiset<int>pt[mxn];
int len[mxn],L[mxn],R[mxn],sum[mxn],mians[mxn];
int w[mxn],col[mxn];
void init(int n){
for(int i=;i<=n;i++)w[i]=INF,col[i]=;
for(int i=;i<=n;i++){L[i]=R[i]=mians[i]=INF;}
return;
}
inline bool isroot(int x){return (t[t[x].fa].ch[]!=x && t[t[x].fa].ch[]!=x);}
void pushup(int x){
if(!x)return;
int lc=t[x].ch[],rc=t[x].ch[];
sum[x]=len[x]+sum[lc]+sum[rc];
int m0=min(w[x],fir(pt[x]));
// printf("m0:%d\n",m0);
int ml=min(R[lc]+len[x],m0);
int mr=min(L[rc],m0);
L[x]=min(L[lc],sum[lc]+len[x]+mr);
R[x]=min(R[rc],sum[rc]+ml);
return;
}
void rotate(int x){
int y=t[x].fa,z=t[y].fa,lc,rc;
if(t[y].ch[]==x)lc=;else lc=;rc=lc^;
if(!isroot(y)){t[z].ch[t[z].ch[]==y]=x;}
t[x].fa=z;t[y].fa=x;
t[t[x].ch[rc]].fa=y;t[y].ch[lc]=t[x].ch[rc];
t[x].ch[rc]=y;
pushup(y);
return;
}
void Splay(int x){
while(!isroot(x)){
int y=t[x].fa,z=t[y].fa;
if(!isroot(y)){
if((t[y].ch[]==x)^(t[z].ch[]==y))rotate(x);
else rotate(y);
}
rotate(x);
}
pushup(x);
return;
}
void access(int x){
for(int y=;x;x=t[x].fa){
Splay(x);
if(t[x].ch[]){pt[x].insert(L[t[x].ch[]]);}
if(y) pt[x].erase(pt[x].find(L[y]));
t[x].ch[]=y;
pushup(x);
y=x;
}
return;
}
void change(int x){
access(x);Splay(x);
// printf("col:%d\n",col[x]);
col[x]^=;
// printf("col:%d\n",col[x]);
if(!col[x])w[x]=;else w[x]=INF;
// printf("!!!change:%d to %d\n",x,w[x]);
pushup(x);
return;
}
int query(int x){
access(x);
Splay(x);
pushup(x);
return R[x];
}
//LCT
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;
t[v].fa=u;
len[v]=;
DFS(v,u);
// pt[u].insert(mians[v]);
pt[u].insert(L[v]);
}
pushup(u);
return;
}
int n,Q;
int main(){
int i,j,u,v;
n=read();
for(i=;i<n;i++){
u=read();v=read();
add_edge(u,v);
add_edge(v,u);
}
init(n);
DFS(,);
Q=read();
while(Q--){
u=read();v=read();
if(u){
int ans=query(v);
if(ans>=INF)printf("-1\n");
else printf("%d\n",ans);
}
else{change(v);}
}
return ;
}
SPOJ QTREE Query on a tree V的更多相关文章
- SPOJ QTREE Query on a tree V ——动态点分治
[题目分析] QTREE4的弱化版本 建立出分治树,每个节点的堆表示到改点的最近白点距离. 然后分治树上一直向上,取min即可. 正确性显然,不用担心出现在同一子树的情况(不会是最优解),请自行脑补. ...
- SPOJ QTREE Query on a tree 树链剖分+线段树
题目链接:http://www.spoj.com/problems/QTREE/en/ QTREE - Query on a tree #tree You are given a tree (an a ...
- spoj QTREE - Query on a tree(树链剖分+线段树单点更新,区间查询)
传送门:Problem QTREE https://www.cnblogs.com/violet-acmer/p/9711441.html 题解: 树链剖分的模板题,看代码比看文字解析理解来的快~~~ ...
- SPOJ QTREE Query on a tree --树链剖分
题意:给一棵树,每次更新某条边或者查询u->v路径上的边权最大值. 解法:做过上一题,这题就没太大问题了,以终点的标号作为边的标号,因为dfs只能给点分配位置,而一棵树每条树边的终点只有一个. ...
- SPOJ QTREE Query on a tree VI
You are given a tree (an acyclic undirected connected graph) with n nodes. The tree nodes are number ...
- SPOJ QTREE - Query on a tree 【树链剖分模板】
题目链接 引用到的大佬博客 代码来自:http://blog.csdn.net/jinglinxiao/article/details/72940746 具体算法讲解来自:http://blog.si ...
- SPOJ QTREE Query on a tree
题意:给一颗n个点的树,有两种操作CHANGE i ti : 把第i条边的权变为tiQUERY a b : 问点a 到 点b 之间的边的最大权 思路:树剖处理边权.由于是边,所以只需要把边权处理到子节 ...
- SPOJ QTREE Query on a tree ——树链剖分 线段树
[题目分析] 垃圾vjudge又挂了. 树链剖分裸题. 垃圾spoj,交了好几次,基本没改动却过了. [代码](自带常数,是别人的2倍左右) #include <cstdio> #incl ...
- SPOJ - QTREE Query on a tree题解
题目大意: 一棵树,有边权,有两个操作:1.修改一条边的权值:2.询问两点间路径上的边的权值的最大值. 思路: 十分裸的树链剖分+线段树,无非是边权要放到深度大的一端的点上,但是有两个坑爹的地方,改了 ...
随机推荐
- VueX源码分析(3)
VueX源码分析(3) 还剩余 /module /plugins store.js /plugins/devtool.js const devtoolHook = typeof window !== ...
- C/C++程序基础 (三)引用和指针
引用 引用变量与原始变量共享同一地址 int &a = b 则 &a = &b 引用类型的变量必须在声明时初始化,且不可以更换指向变量 常量的引用必须是常量引用,非常量的引用可 ...
- 【Python学习之九】asyncio—异步IO
asyncio 这是python3.4引入的标准库,直接内置对异步IO的支持.asyncio的编程模型就是一个消息循环.从asyncio模块中直接获取一个EventLoop的引用,然后把需要执行的协程 ...
- form中 单选框 input[type="radio"] 分组
在form中有时候需要给单选框分组,这种情况下 可以通过给单选框设置相同的name来进行分组: <html> <head> <title> </title&g ...
- composer 自动加载源码解析
一直在用 composer,最近想看一下具体的原理是什么,就仔细阅读了一下源码,一下是个人理解.在看该文章前最好了解一下 PSR-4 自动加载规范 引入类自动加载文件 # 加载类自动加载文件 requ ...
- thinkphp 3.2.3 - Think.class.php 解析
class Think { public static function start() { // 注册AUTOLOAD方法 spl_autoload_register('Think\Think::a ...
- 传送流(TS)的基础知识
数字电视的TS包和TS流的组成和功能 综合考虑几下几个因素: (1)包的长度不能过短,否则包头开销所占比例过大, 导致传输效率下降 (2)包的长度不能过长,否则在丢失同步的情况下恢复同步的 周期过长, ...
- js中123==123为false的问题--写成123=="123"即可解决问题
项目中遇到过一个问题,js拿到后台返回的一个数字,在if中判断时,出现类似123==123为false的结果, 初步分析原因,应该是返回的是string类型的,但拿来比较的是个number类型的,所以 ...
- SQLAlchemy Script
SQLAlchemy: 1.由于sqlalchemy中没有提供choice方法,所以借助SQLAlchemy-Utils组件提供的choice方法 from sqlalchemy_utils impo ...
- linux环境搭建系列之tomcat安装步骤
前提: Linux centOS 64位 JDK 1.7 安装包从官网上下载 安装Tomcat之前要先安装JDK. 我的JDK是1.7版本的,所以Tomcat版本也选了7的 1.新建目录tomcat ...