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 ...
随机推荐
- C#传递数组参数
在C#中,可以将数组作为参数传递给方法,同时方法可以更改数组元素的值. 一.将一维数组作为参数传递给方法 using System;using System.Collections.Generic;u ...
- 远程连接 mySql数据库
远程连接 mySql数据库 一.安装并配置MySQL1.安装MySQL:运行mysql-essential-6.0.11-alpha-win32,按“MySQL+6.0+Windows下安装图解”完成 ...
- Java基础面试操作题:Java代理工厂设计模式 ProxyFactory 有一个Baby类,有Cry行为,Baby可以配一个保姆 但是作为保姆必须遵守保姆协议:能够处理Baby类Cry的行为,如喂奶、哄睡觉。
package com.swift; public class Baby_Baomu_ProxyFactory_Test { public static void main(String[] args ...
- (转发)IOS高级开发~Runtime(二)
一些公用类: @interface ClassCustomClass :NSObject{ NSString *varTest1; NSString *varTest2; NSString *varT ...
- websocket 踩坑记录
ssh execute command error: can't connect str to butes ssh 发送下一次指令回传的是上一次指令的结果 ssh 始终停留在 root 目录内 ssh ...
- NOIP模拟赛 麻将
[题目描述] 众所周知,麻将是我们国家的国粹.这段时间,小D也迷上了麻将这个老少皆宜的游戏. 小D觉得这些不同规则的麻将太麻烦了,所以他集合了很多种麻将规则创造出了一套D麻将.下面是D麻将的几个特点: ...
- 【二分 最小割】cf808F. Card Game
Digital collectible card games have become very popular recently. So Vova decided to try one of thes ...
- k8s资源指标API及metrics-server资源监控
简述: 在k8s早期版本中,对资源的监控使用的是heapster的资源监控工具. 但是从 Kubernetes 1.8 开始,Kubernetes 通过 Metrics API 获取资源使用指标,例如 ...
- windows server 服务器 环境配置
自动备份 xcopy d:\web\zhiku\*.* d:\bak\web\zhiku\%date:~,4%%date:~5,2%%date:~8,2%\ /S /I
- Python基础-面向对象初识--类
什么是类 具有相似功能和属性的一类实物 什么是对象 类的具体体现,具体到一个 面向对象的优势 1.类是一组相似功能的集合,使组织结构更加清晰和规范化 2.研究面向对象要有上帝的思维,用面向对象设计程序 ...