传送门

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. 【代码总结】GD库中简单的验证码

    大体思路: 代码部分: <?php //1.创建画布 $img = imagecreatetruecolor(100,30); //2.设置颜色 值越小,颜色越深 $color1 = image ...

  2. splash-简介及入门

    splash 1.      splash简介 Splash是一个JavaScript渲染服务,是一个带有HTTP API的轻量级浏览器,同时它对接了Python中的Twisted和QT库.利用它,我 ...

  3. java里判断字符串是否为数字类型的方法

    String type = "数字类型";if(StringUtils.isNotBlank(value)){ //区分正负数 if(value.startsWith(" ...

  4. spring boot 配置时区差别

    前提 数据库时区:GMT+8 show variables like '%time_zone%'; 本机电脑时区: 情景一.不指定时区 传递的参数映射到Data不指定时区,连接数据库不指定时区,保存时 ...

  5. Java面向对象编程 -1.3

    类与对象的定义与使用 在Java之中类是一个独立的结构体,所以需要使用class来进行定义,而类之中主要由属性和方法所组成,那么属性就是一个个具体的变量,方法就是可以重复执行的代码. 定义一个类 cl ...

  6. [运维] 如何在云服务器上安装 MySQL 数据库, 并使用 Navicat 实现远程连接管理

    .•●•✿.。.:*.•●•✿.。.:*.•●•✿.。.:*.•●•✿.。.:*.•●•✿.。.:*.•●•✿.。.:*.•.•●•✿.。.:*.•●•✿.。.:*.•●•✿.。.:*.•●•✿.。. ...

  7. paramiko linux pip18.1

    Collecting paramiko Downloading https://files.pythonhosted.org/packages/cf/ae/94e70d49044ccc234bfdba ...

  8. Python3.5学习之旅——day5

    模块初识 一.定义 在python中,模块是用来实现某一特定功能的代码集合.其本质上就是以‘.py’结尾的python文件.例如某文件名为test.py,则模块名为test. 二.导入方法 我们在这一 ...

  9. kubernetes从入门到放弃(二)

    kubernetes对象之pod 1.pod的认识 Pod直译是豆荚,可以把容器想像成豆荚里的豆子,把一个或多个关系紧密的豆子包在一起就是豆荚(一个Pod).在Kubernetes中我们不会直接操作容 ...

  10. Write-up-Violator

    关于 下载地址:点我 Flag:/root/flag.txt 哔哩哔哩:视频 信息收集 网卡:虚拟机vmnet8 ➜ ~ ip addr show dev vmnet8 5: vmnet8: < ...