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

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. 在win32中使用SetWindowSubclass阻止Enter键

    使用阻止Enter键的编辑控件的简单子类来完成此操作: LRESULT CALLBACK EditSubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, L ...

  2. 基于gtid复制主要操作记录

    基于gtid复制主要操作记录 一.安装系统依赖包 在主从上都要安装该依赖包. yum -y install perl-DBI yum -y install perl-DBD-MySQL yum -y ...

  3. node.js模块中exports和module.exports的区别

    Node应用由模块组成,采用CommonJS模块规范. 根据这个规范,每个文件就是一个模块,有自己的作用域.在一个文件里面定义的变量.函数.类,都是私有的,对其他文件不可见. CommonJS规范规定 ...

  4. 【shell】sed指定追加模式空间的次数

    最近遇到一个文本复制的场景,需要把文本的每一行都重复输出三次. 这个用awk或者sed实现都还是很简单的. sed代码: [root]$ seq | sed '{h;G;G}' 现在的问题是,如果每行 ...

  5. springboot 出现异常 java.net.BindException: Address already in use: bind

    java.net.BindException: Address already in use: bind

  6. explicit作用

    在C++中,explicit关键字用来修饰类的构造函数,被修饰的构造函数的类,不能发生相应的隐式类型转换,只能以显示的方式进行类型转换. explicit使用注意事项: explicit 关键字只能用 ...

  7. CSS实现二维码扫描的效果

    扫描二维码的效果,我原以为不好写呢,后来想了想其实挺简单的,几行代码,走起 <div class="code-bg"> <div class="line ...

  8. JAVA语言课堂测试源代码及使用截图

    1源代码 第一部分 package 开学测试.java;class ScoreInformation {String stunumber;String name;double mathematicss ...

  9. (46)LINUX应用编程和网络编程之一Linux应用编程框架

    3.1.1.应用编程框架介绍 3.1.1.1.什么是应用编程 (1)整个嵌入式linux核心课程包括5个点,按照学习顺序依次是:裸机.C高级.uboot和系统移植.linux应用编程和网络编程.驱动. ...

  10. [BZOJ4456][ZJOI2016]旅行者:分治+最短路

    分析 类似于点分治的思想,只统计经过分割线的最短路,然后把地图一分为二. 代码 #include <bits/stdc++.h> #define rin(i,a,b) for(regist ...