SPOJ QTREE3 - Query on a tree again!
You are given a tree (an acyclic undirected connected graph) with N nodes. The tree nodes are numbered from 1 to N. In the start, the color of any node in the tree is white.
We will ask you to perfrom some instructions of the following form:
- 0 i : change the color of the i-th node (from white to black, or from black to white);
or - 1 v : ask for the id of the first black node on the path from node 1 to node v. if it doesn't exist, you may return -1 as its result.
Input
In the first line there are two integers N and Q.
In the next N-1 lines describe the edges in the tree: a line with two integers a b denotes an edge between a and b.
The next Q lines contain instructions "0 i" or "1 v" (1 ≤ i, v ≤ N).
Output
For each "1 v" operation, write one integer representing its result.
Example
Input:
9 8
1 2
1 3
2 4
2 9
5 9
7 9
8 9
6 8
1 3
0 8
1 6
1 7
0 2
1 9
0 2
1 9 Output:
-1
8
-1
2
-1
Constraints & Limits
There are 12 real input files.
For 1/3 of the test cases, N=5000, Q=400000.
For 1/3 of the test cases, N=10000, Q=300000.
For 1/3 of the test cases, N=100000, Q=100000.
一棵树,点数<=100000,初始全是白点,支持两种操作:
1.将某个点的颜色反色。
2.询问某个点至根节点路径上第一个黑点是哪个。
树链剖分
注意到询问的链都是(1,v)
在线段树上维护区间内深度最浅的黑色点位置即可,注意线段树结点到原树的反向映射
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int INF=1e9;
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;
}
struct edge{int v,nxt;}e[mxn<<];
int hd[mxn],mct=;
int n,q;
void add_edge(int u,int v){
e[++mct].v=v;e[mct].nxt=hd[u];hd[u]=mct;return;
}
struct node{
int fa,son;
int top,size;
int w;
}t[mxn];
int sz=;
int dep[mxn];
int id[mxn];
void DFS1(int u,int fa){
dep[u]=dep[fa]+;
t[u].size=;
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;if(v==fa)continue;
t[v].fa=u;
DFS1(v,u);
t[u].size+=t[v].size;
if(t[v].size>t[t[u].son].size)
t[u].son=v;
}
return;
}
void DFS2(int u,int top){
t[u].w=++sz;t[u].top=top;
id[sz]=u;
if(t[u].son)DFS2(t[u].son,top);
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(v==t[u].fa || v==t[u].son)continue;
DFS2(v,v);
}
return;
}
struct SGT{
int ps;
int c;
}st[mxn<<];
void Build(int l,int r,int rt){
st[rt].ps=INF;
if(l==r)return;
int mid=(l+r)>>;
Build(l,mid,rt<<);Build(mid+,r,rt<<|);
return;
}
void update(int p,int l,int r,int rt){
if(l==r){
st[rt].c^=;
st[rt].ps=(st[rt].c)?l:INF;
return;
}
int mid=(l+r)>>;
if(p<=mid)update(p,l,mid,rt<<);
else update(p,mid+,r,rt<<|);
st[rt].ps=min(st[rt<<].ps,st[rt<<|].ps);
return;
}
int query(int L,int R,int l,int r,int rt){
if(L<=l && r<=R){return st[rt].ps;}
int mid=(l+r)>>;
int res=INF;
if(L<=mid)res=min(res,query(L,R,l,mid,rt<<));
if(R>mid && res>mid)res=min(res,query(L,R,mid+,r,rt<<|));
return res;
}
int Qt(int x,int y){
int res=INF;
while(t[x].top!=t[y].top){
if(dep[t[x].top]<dep[t[y].top])swap(x,y);
res=min(res,query(t[t[x].top].w,t[x].w,,n,));
x=t[t[x].top].fa;
}
if(dep[x]>dep[y])swap(x,y);
res=min(res,query(t[x].w,t[y].w,,n,));
return res;
}
int main(){
int i,j,u,v;
n=read();q=read();
for(i=;i<n;i++){
u=read();v=read();
add_edge(u,v);
add_edge(v,u);
}
DFS1(,);
DFS2(,);
Build(,n,);
while(q--){
u=read();v=read();
if(!u)
update(t[v].w,,n,);
else{
int ans=Qt(,v);
if(ans>=INF){printf("-1\n");continue;}
ans=id[ans];
printf("%d\n",ans);
}
}
return ;
}
SPOJ QTREE3 - Query on a tree again!的更多相关文章
- SPOJ QTREE3 Query on a tree again! ——Link-Cut Tree
[题目分析] QTREE2,一看是倍增算法,太懒了,不写了.( ̄_, ̄ ) QTREE3,树链剖分可以做,发现链上的问题LCT也很好做. 要是子树问题貌似可以DFS序. 然后就成LCT模板题了. 考前 ...
- QTREE3 spoj 2798. Query on a tree again! 树链剖分+线段树
Query on a tree again! 给出一棵树,树节点的颜色初始时为白色,有两种操作: 0.把节点x的颜色置反(黑变白,白变黑). 1.询问节点1到节点x的路径上第一个黑色节点的编号. 分析 ...
- SPOJ 375. Query on a tree (树链剖分)
Query on a tree Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Ori ...
- 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 375 Query on a tree(树链剖分,线段树)
Query on a tree Time Limit: 851MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Sub ...
- 动态树(Link Cut Tree) :SPOJ 375 Query on a tree
QTREE - Query on a tree #number-theory You are given a tree (an acyclic undirected connected graph) ...
- SPOJ 375. Query on a tree (动态树)
375. Query on a tree Problem code: QTREE You are given a tree (an acyclic undirected connected graph ...
- SPOJ PT07J - Query on a tree III(划分树)
PT07J - Query on a tree III #tree You are given a node-labeled rooted tree with n nodes. Define the ...
- spoj 913 Query on a tree II (倍增lca)
Query on a tree II You are given a tree (an undirected acyclic connected graph) with N nodes, and ed ...
随机推荐
- /^/m|/$/m|\b|\B|$&|$`|$'|变量捕获|()?|(?:pattern)|(?<LABEL>PATTERN)|$+{LABEL}|(|)|\g{LABEL}
#!/usr/bin/perl use strict; use warnings; $_=' $$ oinn &&& ninq kdownc aninp kkkk'; if ( ...
- Postman 没有走hosts文件
问题: 在Windows10系统中,从官方下载Postman安装并登录后,创建一个请求并执行.但这个请求并没有走hosts文件中定义的192.168.33.10主机,而是走到了线上的主机. 分析: 通 ...
- 拓扑排序 topsort
拓扑排序 对一个有向无环图(Directed Acyclic Graph简称DAG)G进行拓扑排序,是将G中所有顶点排成一个线性序列,使得图中任意一对顶点u和v,若边(u,v)∈E(G),则u在线性序 ...
- centos 7 忘记root 密码
@@@@首先开启系统,出现下图界面以后,按e键. @@@使用下放下箭头找到图中的位置,在下图中 修改 ro 为 rw , 添加init=sysroot/bin/sh @@@按Ctrl + x 进入单用 ...
- LeetCode1090. 受标签影响的最大值
问题: 我们有一个项的集合,其中第 i 项的值为 values[i],标签为 labels[i]. 我们从这些项中选出一个子集 S,这样一来: |S| <= num_wanted 对于任意的标签 ...
- strace用法
strace -- trace system calls and signals strace是Linux环境下的一款程序调试工具,用来监察一个应用程序所使用的系统调用及它所接收的系统信 ...
- Python头脑风暴3
驾校是个暴利行业 如果有高学历靠谱的IT人员做驾校教练等等等等.... Python虽然难做企业级应用,但Python是全球个人自定义应用的首选!!!没有之一,所有语言最快的开发速度,最个性化的私人定 ...
- stm32之Cortex系统定时器(SysTick)
转载自:http://www.21ic.com/app/mcu/201811/781135.htm SysTick时钟,俗称“嘀嗒定时器”,它能按设定的时间产生一次中断.控制工程代码中随处可见形如 ...
- POJ:2777-Count Color(线段树+状压)
Count Color Time Limit: 1000MS Memory Limit: 65536K Description Chosen Problem Solving and Program d ...
- redis--py链接redis【转】
请给原作者点赞--> 原文链接 一.redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链 ...