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. Appium入门(3)__ Appium Server安装

    安装Appium 1.下载并安装:https://bitbucket.org/appium/appium.app/downloads/ 2. 系统变量PATH 增加 C:\Program Files ...

  2. 如何在Win10上永久禁用Windows Defender Antivirus

    1.使用Windows键+ R键盘快捷键打开运行命令. 2.键入regedit,然后单击确定以打开注册表. 3.浏览以下路径: HKEY_LOCAL_MACHINE/SOFTWARE/Policies ...

  3. filter的基本介绍和使用

    简介 过滤器是处在客户端和服务器资源之间的一到过滤网,我们可以根据具体的需求来对请求头和数据就行预处理,也可以对响应头和和数据进行后处理.例如Jsp, Servlet, 静态图片文件或静态 html ...

  4. tomcat在Eclipse中和idea中的使用

    在eclipse中的使用 下载 http://tomcat.apache.org/ 部署项目到tomcat 常见问题 访问时如何出掉项目名 中文乱码问题 1.浏览器编码问题,修改浏览器的编码 2.js ...

  5. 【数据可视化-pyecharts】pyecharts快速入门

    pyecharts快速开始 首先开始来绘制你的第一个图表 from pyecharts import Bar bar = Bar("我的第一个图表", "这里是副标题&q ...

  6. basename 命令

    basename命令的作用是获取脚本的名字 basename 作用:从给定的包含绝对路径的文件名中去除路径,然后返回剩下的文件名 [root@salt-client- sh1]# basename / ...

  7. python安装talib库

    简便安装方法 最简单的安装方式: $ pip install TA-Lib 用setup.py的方式自己安装一个源文件 $ python setup.py install 有可能遇到的问题 func. ...

  8. eclipse 优化

    1.取消验证 windows–>perferences–>validation 把 除了manual 下面的全部点掉,build下只留 classpath dependency Valid ...

  9. SSLServerSocket代码实现

    理解一个设计思想,结合代码是最好的途径.安全套接字服务端的实现代码如下: X509TrustManager MyX509TrustManager = new X509TrustManager() { ...

  10. SecureCRT乱码问题的解决

    == 安装后默认的设置是utf8,不是汉字乱码,是一块一块的看不清,OMG... 设置这两个页面就好了