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!的更多相关文章

  1. SPOJ QTREE3 Query on a tree again! ——Link-Cut Tree

    [题目分析] QTREE2,一看是倍增算法,太懒了,不写了.( ̄_, ̄ ) QTREE3,树链剖分可以做,发现链上的问题LCT也很好做. 要是子树问题貌似可以DFS序. 然后就成LCT模板题了. 考前 ...

  2. QTREE3 spoj 2798. Query on a tree again! 树链剖分+线段树

    Query on a tree again! 给出一棵树,树节点的颜色初始时为白色,有两种操作: 0.把节点x的颜色置反(黑变白,白变黑). 1.询问节点1到节点x的路径上第一个黑色节点的编号. 分析 ...

  3. SPOJ 375. Query on a tree (树链剖分)

    Query on a tree Time Limit: 5000ms Memory Limit: 262144KB   This problem will be judged on SPOJ. Ori ...

  4. 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 ...

  5. spoj 375 Query on a tree(树链剖分,线段树)

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

  6. 动态树(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) ...

  7. SPOJ 375. Query on a tree (动态树)

    375. Query on a tree Problem code: QTREE You are given a tree (an acyclic undirected connected graph ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. MySQL索引类型及优化

    索引是快速搜索的关键.MySQL索引的建立对于MySQL的高效运行是很重要的.下面介绍几种常见的MySQL索引类型. 在数据库表中,对字段建立索引可以大大提高查询速度.假如我们创建了一个 mytabl ...

  2. 01_5_SERVLET为什么有2个init方法

    01_5_SERVLET为什么有2个init方法 1. 在web.xml配置初始化参数 <servlet> <servlet-name>TestInitServlet</ ...

  3. NSOperation、NSOperationQueue

    NSOperation.NSOperationQueue NSOperation 和 NSOperationQueue 配合使用也能实现多线程. NSOperation 继承于 NSObject,是一 ...

  4. python有三元运算符吗

    所属网站分类: python基础 > 语法,变量,运算符 作者:goodbody 链接: http://www.pythonheidong.com/blog/article/12/ 来源:pyt ...

  5. 掌握这些Python代码技巧,编程至少快一半!

    被人工智能捧红的 Python 已是一种发展完善且非常多样化的语言,其中肯定有一些你尚未发现的功能.本文或许能够让你学到一些新技巧. ​ Python 是世界上最流行.热门的编程语言之一,原因很多,比 ...

  6. python2和python3,字典和json

    Python2的标准数据类型有: Numbers (数字) String (字符串) List (列表) Tuple (元组) Dictionary (字典) Python3的标准数据类型有: Num ...

  7. V4L2使用V4L2_MEMORY_USERPTR和V4L2_MEMORY_MMAP的区别

    视频应用可以通过两种方式从V4L2驱动申请buffer 1. USERPTR, 顾名思义是用户空间指针的意思,应用层负责分配需要的内存空间,然后以指针的形式传递给V4L2驱动层,V4L2驱动会把cap ...

  8. 妹子图爬取__RE__BS4

    妹子图爬取 页面链接 感谢崔大佬: 原文链接 正则实现代码: import requests import re import os import random class mzitu(): def ...

  9. UOJ 152 汉诺塔 分治

    题目链接 题意: 有三根编号为\((1, \, 2, \, 3)\)的柱子,然后第一根柱子上有编号为\(1 \sim n(n \leq 10000)\)的盘子,从上到下第\(i\)个盘子的编号是\(A ...

  10. Activity树图