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 树的上的查询的更多相关文章

  1. 浅谈oracle树状结构层级查询之start with ....connect by prior、level及order by

    浅谈oracle树状结构层级查询 oracle树状结构查询即层次递归查询,是sql语句经常用到的,在实际开发中组织结构实现及其层次化实现功能也是经常遇到的,虽然我是一个java程序开发者,我一直觉得只 ...

  2. 计蒜客 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 ...

  3. 浅谈oracle树状结构层级查询测试数据

    浅谈oracle树状结构层级查询 oracle树状结构查询即层次递归查询,是sql语句经常用到的,在实际开发中组织结构实现及其层次化实现功能也是经常遇到的,虽然我是一个java程序开发者,我一直觉得只 ...

  4. 在Bootstrap开发框架中使用bootstrapTable表格插件和jstree树形列表插件时候,对树列表条件和查询条件的处理

    在我Boostrap框架中,很多地方需要使用bootstrapTable表格插件和jstree树形列表插件来共同构建一个比较常见的查询界面,bootstrapTable表格插件主要用来实现数据的分页和 ...

  5. 南昌网络赛 Distance on the tree 主席树+树剖 (给一颗树,m次查询ui->vi这条链中边权小于等于ki的边数。)

    https://nanti.jisuanke.com/t/38229 题目: 给一颗树,m次查询ui->vi这条链中边权小于等于ki的边数. #include <bits/stdc++.h ...

  6. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  7. 170406、用uid分库,uname(用户名)上的查询怎么办

    [缘起] 用户中心是几乎每一个公司必备的基础服务,用户注册.登录.信息查询与修改都离不开用户中心. 当数据量越来越大时,需要多用户中心进行水平切分.最常见的水平切分方式,按照uid取模分库: 通过ui ...

  8. 【POJ 2777】 Count Color(线段树区间更新与查询)

    [POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4094 ...

  9. Oracle11gR2 sqlplus中可以执行上键查询backspace删除

    1.1 sqlplus中可以执行上键查询backspace删除 1.1.1 上键查询 方法1: 安装源-导入key-安装rpm包-进入配置文件修改参数 rpm -ivh http://download ...

随机推荐

  1. UI自动化遇到的问题

    1.找不到元素:查看是否有[frame]或者[iframe]元素 2.click无响应:1)使用sendkeys(Keys.Enter) 还是无响应:使用js定位 3.弹框处理: 确认:switcht ...

  2. vue的生命周期(lifecycle)

    这边转载一篇文章,个人认为写的不错,代码举了个例子很生动. https://segmentfault.com/a/1190000010336178

  3. 基于w2v词向量聚类出现的问题(有待解决)

    1.训练词向量代码如下:#训练词语为向量表示def w2v_train(self): ques = self.cu.execute('select question from activity')#将 ...

  4. JavaScript学习笔记--语法

    代码风格: 每句结尾不用加分号: 单行注释用//,多行注释用/*...需要注释掉的代码....*/ 严格区分大小写 优雅的代码需要注意锁进 基础知识: Number:JavaScript不区分整数和浮 ...

  5. net use共享文件访问

    NET USE "\\xxx.xxx.xxx.xxx\vms\Application Files" "password123" /USER:"ap\1 ...

  6. memcached命令知识

    memcached安装配置 yum install memcached      memcached最大的缓存单位为1M,大于1M的单项数据将不会缓存      memcached是基于文本协议的,所 ...

  7. w97常用功能代码

    1,onclick中添加日期控件 2,onpicked事件即是点击控件后触发的事件 3,dp.cal.getNewDateStr()即是点击到的日期字符串 <script> functio ...

  8. rest_framework的认证系统

    1.认证模块 必须用户登录之后才能访问所有图书,才能修改图片,才能查询单个图书 2.怎么使用 其实本质上就是携带token字符串,然后后台拿到数据再取数据库进行校验,看是否有这个用户 先手写一个认证模 ...

  9. poj3278 Catch That Cow(简单的一维bfs)

    http://poj.org/problem?id=3278                                                                       ...

  10. Python socketserver模块解析

    参考:https://blog.csdn.net/qq_33733970/article/details/79153938 1.功能简介 socketserver模块是对socket模块的再封装,用于 ...