Spoj Query on a tree III
题目描述
给出N个点的一棵树(N-1条边),节点有白有黑,初始全为白
有两种操作:
0 i : 改变某点的颜色(原来是黑的变白,原来是白的变黑)
1 v : 询问1到v的路径上的第一个黑点,若无,输出-1
输入输出格式
输入格式:
第一行 N,Q,表示N个点和Q个操作
第二行到第N行N-1条无向边
再之后Q行,每行一个操作"0 i" 或者"1 v" (1 ≤ i, v ≤ N).
输出格式:
对每个1 v操作输出结果
输入输出样例
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
-1
8
-1
2
-1
说明
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.
(假装我是在spoj上做的一样hhh)
贼裸的树剖+线段树,,,练手题(今天建模能力为0,全在码模板hhh)
#include<bits/stdc++.h>
#define ll long long
#define maxn 100005
#define pb push_back
using namespace std;
vector<int> g[maxn];
int dfn[maxn],dy[maxn],pos;
int siz[maxn],f[maxn],opt;
int cl[maxn],n,m,son[maxn],dc=;
int sum[maxn<<|],mxp[maxn<<|];
int le,ri; void dfs1(int x,int fa){
f[x]=fa,siz[x]=;
int to;
for(int i=g[x].size()-;i>=;i--){
to=g[x][i];
if(to==fa) continue;
dfs1(to,x),siz[x]+=siz[to];
if(!son[x]||siz[to]>siz[son[x]]) son[x]=to;
}
} void dfs2(int x,int tp){
dfn[x]=++dc,dy[dc]=x,cl[x]=tp;
if(son[x]) dfs2(son[x],tp);
int to;
for(int i=g[x].size()-;i>=;i--){
to=g[x][i];
if(to==f[x]||to==son[x]) continue;
dfs2(to,to);
}
} void update(int o,int l,int r){
if(l==r){
sum[o]^=;
if(sum[o]) mxp[o]=dy[l];
else mxp[o]=;
return;
} int mid=l+r>>,lc=o<<,rc=(o<<)|;
if(le<=mid) update(lc,l,mid);
else update(rc,mid+,r);
if(sum[lc]) mxp[o]=mxp[lc];
else mxp[o]=mxp[rc];
sum[o]=sum[lc]+sum[rc];
} int query(int o,int l,int r){
if(l>=le&&r<=ri) return mxp[o];
int mid=l+r>>,lc=o<<,rc=(o<<)|,an=;
if(le<=mid) an=query(lc,l,mid);
if(!an&&ri>mid) an=query(rc,mid+,r);
return an;
} inline void tolt(int x){
le=dfn[x],update(,,n);
} inline int answer(int x){
int an=-,po;
while(x){
le=dfn[cl[x]],ri=dfn[x];
po=query(,,n);
if(po) an=po;
x=f[cl[x]];
}
return an;
} int main(){
int uu,vv;
scanf("%d%d",&n,&m);
for(int i=;i<n;i++){
scanf("%d%d",&uu,&vv);
g[uu].pb(vv),g[vv].pb(uu);
} dfs1(,);
dfs2(,); while(m--){
scanf("%d%d",&opt,&pos);
if(opt) printf("%d\n",answer(pos));
else tolt(pos);
} return ;
}
Spoj Query on a tree III的更多相关文章
- SPOJ Query on a tree III (树剖(dfs序)+主席树 || Splay等平衡树)(询问点)
You are given a node-labeled rooted tree with n nodes. Define the query (x, k): Find the node whose ...
- 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 ...
- SP1487 PT07J - Query on a tree III (主席树)
SP1487 PT07J - Query on a tree III 题意翻译 你被给定一棵带点权的n个点的有根数,点从1到n编号. 定义查询 query(x,k): 寻找以x为根的k大点的编号(从小 ...
- 【BZOJ1803】Spoj1487 Query on a tree III 主席树+DFS序
[BZOJ1803]Spoj1487 Query on a tree III Description You are given a node-labeled rooted tree with n n ...
- 「SPOJ1487」Query on a tree III
「SPOJ1487」Query on a tree III 传送门 把树的 \(\text{dfs}\) 序抠出来,子树的节点的编号位于一段连续区间,然后直接上建主席树区间第 \(k\) 大即可. 参 ...
- SPOJ 1487 Query on a tree III(划分树)
题目链接:http://www.spoj.com/problems/PT07J/ 题意:给出一个有根树,1为根节点,每个节点有权值.若干询问,询问以u为根的子树中权值第K小的节点编号. 思路:DFS一 ...
- [ SPOJ PT07J ] Query on a tree III
\(\\\) Description 其实这题才是正版的 Qtree3...... 给定 \(n\) 个点,以 \(1\) 号节点为根的树,点有点权. \(m\) 次询问 以 \(x\) 为根的子树内 ...
- bzoj1803: Spoj1487 Query on a tree III
Description You are given a node-labeled rooted tree with n nodes. Define the query (x, k): Find the ...
- SPOJ Query on a tree 树链剖分 水题
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
随机推荐
- [bzoj 2115]线性基+图论
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2115 给定一个带权无向图,要找出从1到n路径权值异或和最大的那一条的路径异或和. 考虑1到 ...
- wyh的物品~(二分)
链接:https://www.nowcoder.com/acm/contest/93/I来源:牛客网 题目描述 wyh学长现在手里有n个物品,这n个物品的重量和价值都告诉你,然后现在让你从中选取k个, ...
- 用npm安装express时报proxy的错误的解决方法
首先要说明一点:当使用npm install <module-name>时安装组件时,安装的目录是cmd的目录+node_modules+组件名 例子如下:假如你现在安装express这个 ...
- bzoj 4004 [JLOI2015]装备购买 拟阵+线性基
[JLOI2015]装备购买 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 1820 Solved: 547[Submit][Status][Dis ...
- Spring Boot(一)
1.注解 @EnableAutoConfiguration 官方文档:The @EnableAutoConfiguration annotation is often placed on your ...
- 面向对象的tab选项卡实现
利用最基础的面向对象的思想,实现tab选项卡效果: 效果截图:
- 【Foreign】旅行路线 [倍增]
旅行路线 Time Limit: 20 Sec Memory Limit: 256 MB Description Input Output 仅一行一个整数表示答案. Sample Input 3 2 ...
- BZoj 1003 物流运输 DP+最短路
2013-09-11 09:56 W[I]代表前I天能取得的最小花费,假设在第J天更改一次路线,那么如果有 W[I]>W[J]+第j+1到第I天的最小花费+更改路线的花费(K) 那么更新W[I] ...
- go语言中的json
结构体类型转化为json格式 package main import ( "encoding/json" "fmt" ) //如果要转化成json格式,那么成员 ...
- Newtonsoft.Json 序列化和反序列化 以及时间格式
1.JSON序列化 string JsonStr= JsonConvert.SerializeObject(Entity); eg: A a=new A(); a.Name="Elain ...