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 之间的边的最大权 思路:树剖处理边权.由于是边,所以只需要把边权处理到子节 ...
随机推荐
- CSS布局之-强大的负边距
css中的负边距(negative margin)是布局中的一个常用技巧,只要运用得合理常常会有意想不到的效果.很多特殊的css布局方法都依赖于负边距,所以掌握它的用法对于前端的同学来说,那是必须的. ...
- 洛谷 P2068 统计和
题目描述 给定一个长度为n(n<=100000),初始值都为0的序列,x(x<=10000)次的修改某些位置上的数字,每次加上一个数,然后提出y (y<=10000)个问题,求每段区 ...
- 洛谷 P1744 采购特价商品
题目背景 <爱与愁的故事第三弹·shopping>第一章. 题目描述 中山路店山店海,成了购物狂爱与愁大神的“不归之路”.中山路上有n(n<=100)家店,每家店的坐标均在-1000 ...
- nginx “403 Forbidden” 错误 解决方法
错误的原因是缺少index.html或者index.php文件,就是配置文件中index index.html index.htm这行中的指定的文件 只需要配置时加一句 index index.h ...
- 前台解析json的方法
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScript 原生格式,这意 ...
- CS193p Lecture 9 - Animation, Autolayout
Animation(动画) Demo Dropit续 Autolayout(自动布局) 三种添加自动布局的方法: 使用蓝色辅助虚线,右键选择建议约束(Reset to Suggested Constr ...
- vue 使用element-ui实现城市三级联动
<template> <div> <el-select v-model="prov" style="width:167px;margin-r ...
- mysql的字符串处理函数用法
1.LOCATE函数 LOCATE(substr,str) 返回子串 substr 在字符串 str 中第一次出现的位置.如果子串 substr 在 str 中不存在,返回值为 0.如果substr或 ...
- C/SV/VERILOG语句块界定符不一样
C是一对大括号{} SV /VERILOG 是begin...end
- 解决 mounting /dev/block/mmcblk0p1 on /sdcard failed
http://www.liyu8.com/article/sdcard.htm 之前在recovery下的adb shell执行mount -a总是会有 mount: mouting /dev/blo ...