hdu1762 树的上的查询
2015-10-07 20:44:42
题意问的是给了一颗树,然后又1000000次查询u,v,问不在树路径上的点的编号最小值,以1为根 建这颗树,然后在同一棵子树中的点子让就输出1 否则我们记录每个点从离1最近的那个点也就是1的孩子,到该点所经过的最小值,以及在他父亲到1的孩子,这段间和他不在同一条叉到上的最小值,还有就是他子树的最小值,然后遍历一遍,每次查询的时候搞一下就好了
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string.h>
using namespace std;
const int maxn=;
template <class T>
inline bool rd(T &ret) {
char c; int sgn;
if (c = getchar(), c == EOF) return ;
while (c != '-' && (c<'' || c>'')) c = getchar();
sgn = (c == '-') ? - : ;
ret = (c == '-') ? : (c - '');
while (c = getchar(), c >= ''&&c <= '') ret = ret * + (c - '');
ret *= sgn;
return ;
}
template <class T>
inline void pt(T x) {
if (x <) {
putchar('-');
x = -x;
}
if (x>) pt(x / );
putchar(x % + '');
}
int fa[maxn],H[maxn],to[maxn*],nx[maxn*],numofE;
int upMIN[maxn],downMIN[maxn][],other[maxn];
int belong[maxn]; void init(int N){
numofE=;
memset(H,,sizeof(H));
}
void add(int u,int v)
{
numofE++;
to[numofE]=v;
nx[numofE]=H[u];
H[u]=numofE;
}
int Q[maxn];
void cmp(int O,int a){
if(a<downMIN[O][]){
downMIN[O][]=downMIN[O][];
downMIN[O][]=a;
}else if(a<downMIN[O][]){
downMIN[O][]=a;
}
}
void searchroot(int cur)
{
int rear=;
Q[rear++]=cur;
fa[cur]=;
belong[cur]=cur;
upMIN[cur]=cur;
for(int i=; i<rear; i++)
{
int x=Q[i];
other[x]=downMIN[x][]=downMIN[x][]=maxn;
for(int j=H[x]; j; j=nx[j])
{
int too=to[j];
if(too==fa[x])continue;
Q[rear++]=too;
fa[too]=x;
upMIN[too]=min(too,upMIN[x]);
belong[too]=cur;
}
}
for(int i=rear-; i>=; i--)
{
int x=Q[i];
for(int j=H[x]; j; j=nx[j])
{
int too=to[j];
if(too==fa[x])continue;
int a=min(too,downMIN[too][]);
cmp(x,a);
}
} for(int i=; i<rear; i++){
int x=Q[i];
int a=min(x,downMIN[x][]);
int f=fa[x];
if(a!=downMIN[f][]){
other[x]=min(other[f],downMIN[f][]);
}else{
other[x]=min(other[f],downMIN[f][]);
}
}
}
int A[],B[];
void jud(int a)
{
A[]=a;
for(int i=; i>=; i--)
if(A[i]>A[i+])swap(A[i],A[i+]);
else break;
}
void solve1(int a,int b)
{
for(int i=; i<; i++)
if(A[i]!=a&&A[i]!=b){
B[]=A[i];return ;
}
}
int jud2()
{
int v=B[];
for(int i=;i<;i++)
v=min(B[i],v);
return v;
} int main()
{
int N,q;
downMIN[][]=downMIN[][]=other[]=upMIN[]=maxn;
while(scanf("%d%d",&N,&q)==)
{
init(N);
for(int i=; i<N; i++)
{
int a,b;
rd(a);rd(b); add(a,b);add(b,a);
}
A[]=A[]=A[]=maxn;
for(int i=H[]; i; i=nx[i])
{
searchroot(to[i]);
jud(min(to[i],downMIN[to[i]][]));
}
int d=;
for(int i=; i<q; i++)
{
int a,b;
rd(a);rd(b);
a^=d;b^=d;
if(a==&&b==){
d=;
puts("");continue;
}
if(belong[a]==belong[b]){
d=;
puts("");continue;
}
if(a==||b==){
a=max(a,b);b=max(a,b);
}
solve1(upMIN[a],upMIN[b]);
B[]=other[a];B[]=other[b];
B[]=downMIN[a][];B[]=downMIN[b][];
d=jud2();
printf("%d\n",d);
}
} return ;
}
hdu1762 树的上的查询的更多相关文章
- 浅谈oracle树状结构层级查询之start with ....connect by prior、level及order by
浅谈oracle树状结构层级查询 oracle树状结构查询即层次递归查询,是sql语句经常用到的,在实际开发中组织结构实现及其层次化实现功能也是经常遇到的,虽然我是一个java程序开发者,我一直觉得只 ...
- 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)
Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...
- 浅谈oracle树状结构层级查询测试数据
浅谈oracle树状结构层级查询 oracle树状结构查询即层次递归查询,是sql语句经常用到的,在实际开发中组织结构实现及其层次化实现功能也是经常遇到的,虽然我是一个java程序开发者,我一直觉得只 ...
- 在Bootstrap开发框架中使用bootstrapTable表格插件和jstree树形列表插件时候,对树列表条件和查询条件的处理
在我Boostrap框架中,很多地方需要使用bootstrapTable表格插件和jstree树形列表插件来共同构建一个比较常见的查询界面,bootstrapTable表格插件主要用来实现数据的分页和 ...
- 南昌网络赛 Distance on the tree 主席树+树剖 (给一颗树,m次查询ui->vi这条链中边权小于等于ki的边数。)
https://nanti.jisuanke.com/t/38229 题目: 给一颗树,m次查询ui->vi这条链中边权小于等于ki的边数. #include <bits/stdc++.h ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- 170406、用uid分库,uname(用户名)上的查询怎么办
[缘起] 用户中心是几乎每一个公司必备的基础服务,用户注册.登录.信息查询与修改都离不开用户中心. 当数据量越来越大时,需要多用户中心进行水平切分.最常见的水平切分方式,按照uid取模分库: 通过ui ...
- 【POJ 2777】 Count Color(线段树区间更新与查询)
[POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4094 ...
- Oracle11gR2 sqlplus中可以执行上键查询backspace删除
1.1 sqlplus中可以执行上键查询backspace删除 1.1.1 上键查询 方法1: 安装源-导入key-安装rpm包-进入配置文件修改参数 rpm -ivh http://download ...
随机推荐
- LeetCode 669 Trim a Binary Search Tree 解题报告
题目要求 Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so t ...
- 20165336 2017-2018-2 《Java程序设计》第5周学习总结
20165336 2017-2018-2 <Java程序设计>第5周学习总结 教材学习内容总结 内部类的类体中不可以声明类变量和类方法. 内部类仅供他的外嵌类使用,其他类不可以用某个类的内 ...
- kubernetes的apiserver
1. API Server简介 k8s API Server提供了k8s各类资源对象(pod,RC,Service等)的增删改查及watch等HTTP Rest接口,是整个系统的数据总线和数据中心. ...
- fiddler抓包常用功能详解
一.基础部分: 1.设置代理ip及端口,tools --> telerik fiddler options --> connections -->勾选 “ Allow romote ...
- 转: js实现全角半角检测的方法
//全角半角校验 function issbccase(strTmp) { for (var i=0; i<strTmp.length; i++) { if (strTmp.charCodeAt ...
- linux批量替换文本字符串
(一)通过vi编辑器来替换.vi/vim 中可以使用 :s 命令来替换字符串.:s/well/good/ 替换当前行第一个 well 为 good:s/well/good/g 替换当前行所有 well ...
- scp & cat远程文件操作
对拷文件夹 (包括文件夹本身) scp -r /home/wwwroot/www/charts/util root@192.168.1.65:/home/wwwroot/limesurvey_back ...
- 第 6 章 es5 对象创建和继承
目录 第 6 章 Object 一.创建对象 1. 字面量 2. 工厂模式 3. 构造函数 4. 原型 5. 构造+原型 6. 其他 二.Object静态属性 Object.definePropert ...
- php 关于时间函数
1. 设置时区 date_default_timezone_set() 和 putenv() 让时间安全地设置就,输入如下代码: date_default_timezone_set('UTC'); / ...
- Spring JPA实现逻辑源码分析总结
1.SharedEntityManagerCreator: entitymanager的创建入口 该类被EntityManagerBeanDefinitionRegistrarPostProcesso ...