一棵结点为黑色或白色的树,初始都是白色的。有两种操作

1 将一个结点换颜色
2 询问从根到结点u路径上面的第一个黑色点,没有则输出-1
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.
Sample 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
Sample Output
-1
8
-1
2
-1

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;const int N=200001;
int n,m,sum,cnt,now,pre[N],f[N],nxt[N],h[N],top[N],id[N],size[N],dep[N],ans,maxx,di[N];
struct oo{int a,b,dp,now;bool v;}s[N*2-20000];
void dfs(int x)
{
size[x]=1;
for(int i=h[x];i;i=nxt[i])
{
if(pre[i]==f[x])continue;
dep[pre[i]]=dep[x]+1;
f[pre[i]]=x;
dfs(pre[i]);
size[x]+=size[pre[i]];
}
}
void dfs2(int x,int f)
{
int k=0;
id[x]=++cnt;
di[cnt]=x; //dfs序中第cnt个点是x
top[x]=f;
for(int i=h[x];i;i=nxt[i])
if(size[pre[i]]>size[k]&&dep[pre[i]]>dep[x])k=pre[i];
if(!k)return ;
dfs2(k,f);
for(int i=h[x];i;i=nxt[i])
if(dep[pre[i]]>dep[x]&&pre[i]!=k)
dfs2(pre[i],pre[i]);
}
void ins(int x,int y)
{
pre[++now]=y;
nxt[now]=h[x];
h[x]=now;
}
void build(int x,int l,int r)
{
s[x].a=l,s[x].b=r;
s[x].dp=1e9;
if(l==r)
{
return ;
}
build(x<<1,l,l+r>>1);
build(x<<1|1,(l+r>>1)+1,r);
}
void get(int x,int l,int r)
//求出线段树上[l,r]哪个点是黑色的,且深度最小
{
if(s[x].a>=l&&r>=s[x].b)
{
if(s[x].dp<maxx)
{
maxx=s[x].dp; //找出深度最小值
sum=s[x].now;//对应的点
}
return ;
}
else
{
int mid=s[x].a+s[x].b>>1;
if(l<=mid)get(x<<1,l,r);
if(r>mid)get(x<<1|1,l,r);
}
}
void qmax(int x,int y)
{
maxx=1e9,sum=-1;
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]])
swap(x,y);
get(1,id[top[x]],id[x]);
x=f[top[x]];
}
if(id[x]>id[y])
swap(x,y);
get(1,id[x],id[y]);
if(sum==0)
sum=-1;
}
void change(int x,int l)
//x指目前在哪个树上
//l是指线段中哪个位置
{
if(s[x].a==s[x].b)
{
s[x].v^=1;
if(s[x].v) //如果染成黑色
{
s[x].dp=dep[di[l]]; //求出深度
s[x].now=di[l];
//di[l]指dfs序列中第l个点是原树上哪个点
}
else //还原成白色
s[x].dp=1e9,s[x].now=0;
return ;
}
int mid=s[x].a+s[x].b>>1;
if(l<=mid)
change(x<<1,l);
else
change(x<<1|1,l);
if(s[x<<1].dp<s[x<<1|1].dp)//求出最小的深度
{
s[x].dp=s[x<<1].dp;
s[x].now=s[x<<1].now;
}
else
{
s[x].dp=s[x<<1|1].dp;
s[x].now=s[x<<1|1].now;
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1,x,y;i<n;i++)
{
scanf("%d%d",&x,&y);
ins(x,y);
ins(y,x);
}
dfs(1);
dfs2(1,1);
build(1,1,n);
int c;
for(int i=1;i<=m;i++)
{
int a,b;
scanf("%d",&c);
if(c) //查询操作
{
scanf("%d",&b);
qmax(1,b);
printf("%d\n",sum);
}
if(!c) //将某个结点换颜色
scanf("%d",&b),change(1,id[b]);
}
}

  

 

  

Spoj 2798 Qtree3的更多相关文章

  1. SPOJ 2798 QTREE3 - Query on a tree again!

    原oj题面 Time limit 2000 ms Memory limit 1572864 kB Code length Limit 50000 B OS Linux Language limit A ...

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

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

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

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

  4. SPOJ QTREE3 lct

    题目链接 题意: 给定n个点 q个询问 以下n-1行给出树边,点有黑或白色.初始化为白色 以下q行: 询问有2种: 1. 0 x 把x点黑变白,白变黑 2.1 x 询问Path(1,x)路径上第一个黑 ...

  5. SPOJ QTREE3 - Query on a tree again!

    You are given a tree (an acyclic undirected connected graph) with N nodes. The tree nodes are number ...

  6. Query on a tree again! SPOJ - QTREE3

    https://vjudge.net/problem/SPOJ-QTREE3 https://www.luogu.org/problemnew/show/P4116 一个log(LCT)比两个log( ...

  7. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  8. SPOJ DQUERY D-query(主席树)

    题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...

  9. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

随机推荐

  1. At grand 024

    A /* Huyyt */ #include <bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define mkp(a,b) ...

  2. 彻底解决 TypeScript 报错:“无法重新声明块范围变量”的问题

    背景 当使用 TypeScript + TSlint + Babel + Jest 搭建开发环境时,在开发过程中偶尔会被 IDE 提示「无法重新声明块范围变量」,从而导致编译出错,报错图示如下: 相关 ...

  3. controller 有两种写法,讨论一下两种写法的区别:

    controller 有两种写法,讨论一下两种写法的区别: 写法 1: app.controller('myCtrl', function($scope, $location) { $scope.my ...

  4. html头部和底部固定时,中间的内容随屏幕分别率铺满页面

    html页面头部和底部有东西时,怎么让内容填充到中间的页面,且去适应不同的电脑分辨率,看代码 <!DOCTYPE html> <html> <head> <m ...

  5. ks代码助解

    代码实现: data_test_2 = {'gd':[1,1,1,1,1,1,0,0,0,0,0,0,0],'score':[1,2,0,2,2,7,4,5,4,0,4,18,np.nan]} dat ...

  6. thinkphp一对多关系

    兹有用户表user和评论表comment 一对一 public function returnmany() { return $this->hasOne('commnet','uid','use ...

  7. POJ-2104-Kth Number(主席树)

    链接: https://vjudge.net/problem/POJ-2104#author=malic 题意: 给定一个数组 a[1...n],数组元素各不相同,你的程序要对每次查询Q(i,j,k) ...

  8. VMware安装Ghost版Win10 失败的解决方法

    第一个失败点,是分区之后,重启,提示alt+ctrl+del要求重启,然后就是无限提示,解决方案:在重启读条的时候,按Esc,或者F2调整系统启动优先级读取位置,设置为CD的那个,就可以进入到安装系统 ...

  9. xgboost使用细节

    from http://blog.csdn.net/zc02051126/article/details/46771793 在Python中使用XGBoost 下面将介绍XGBoost的Python模 ...

  10. 几道好玩的JS题目,看看你能答中几道。

    1,for (var i = 0; i < 5; i++) { console.log(i); } 这几行代码会输出什么? 输出 0 1 2 3 4 2,for (var i = 0; i &l ...