SPOJ QTREE Query on a tree V ——动态点分治
【题目分析】
QTREE4的弱化版本
建立出分治树,每个节点的堆表示到改点的最近白点距离。
然后分治树上一直向上,取min即可。
正确性显然,不用担心出现在同一子树的情况(不会是最优解),请自行脑补。
然后弱渣我写了1.5h
【代码】
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define F(i,j,k) for (int i=j;i<=k;++i)
#define D(i,j,k) for (int i=j;i>=k;--i)
#define inf 0x3f3f3f3f
#define maxe 200005
#define maxn 100005
struct Heap{
priority_queue<int, vector<int>, greater<int> > heap,del;
void Ins(int x){heap.push(x);}
void Del(int x){del.push(x);}
int Size(){return heap.size()-del.size();}
int Top(){while (del.size()&&heap.top()==del.top()) heap.pop(),del.pop();return heap.top();}
}s[maxn]; int h[maxe],to[maxe],ne[maxe],en=0,n,m,_log[maxn<<2],a[maxn<<2][20],col[maxn],tag=0,x;
int siz[maxn],mx[maxn],root,now,b[maxn<<2],top=0,pos[maxn],size,ban[maxn],T_rt,dst[maxn],fa[maxn]; void add(int a,int b){to[en]=b;ne[en]=h[a];h[a]=en++;} void dfs(int o,int fa)
{
siz[o]=1; mx[o]=0;
if (!tag) b[++top]=o,pos[o]=top;
for (int i=h[o];i>=0;i=ne[i])
if (!ban[to[i]]&&to[i]!=fa)
{
dfs(to[i],o);
if (!tag) b[++top]=o;
siz[o]+=siz[to[i]];
mx[o]=max(mx[o],siz[to[i]]);
}
} void dfs_root(int o,int fa)
{
if (now>max(mx[o],size-siz[o])) root=o,now=max(mx[o],size-siz[o]);
for (int i=h[o];i>=0;i=ne[i])
if (!ban[to[i]]&&to[i]!=fa)
dfs_root(to[i],o);
} void dfs_dist(int o,int fa)
{
for (int i=h[o];i>=0;i=ne[i])
if (!ban[to[i]]&&to[i]!=fa)
dst[to[i]]=dst[o]+1,dfs_dist(to[i],o);
} void Divide(int o,int fat)
{
dfs(o,-1);now=inf;size=siz[o];dfs_root(o,-1);
int rt=root; ban[rt]=1;fa[rt]=fat;
for (int i=h[rt];i>=0;i=ne[i])
if (!ban[to[i]]) Divide(to[i],rt);
} int dist(int x,int y)
{
int ret=dst[x]+dst[y];
x=pos[x],y=pos[y];
if (x>y) swap(x,y);
int l=_log[y-x+1];
return ret-2*min(a[x][l],a[y-(1<<l)+1][l]);
} void Delete(int o)
{
s[o].Del(0);
int now=fa[o];
while (now)
{
s[now].Del(dist(o,now));
now=fa[now];
}
} void Insert(int o)
{
s[o].Ins(0);
int now=fa[o];
while (now)
{
s[now].Ins(dist(o,now));
now=fa[now];
}
} int query(int o)
{
int ret=inf;
if (s[o].Size()) ret=min(ret,s[o].Top());
int now=fa[o];
while (now)
{
if (s[now].Size()) ret=min(s[now].Top()+dist(o,now),ret);
now=fa[now];
}
if (ret==inf) printf("-1\n");
else printf("%d\n",ret);
} int main()
{
memset(h,-1,sizeof h);
scanf("%d",&n);
F(i,1,n-1)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b);add(b,a);
}
tag=1; dfs(1,-1); size=siz[1]; now=inf; dfs_root(1,-1); T_rt=root;
tag=0; dfs(root,-1); dfs_dist(root,-1); tag=1;
F(i,2,top) _log[i]=_log[i>>1]+1;
F(i,1,top) a[i][0]=dst[b[i]];
F(j,1,_log[top])
for (int i=1;i+(1<<j)-1<=top;++i)
a[i][j]=min(a[i][j-1],a[i+(1<<j-1)][j-1]);
Divide(T_rt,0);
scanf("%d",&m);
F(i,1,m)
{
int opt; scanf("%d",&opt);
switch(opt)
{
case 0:scanf("%d",&x);if (col[x]) Delete(x); else Insert(x); col[x]^=1; break;
case 1:scanf("%d",&x);query(x); break;
}
}
}
SPOJ QTREE Query on a tree V ——动态点分治的更多相关文章
- SPOJ QTREE Query on a tree V
You are given a tree (an acyclic undirected connected graph) with N nodes. The tree nodes are number ...
- SPOJ QTREE4 Query on a tree IV ——动态点分治
[题目分析] 同bzoj1095 然后WA掉了. 发现有负权边,只好把rmq的方式改掉. 然后T了. 需要进行底(ka)层(chang)优(shu)化. 然后还是T 下午又交就A了. [代码] #in ...
- SPOJ QTREE Query on a tree 树链剖分+线段树
题目链接:http://www.spoj.com/problems/QTREE/en/ QTREE - Query on a tree #tree You are given a tree (an a ...
- SPOJ 375. Query on a tree (动态树)
375. Query on a tree Problem code: QTREE You are given a tree (an acyclic undirected connected graph ...
- spoj QTREE - Query on a tree(树链剖分+线段树单点更新,区间查询)
传送门:Problem QTREE https://www.cnblogs.com/violet-acmer/p/9711441.html 题解: 树链剖分的模板题,看代码比看文字解析理解来的快~~~ ...
- SPOJ QTREE Query on a tree --树链剖分
题意:给一棵树,每次更新某条边或者查询u->v路径上的边权最大值. 解法:做过上一题,这题就没太大问题了,以终点的标号作为边的标号,因为dfs只能给点分配位置,而一棵树每条树边的终点只有一个. ...
- SPOJ QTREE Query on a tree VI
You are given a tree (an acyclic undirected connected graph) with n nodes. The tree nodes are number ...
- SPOJ QTREE - Query on a tree 【树链剖分模板】
题目链接 引用到的大佬博客 代码来自:http://blog.csdn.net/jinglinxiao/article/details/72940746 具体算法讲解来自:http://blog.si ...
- SPOJ QTREE Query on a tree
题意:给一颗n个点的树,有两种操作CHANGE i ti : 把第i条边的权变为tiQUERY a b : 问点a 到 点b 之间的边的最大权 思路:树剖处理边权.由于是边,所以只需要把边权处理到子节 ...
随机推荐
- DataPicker以及TimePicker显示时间和日期(屏幕上显示)
public class MainActivity extends Activity { private DatePicker date_picker;private TimePicker time_ ...
- AzureARM 使用 powershell 扩容系统磁盘大小
azure中的虚拟机,windows磁盘大小为127G,linux磁盘大小为30G,在很多时候部署应用程序时直接部署到系统磁盘内导致磁盘后期容量不够需要扩容,在执行分区扩容前我们需要先通过Potal或 ...
- Android串口通信
前段时间因为工作需要研究了一下android的串口通信,网上有很多讲串口通信的文章,我在做的时候也参考了很多文章,现在就将我学习过程中的一些心得分享给大家,希望可以帮助大家在学习的时候少走一些弯路,有 ...
- UVA116 Unidirectional TSP 单向TSP
分阶段的DAG,注意字典序的处理和路径的保存. 定义状态d[i][j]为从i,j 出发到最后一列的最小花费,转移的时候只有三种,向上,向下,或平移. #include<bits/stdc++.h ...
- Java poi 的使用
poi可操作老旧版本的excel 下载jar包,http://archive.apache.org/dist/poi/release/bin/poi-bin-3.17-20170915.tar.gz ...
- python基础一 day10(2)
复习: # 三元运算符# 接收结果的变量 = 条件为真的结果 if 条件 else 条件为假的结果# 接收结果的变量 = “真结果” if 条件 else “假结果”## 命名空间 和 作用域# 三种 ...
- JSONP 跨域请求 - 获取JSON数据
如何用原生方式使用JSONP? 下边这一DEMO实际上是JSONP的简单表现形式,在客户端声明回调函数之后,客户端通过script标签向服务器跨域请求数据,然后服务端返回相应的数据并动态执行回调函数. ...
- Springboot 命令注入属性[--]&[-D]
场景 在用Jenkins,做自动化部署时,遇到一些命令问题. 需要通过命令的形式,注入些业务值. -D 系统属性注入 Java,启动jar 命令: java [ options ] -jar file ...
- snprintf()返回值的陷阱
int snprintf(char *restrict buf, size_t n, const char * restrict format, ...); 函数说明:最多从源串中拷贝n-1个字符到 ...
- shell脚本,判断给出的字符串是否相等。
第一种方法[root@localhost wyb]# .sh #!/bin/bash #判断给出的字符串是否相等 read -p "Please Input a number:" ...