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 ...
随机推荐
- 《Redis 主从复制》
万念俱灰,说的就是我现在的心情...... 周六下午写了一下午的读书笔记,由于我的 MAC 有点问题,重启了一下...... 灰飞烟灭...... 读 第八章<集群> 总结 1:如何开启主 ...
- bug:*** Collection <__NSArrayM: 0x1c444d440> was mutated while being enumerated.
崩溃提示:Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <CAL ...
- Python 字符串常用方法总结
明确:对字符串的操作方法都不会改变原来字符串的值 1,去掉空格和特殊符号 name.strip() 去掉空格和换行符 name.strip('xx') 去掉某个字符串 name.lstrip() ...
- 第一章:深入.NET框架
1..net框架结构 主要包含公共语言运行时(CLR)和框架类库(.NET Framework 类库 ,FCL) 2.CLR 1.对于一个将要面向.NET平台进行开发的人来说,了解一下.NET平台的 ...
- LeetCode-188.Best Time to Buy and Sell Stock IV
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- basename 命令
basename命令的作用是获取脚本的名字 basename 作用:从给定的包含绝对路径的文件名中去除路径,然后返回剩下的文件名 [root@salt-client- sh1]# basename / ...
- MongoDB limit 选取 skip跳过 sort排序 方法
MongoDB limit 选取 skip跳过 sort排序 在mysql里有order by MongoDB用sort代替order by > db.user.find() { " ...
- linux 修改centos7的网卡ens33修改为eth0
Linux 操作系统的网卡设备的传统命名方式是 eth0.eth1.eth2等,而 CentOS7 提供了不同的命名规则,默认是基于固件.拓扑.位置信息来分配.这样做的优点是命名全自动的.可预知的,缺 ...
- Eclipse ADT中的logcat不显示解决方法
今天维护android的程序,也不知道几百年前写的,elipse,一个日志文件都不显示,做的想哭,我使用的方法二,这里给自己留一个备份,怕再忘记了 1.在Eclipse界面中找到DDMS,然后找到 ...
- 004-linux命令-搜索命令find、locate、whereis、which、type
一.概述 使用linux系统难免会忘记文件所在的位置,可以使用以下命令对系统中的文件进行搜索. 1.1.find 语法:find <指定目录> <指定条件> <指定动作& ...