传送门

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」寻宝游戏的更多相关文章

  1. 【LOJ】#2182. 「SDOI2015」寻宝游戏

    题解 终于了解怎么动态维护虚树了 就是把点按照dfs序排个序啊 这道题显然是求虚树上所有边长的两倍 我们把dfs序排完序,相邻两个点加上路径长(包括首尾),删除的时候删一个点减去它到两边再加上新近相邻 ...

  2. loj2182 「SDOI2015」寻宝游戏

    参考这里 #include <iostream> #include <cstdio> #include <set> using namespace std; typ ...

  3. Loj #2494. 「AHOI / HNOI2018」寻宝游戏

    Loj #2494. 「AHOI / HNOI2018」寻宝游戏 题目描述 某大学每年都会有一次 Mystery Hunt 的活动,玩家需要根据设置的线索解谜,找到宝藏的位置,前一年获胜的队伍可以获得 ...

  4. 【BZOJ】【3991】【SDOI2015】寻宝游戏

    dfs序 我哭啊……这题在考试的时候(我不是山东的,CH大法吼)没想出来……只写了50分的暴力QAQ 而且苦逼的写的比正解还长……我骗点分容易吗QAQ 骗分做法: 1.$n,m\leq 1000$: ...

  5. 【BZOJ3991】【SDOI2015】寻宝游戏

    Description ​ 小B最近正在玩一个寻宝游戏,这个游戏的地图中有N个村庄和N-1条道路,并且任何两个村庄之间有且仅有一条路径可达.游戏开始时,玩家可以任意选择一个村庄,瞬间转移到这个村庄,然 ...

  6. 【LOJ】#2494. 「AHOI / HNOI2018」寻宝游戏

    题面 题解 第\(i\)个数之前的符号是或那么记为0,是与就记为1,得到一个二进数x 然后按位分开考虑,如果这一行是1那么记为1,如果这一位数位0记为0,得到一个二进制数\(b_i\) 第\(N\)行 ...

  7. 「Githug」Git 游戏通关流程

    Githug 他喵的这是个啥!?难道不是 GitHub 拼错了么,和 Git 什么关系? 和游戏又有什么关系? 其实,他的元身在这里:https://github.com/Gazler/githug  ...

  8. 【LOJ】#2067. 「SDOI2016」硬币游戏

    题解 c一样的就是一个独立的游戏 我们对于2和3的指数 sg[i][j] 表示\(c \cdot 2^i \cdot 3^j\)的棋子,只有这个硬币是反面,翻转的硬币是正面的sg值 枚举sg函数所有可 ...

  9. @loj - 2004@ 「SDOI2017」硬币游戏

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 周末同学们非常无聊,有人提议,咱们扔硬币玩吧,谁扔的硬币正面次数 ...

随机推荐

  1. numpy常用函数之arange函数

    2.np.arange([start, ]stop, [step, ]dtype=None) 作用:   arange函数用于创建等差数组 start:可忽略不写,默认从0开始;起始值 stop:结束 ...

  2. Introducing .NET 5

    Today, we’re announcing that the next release after .NET Core 3.0 will be .NET 5. This will be the n ...

  3. 翻页插件 jquery

    //css <style> * { padding:; margin:; list-style: none; } .wrapper { width: 100%; cursor: point ...

  4. 24 JavaScript对象访问器&JavaScript对象构造器

    ES5引入了Getter和Setter Getter和Setter允许定义对象访问器 JavaScript Getter(get关键字):获取对象属性 <script> var perso ...

  5. 吴裕雄--天生自然Numpy库学习笔记:NumPy 位运算

    bitwise_and() 函数对数组中整数的二进制形式执行位与运算. import numpy as np print ('13 和 17 的二进制形式:') a,b = 13,17 print ( ...

  6. Kafka 消息的消费原理

    https://www.cnblogs.com/huxi2b/p/6061110.html 1.老版本的kafka的offset是维护在zk上的,新版本的kafka把consumer的offset维护 ...

  7. Codeforces Round #606 (Div. 2) - E. Two Fairs(割点+dfs)

    题意:给你一张无向连通图,对于求有多少对$(x,y)$满足互相到达必须经过$(a,b)$,其中$x\neq a,x\neq b,y\neq a,y\neq b$ 思路:显然$a,b$都必须为割点,所以 ...

  8. MQTT的签名算法

    一 使用技小新的算法,时间戳不能带参数否则连接失败: 二 网页的签名算法:http://encode.chahuo.com/ 注意将红色的4个参数换成你自己的clientIddeviceName 密钥 ...

  9. C语言-define 与do{}while(0)

    问题引出: 我们都知道宏定义#define只是简单替换,所以遇到复杂的带参数宏,必须很小心的为需要的参数加上括号“()”:同样碰到复杂的多条语句替代,虽然加{}可以将其封装成一个整体,但同时又有另一个 ...

  10. JSON 解析中遇到的坑😭

    最近做加解密遇到一个很“奇葩的问题”,解析服务端加密后的字符串 序列化 时一直报错 "json解析失败:Error Domain=NSCocoaErrorDomain Code=3840 & ...