zoukankan      html  css  js  c++  java
  • [BZOJ1095][ZJOI2007]Hide 捉迷藏

    [BZOJ1095][ZJOI2007]Hide 捉迷藏

    试题描述

    捉迷藏 Jiajia和Wind是一对恩爱的夫妻,并且他们有很多孩子。某天,Jiajia、Wind和孩子们决定在家里玩捉迷藏游戏。他们的家很大且构造很奇特,由N个屋子和N-1条双向走廊组成,这N-1条走廊的分布使得任意两个屋子都互相可达。游戏是这样进行的,孩子们负责躲藏,Jiajia负责找,而Wind负责操纵这N个屋子的灯。在起初的时候,所有的灯都没有被打开。每一次,孩子们只会躲藏在没有开灯的房间中,但是为了增加刺激性,孩子们会要求打开某个房间的电灯或者关闭某个房间的电灯。为了评估某一次游戏的复杂性,Jiajia希望知道可能的最远的两个孩子的距离(即最远的两个关灯房间的距离)。 我们将以如下形式定义每一种操作: C(hange) i 改变第i个房间的照明状态,若原来打开,则关闭;若原来关闭,则打开。 G(ame) 开始一次游戏,查询最远的两个关灯房间的距离。

    输入

    第一行包含一个整数N,表示房间的个数,房间将被编号为1,2,3…N的整数。接下来N-1行每行两个整数a, b,表示房间a与房间b之间有一条走廊相连。接下来一行包含一个整数Q,表示操作次数。接着Q行,每行一个操作,如上文所示。

    输出

    对于每一个操作Game,输出一个非负整数到hide.out,表示最远的两个关灯房间的距离。若只有一个房间是关着灯的,输出0;若所有房间的灯都开着,输出-1。

    输入示例

    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
    ", ans.empty() ? -1 : ans.top());
    		else update(read());
    	}
    	
    	return 0;
    }
    

    1A 了好爽 2333333

  • 相关阅读:
    引用类型构造器
    正则指引量词
    Ajax的XMLHttpRequest对象
    正则指引字符组
    方法可变数量的参数
    不使用XMLHttpRequest实现异步加载:Iframe和script
    可选参数、命名参数
    常量和字段
    正则指引括号
    值类型实例构造器
  • 原文地址:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/6663759.html
Copyright © 2011-2022 走看看