「SDOI2015」寻宝游戏
传送门
Luogu
解题思路
发现一个性质:
对于所有的宝藏点 \({a_1,a_2...a_k}\) ,按照dfs序递增排列,答案就是:
\(dis(a_1, a_2) + dis(a_2, a_3) + \cdots + dis(a_{k-1}, a_k) + dis(a_k, a_1)\)
考虑加入一个点的贡献:
假设加入的点是 \(u\),那么贡献就是 \(dis(L, u) + dis(R, u) - dis(L, R)\)
其中 \(L, R\) 分别是 \(u\) 点的对应dfs序的前驱和后继。
这个应该挺好理解的。
然后我们搞一个 set 就可以维护了。
细节注意事项
- 咕咕咕
参考代码
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <set>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
}
typedef long long LL;
const int _ = 100010;
int tot, head[_], nxt[_ << 1], ver[_ << 1], w[_ << 1];
inline void Add_edge(int u, int v, int d)
{ nxt[++tot] = head[u], head[u] = tot, ver[tot] = v, w[tot] = d; }
int n, m, num, vis[_]; LL dis[_];
int fa[_], siz[_], dep[_], top[_], son[_], dfn[_], id[_];
set < int > s;
set < int > ::iterator it;
inline void dfs1(int u, int f) {
fa[u] = f, dep[u] = dep[f] + 1;
siz[u] = 1, id[dfn[u] = ++num] = u;
for (rg int v, i = head[u]; i; i = nxt[i])
if (!dep[v = ver[i]]) {
dis[v] = dis[u] + w[i];
dfs1(v, u), siz[u] += siz[v];
if (siz[son[u]] < siz[v]) son[u] = v;
}
}
inline void dfs2(int u, int topf) {
top[u] = topf;
if (!son[u]) return; dfs2(son[u], topf);
for (rg int v, i = head[u]; i; i = nxt[i])
if (!top[v = ver[i]]) dfs2(v, v);
}
inline int LCA(int x, int y) {
int fx = top[x], fy = top[y];
while (fx != fy) {
if (dep[fx] < dep[fy]) swap(x, y), swap(fx, fy);
x = fa[fx], fx = top[x];
}
return dep[x] < dep[y] ? x : y;
}
inline LL dist(int x, int y) { return dis[x] + dis[y] - 2 * dis[LCA(x, y)]; }
int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n), read(m);
for (rg int u, v, d, i = 1; i < n; ++i)
read(u), read(v), read(d), Add_edge(u ,v, d), Add_edge(v, u, d);
dfs1(1, 0), dfs2(1, 1);
LL ans = 0;
while (m--) {
int x, d; read(x), d = dfn[x];
if (!vis[x]) s.insert(d);
int l = id[(it = s.lower_bound(d)) == s.begin() ? *--s.end() : *--it];
int r = id[(it = s.upper_bound(d)) == s.end() ? *s.begin() : *it];
if (vis[x]) s.erase(d);
LL delta = dist(l, x) + dist(r, x) - dist(l, r);
if (vis[x]) ans -= delta, vis[x] = 0;
else ans += delta, vis[x] = 1;
printf("%lld\n", ans);
}
return 0;
}
完结撒花 \(qwq\)
「SDOI2015」寻宝游戏的更多相关文章
- 【LOJ】#2182. 「SDOI2015」寻宝游戏
题解 终于了解怎么动态维护虚树了 就是把点按照dfs序排个序啊 这道题显然是求虚树上所有边长的两倍 我们把dfs序排完序,相邻两个点加上路径长(包括首尾),删除的时候删一个点减去它到两边再加上新近相邻 ...
- loj2182 「SDOI2015」寻宝游戏
参考这里 #include <iostream> #include <cstdio> #include <set> using namespace std; typ ...
- Loj #2494. 「AHOI / HNOI2018」寻宝游戏
Loj #2494. 「AHOI / HNOI2018」寻宝游戏 题目描述 某大学每年都会有一次 Mystery Hunt 的活动,玩家需要根据设置的线索解谜,找到宝藏的位置,前一年获胜的队伍可以获得 ...
- 【BZOJ】【3991】【SDOI2015】寻宝游戏
dfs序 我哭啊……这题在考试的时候(我不是山东的,CH大法吼)没想出来……只写了50分的暴力QAQ 而且苦逼的写的比正解还长……我骗点分容易吗QAQ 骗分做法: 1.$n,m\leq 1000$: ...
- 【BZOJ3991】【SDOI2015】寻宝游戏
Description 小B最近正在玩一个寻宝游戏,这个游戏的地图中有N个村庄和N-1条道路,并且任何两个村庄之间有且仅有一条路径可达.游戏开始时,玩家可以任意选择一个村庄,瞬间转移到这个村庄,然 ...
- 【LOJ】#2494. 「AHOI / HNOI2018」寻宝游戏
题面 题解 第\(i\)个数之前的符号是或那么记为0,是与就记为1,得到一个二进数x 然后按位分开考虑,如果这一行是1那么记为1,如果这一位数位0记为0,得到一个二进制数\(b_i\) 第\(N\)行 ...
- 「Githug」Git 游戏通关流程
Githug 他喵的这是个啥!?难道不是 GitHub 拼错了么,和 Git 什么关系? 和游戏又有什么关系? 其实,他的元身在这里:https://github.com/Gazler/githug ...
- 【LOJ】#2067. 「SDOI2016」硬币游戏
题解 c一样的就是一个独立的游戏 我们对于2和3的指数 sg[i][j] 表示\(c \cdot 2^i \cdot 3^j\)的棋子,只有这个硬币是反面,翻转的硬币是正面的sg值 枚举sg函数所有可 ...
- @loj - 2004@ 「SDOI2017」硬币游戏
目录 @description@ @solution@ @accepted code@ @details@ @description@ 周末同学们非常无聊,有人提议,咱们扔硬币玩吧,谁扔的硬币正面次数 ...
随机推荐
- html 中js 如何给字符串加换行符
var str = 你好'+"\n"+ '世界'; 这种写法在html中是会被识别为"你好\n世界" 那么如何保证其这么写会被识别,只需要在该div的样式中加入 ...
- 翻页插件 jquery
//css <style> * { padding:; margin:; list-style: none; } .wrapper { width: 100%; cursor: point ...
- 吴裕雄--天生自然TensorFlow2教程:函数优化实战
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D def himme ...
- 吴裕雄--天生自然TensorFlow2教程:多输出感知机及其梯度
import tensorflow as tf x = tf.random.normal([2, 4]) w = tf.random.normal([4, 3]) b = tf.zeros([3]) ...
- LoRa基础知识
摘自:LoRaWAN介绍 - LoRa从业者读这篇就够了 https://blog.csdn.net/iotisan/article/details/69939241 LoRa网络结构 ...
- java判断字符串是否是数字
正则表达式 代码如下: public static boolean isNum(String num){ return num.matches("(\\s)*([+-])?(([0-9]*\ ...
- 某元素 fixed 后,从页面消失
position: fixed; top: 0; left: 0; width: 100%; z-index: 1; 一开始是定位后,元素就从页面消失了,添加了 层级 之后就可以了.
- Python 基础之面向对象之八步理解装饰器
装饰器:在不改变原有代码的情况下,为该原函数扩展新功能特征:返回新函数,替换旧函数语法:@ 语法糖 1.装饰器原型 #例1: def kuozhan(func): def newfunc(): ...
- 记数问题(0)<P2013_1>
记数问题 (count.cpp/c/pas) [问题描述] 试计算在区间1到n的所有整数中,数字x(0≤x≤9)共出现了多少次?例如,在1到11中,即在1.2.3.4.5.6.7.8.9.10.11 ...
- 入门项目数字手写体识别:使用Keras完成CNN模型搭建(重要)
摘要: 本文是通过Keras实现深度学习入门项目——数字手写体识别,整个流程介绍比较详细,适合初学者上手实践. 对于图像分类任务而言,卷积神经网络(CNN)是目前最优的网络结构,没有之一.在面部识别. ...