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 ...
随机推荐
- day2_Jmeter压测
1.线程组各项设置的意思 2.压测结果查看各指标意思 备注:tps:每秒钟系统能够处理的交易或事务的数量.它是衡量系统处理能力的重要指标.tps越高说明服务器处理能力越好. 3.在一台电脑上做一个简单 ...
- LeetCode 985 Sum of Even Numbers After Queries 解题报告
题目要求 We have an array A of integers, and an array queries of queries. For the i-th query val = queri ...
- jetty在eclipse和Idea中的使用
eclipse中的配置 下载 http://www.eclipse.org/jetty/download.html 下载保存到特定的位置,解压. 整合到eclipse中 这里通过在eclipse中安装 ...
- 【PyQt5-Qt Designer】PyQt5+pyecharts 实现GUI界面的数据可视化展示
先用纯Python代码写一个简单的小案例: from PyQt5.QtCore import QUrl from PyQt5.QtWidgets import QApplication,QWidget ...
- u-boot 编译,启动流程分析,移植
分析u-boot-1.1.6 的启动流程 移植u-boot 2012.04版本到JZ2440开发板 源码百度云链接:https://pan.baidu.com/s/10VnxfDWBqJVGY3SCY ...
- python面向对象的三大特性
一.继承 面向对象中的继承就是继承的类直接拥有被继承类的属性而不需要在自己的类体中重新再写一遍,其中被继承的类叫做父类.基类,继承的类叫做派生类.子类.在python3中如果不指定继承哪个类,默认就会 ...
- 简述 cookies 和 session 的区别
1.cookie 和 session 的区别是:cookie数据保存在客户端,session数据保存在服务器端: 2.两个都可以用来存私密的东西,同样也都有有效期的说法,区别在于session是放在服 ...
- Docker For Mac没有docker0网桥
在使用Docker时,要注意平台之间实现的差异性,如Docker For Mac的实现和标准Docker规范有区别,Docker For Mac的Docker Daemon是运行于虚拟机(xhyve) ...
- 20181223 python 使用Beautiful Soup
(这篇,没什么营养价值) 怎么说呢! 爬虫吧!把html页面进行解析得到有效数据,而beautiful soup 能快速格式化页面再进行方法对数进行提取,存入想要存入的DB中. from bs4 im ...
- what's the python之可迭代对象、迭代器与生成器(附面试题)
可迭代对象 字符串.列表.元祖.集合.字典都是可迭代的,数字是不可迭代的.(可以用for循环遍历取出内部元素的就是可迭代的) 如何查看一个变量是否为可迭代: from collections impo ...