标签:queue mat log clu vector 打开 roo 数据 getc
[BZOJ1095][ZJOI2007]Hide 捉迷藏
试题描述
输入
输出
输入示例
8 1 2 2 3 3 4 3 5 3 6 6 7 6 8 7 G C 1 G C 2 G C 1 G
输出示例
4 3 3 4
数据规模及约定
对于100%的数据, N ≤100000, M ≤500000。
题解
这题一看就肯定是动态点分治。
然而 sb 的我并没有自己想出具体做法。。。
建立好重心树后,我们维护三种堆:(以下的描述都是在重心树上的,与原树无关)
1. 每个节点一个堆维护子树中所有节点到它父亲的距离;
2. 每个节点一个堆维护所有儿子的子树到自己的最大距离(维护的是这些最大距离的集合);
3. 一个堆维护每个节点对应的子树中经过它的最长链(维护的是这些最长链的集合)。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std;
const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
int read() {
int x = 0, f = 1; char c = Getchar();
while(!isdigit(c)){ if(c == ‘-‘) f = -1; c = Getchar(); }
while(isdigit(c)){ x = x * 10 + c - ‘0‘; c = Getchar(); }
return x * f;
}
#define maxn 100010
#define maxm 200010
#define maxlog 18
int n, m, head[maxn], nxt[maxm], to[maxm];
void AddEdge(int a, int b) {
to[++m] = b; nxt[m] = head[a]; head[a] = m;
swap(a, b);
to[++m] = b; nxt[m] = head[a]; head[a] = m;
return ;
}
int mnd[maxlog][maxn<<1], clo, dfn[maxn], dep[maxn], Log[maxn<<1];
void build(int u, int pa) {
dfn[u] = ++clo;
mnd[0][clo] = dep[u];
for(int e = head[u]; e; e = nxt[e]) if(to[e] != pa)
dep[to[e]] = dep[u] + 1, build(to[e], u), mnd[0][++clo] = dep[u];
return ;
}
void rmq_init() {
Log[1] = 0;
for(int i = 2; i <= clo; i++) Log[i] = Log[i>>1] + 1;
for(int j = 1; (1 << j) <= clo; j++)
for(int i = 1; i + (1 << j) - 1 <= clo; i++)
mnd[j][i] = min(mnd[j-1][i], mnd[j-1][i+(1<<j-1)]);
return ;
}
int cdist(int a, int b) {
int l = dfn[a], r = dfn[b];
if(l > r) swap(l, r);
int t = Log[r-l+1];
return dep[a] + dep[b] - (min(mnd[t][l], mnd[t][r-(1<<t)+1]) << 1);
}
int rt, size, f[maxn], siz[maxn];
bool vis[maxn];
void getroot(int u, int pa) {
siz[u] = 1; f[u] = 0;
for(int e = head[u]; e; e = nxt[e]) if(to[e] != pa && !vis[to[e]]) {
getroot(to[e], u);
siz[u] += siz[to[e]];
f[u] = max(f[u], siz[to[e]]);
}
f[u] = max(f[u], size - siz[u]);
if(f[rt] > f[u]) rt = u;
return ;
}
void dfs(int u, int pa) {
siz[u] = 1;
for(int e = head[u]; e; e = nxt[e]) if(to[e] != pa && !vis[to[e]])
dfs(to[e], u), siz[u] += siz[to[e]];
return ;
}
int fa[maxn];
void solve(int u) {
vis[u] = 1;
for(int e = head[u]; e; e = nxt[e]) if(!vis[to[e]]) {
dfs(to[e], u);
f[rt = 0] = size = siz[to[e]]; getroot(to[e], u);
fa[rt] = u; solve(rt);
}
return ;
}
priority_queue <int> tofa[maxn], tofa_del[maxn], son[maxn], son_del[maxn], ans, ans_del;
bool has[maxn];
void tofadel(int u, int d) {
tofa_del[u].push(d);
while(!tofa[u].empty() && !tofa_del[u].empty() && tofa[u].top() == tofa_del[u].top())
tofa[u].pop(), tofa_del[u].pop();
return ;
}
void sondel(int u, int d) {
son_del[u].push(d);
while(!son[u].empty() && !son_del[u].empty() && son[u].top() == son_del[u].top())
son[u].pop(), son_del[u].pop();
return ;
}
void ansdel(int d) {
ans_del.push(d);
while(!ans.empty() && !ans_del.empty() && ans.top() == ans_del.top())
ans.pop(), ans_del.pop();
return ;
}
int upans(int u) {
if(son[u].empty()) return -1;
int t1 = son[u].top(); son[u].pop();
while(!son[u].empty() && !son_del[u].empty() && son[u].top() == son_del[u].top())
son[u].pop(), son_del[u].pop();
if(son[u].empty()){ son[u].push(t1); return -1; }
int t2 = son[u].top();
son[u].push(t1);
return t1 + t2;
}
void update(int s) {
int tmp = upans(s);
if(tmp >= 0) ansdel(tmp);
if(!has[s]) son[s].push(0);
else sondel(s, 0);
tmp = upans(s);
if(tmp >= 0) ans.push(tmp);
for(int u = s; fa[u]; u = fa[u]) {
int d = cdist(s, fa[u]);
tmp = upans(fa[u]);
if(tmp >= 0) ansdel(tmp);
if(!has[s]) {
if(tofa[u].empty()) son[fa[u]].push(d);
else if(tofa[u].top() < d) sondel(fa[u], tofa[u].top()), son[fa[u]].push(d);
tofa[u].push(d);
}
else {
tofadel(u, d);
if(tofa[u].empty()) sondel(fa[u], d);
else if(tofa[u].top() < d) sondel(fa[u], d), son[fa[u]].push(tofa[u].top());
}
tmp = upans(fa[u]);
if(tmp >= 0) ans.push(tmp);
}
has[s] ^= 1;
return ;
}
int main() {
n = read();
for(int i = 1; i < n; i++) {
int a = read(), b = read();
AddEdge(a, b);
}
build(1, 0);
rmq_init();
f[rt = 0] = size = n; getroot(1, 0);
solve(rt);
for(int i = 1; i <= n; i++) update(i);
int q = read();
while(q--) {
char cmd = Getchar();
while(!isalpha(cmd)) cmd = Getchar();
if(cmd == ‘G‘) printf("%d\n", ans.empty() ? -1 : ans.top());
else update(read());
}
return 0;
}
1A 了好爽 2333333
标签:queue mat log clu vector 打开 roo 数据 getc
原文地址:http://www.cnblogs.com/xiao-ju-ruo-xjr/p/6663759.html