Spoj 2798 Qtree3
一棵结点为黑色或白色的树,初始都是白色的。有两种操作
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的更多相关文章
- 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 ...
- QTREE3 spoj 2798. Query on a tree again! 树链剖分+线段树
Query on a tree again! 给出一棵树,树节点的颜色初始时为白色,有两种操作: 0.把节点x的颜色置反(黑变白,白变黑). 1.询问节点1到节点x的路径上第一个黑色节点的编号. 分析 ...
- SPOJ QTREE3 Query on a tree again! ——Link-Cut Tree
[题目分析] QTREE2,一看是倍增算法,太懒了,不写了.( ̄_, ̄ ) QTREE3,树链剖分可以做,发现链上的问题LCT也很好做. 要是子树问题貌似可以DFS序. 然后就成LCT模板题了. 考前 ...
- SPOJ QTREE3 lct
题目链接 题意: 给定n个点 q个询问 以下n-1行给出树边,点有黑或白色.初始化为白色 以下q行: 询问有2种: 1. 0 x 把x点黑变白,白变黑 2.1 x 询问Path(1,x)路径上第一个黑 ...
- 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 ...
- Query on a tree again! SPOJ - QTREE3
https://vjudge.net/problem/SPOJ-QTREE3 https://www.luogu.org/problemnew/show/P4116 一个log(LCT)比两个log( ...
- 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 ...
- SPOJ DQUERY D-query(主席树)
题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...
- 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 ...
随机推荐
- 跨域 (2) cors
html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- docker安装踩坑记录
.docker安装包安装很顺利, yum安装安装源在官网或镜像网站下载, fedora/centos分别下载不同的, 在/etc/yum.repos.d/doccker-ce.repo, 把网址改为镜 ...
- Puppet利用Nginx多端口实现负载均衡
随着公司应用需求的增加,需要不断的扩展,服务器数量也随之增加,当服务器数量不断增加,我们会发现一台puppetmaster压力大,解析缓慢,而且时不时出现"time out"之类的 ...
- keep-alive前进没有刷新
https://segmentfault.com/a/1190000012083511
- JAVA笔记17-容器之一 图、Collection接口、Iterator接口(重要)
掌握1136: (1)1个图 (2)1个类:Collections (3)3个知识点:For(了解),Generic泛型,Auto-boxing/unboxing(自动打包/解包) (4)6个接口:C ...
- 浅谈Vue中Slot以及slot-scope
vue中关于插槽的文档说明很短,语言又写的很凝练,再加上其和methods,data,computed等常用选项使用频率.使用先后上的差别,这就有可能造成初次接触插槽的开发者容易产生“算了吧,回头再学 ...
- c# B/S下 如何优化文件上传速度和实现断点续传问题
IE的自带下载功能中没有断点续传功能,要实现断点续传功能,需要用到HTTP协议中鲜为人知的几个响应头和请求头. 一. 两个必要响应头Accept-Ranges.ETag 客户端每次提交下载请求时,服务 ...
- 批量下载文件asp.net
一.实现步骤 在用户操作界面,由用户选择需要下载的文件,系统根据所选文件,在服务器上创建用于存储所选文件的临时文件夹,将所选文件拷贝至临时文件夹.然后调用 RAR程序,对临时文件夹进行压缩,然后输出到 ...
- java+web+批量下载文件
JavaWeb 文件下载功能 文件下载的实质就是文件拷贝,将文件从服务器端拷贝到浏览器端,所以文件下载需要IO技术将服务器端的文件读取到,然后写到response缓冲区中,然后再下载到个人客户端. 1 ...
- Jenkins部署从节点
由于jenkins上承载项目太多,需要专门的节点来执行需要构建的操作. 参考:https://www.cnblogs.com/lxs1314/p/7551309.html job仅使用绑定的slave ...