Solution -「LOCAL」画画图
\(\mathcal{Description}\)
给定一棵 \(n\) 个点的树形随机的带边权树,求所有含奇数条边的路径中位数之和。树形生成方式为随机取不连通两点连边直到全部连通。
\(n\le32000\)。
\(\mathcal{Solution}\)
考虑用中位数的标准姿势统计每条边的贡献——小于它的设为 \(-1\),大于它的设为 \(+1\),边权相等按编钦定大小关系。那么这条边的贡献就是路径两端权值加和为 \(0\) 的路径对数(显然每对路径连起来都是奇数路径)。
令 \(f(u,i)\) 表示 \(u\) 子树内到 \(u\) 路径权值和为 \(i\) 的结点数量。对于一条边 \((u,v)\),不妨设 \(u\) 是 \(v\) 的父亲,设我们已经得到了全树的 DP 信息,考虑到题目中随机树的期望深度为 \(\mathcal O(\sqrt n)\),所以暴力爬树统计 \(v\) 子树内和 \(v\) 子树外的信息即可求到当前答案。顺序枚举边,每条边仅会由 \(+1\) 变为 \(-1\),所以暴力仍然暴力爬树修改 DP 信息即可。实现上,利用树深限制,拿一个内存池储存 DP 信息;爬树统计时限制可能贡献答案的权值区间即可通过本题。
复杂度 \(\mathcal O(n^2)\),不过可以算出带一个 \(\frac{1}6\) 的常数,所以可过。
\(\mathcal{Code}\)
这里以直径中点作为根(为了和某毒瘤比赛卡常),不过随便选一个根都是可过的。
#include <queue>
#include <cstdio>
#include <cstring>
typedef long long LL;
const int MAXN = 32000, MAXSQRT = 180, MAXV = 1e6;
int n, ecnt, head[MAXN + 5], fa[MAXN + 5], dep[MAXN + 5], faw[MAXN + 5];
int mempool[MAXN * MAXSQRT * 2], *f[MAXN + 5], *frepos = mempool, *g;
int pre[MAXN + 5], buc[MAXV + 5], ori[MAXN + 5], down[MAXN + 5];
struct Edge { int to, cst, nxt; } graph[MAXN * 2 + 5];
struct EdgeSet { int u, v, w; } eset[MAXN + 5];
inline int rint () {
int x = 0; char s = getchar ();
for ( ; s < '0' || '9' < s; s = getchar () );
for ( ; '0' <= s && s <= '9'; s = getchar () ) x = x * 10 + ( s ^ '0' );
return x;
}
inline int min_ ( const int a, const int b ) { return a < b ? a : b; }
inline int max_ ( const int a, const int b ) { return a < b ? b : a; }
inline void link ( const int s, const int t, const int c ) {
graph[++ ecnt] = { t, c, head[s] };
head[s] = ecnt;
}
inline void BFS ( const int s ) {
static std::queue<int> que;
que.push ( s ); dep[s] = 1;
while ( ! que.empty () ) {
int u = que.front (); que.pop ();
for ( int i = head[u], v; i; i = graph[i].nxt ) {
if ( ! dep[v = graph[i].to] ) {
pre[v] = u, dep[v] = dep[u] + 1;
que.push ( v );
}
}
}
}
inline void init ( const int u ) {
dep[u] = 0;
for ( int i = head[u], v; i; i = graph[i].nxt ) {
if ( ( v = graph[i].to ) ^ fa[u] ) {
fa[v] = u, faw[v] = 1, down[graph[i].cst] = v, init ( v );
if ( dep[v] + 1 > dep[u] ) dep[u] = dep[v] + 1;
}
}
f[u] = frepos += dep[u] + 1, frepos += dep[u] + 1;
}
inline void DP ( const int u ) {
*f[u] = 1;
for ( int i = head[u], v; i; i = graph[i].nxt ) {
if ( ( v = graph[i].to ) ^ fa[u] ) {
DP ( v );
for ( int j = -dep[v]; j <= dep[v]; ++ j ) f[u][j + 1] += f[v][j];
}
}
}
inline LL update ( const int s ) {
int l = -dep[s], r = dep[s]; faw[s] = -1;
for ( int u = fa[s], v = s, d1 = 0, d2 = 0; u; u = fa[v = u] ) {
d1 += v ^ s ? faw[v] : 1, d2 += faw[v];
for ( int i = -dep[s]; i <= dep[s]; ++ i ) {
f[u][i + d1] -= f[s][i];
f[u][i + d2] += f[s][i];
}
for ( int i = max_ ( -dep[u], l ), t = min_ ( dep[u], r ), d = d2 + 1; i <= t; ++ i ) {
g[i + d] += f[u][i] - f[v][i - faw[v]];
}
l -= faw[u], r -= faw[u];
}
LL ret = 0;
for ( int i = -dep[s]; i <= dep[s]; ++ i ) {
ret += 1ll * f[s][i] * g[-i];
g[-i] = 0;
}
return ret;
}
int main () {
n = rint (); int mxw = 0;
for ( int i = 1, u, v, w; i < n; ++ i ) {
u = rint (), v = rint (), w = rint ();
eset[i] = { u, v, w }, ++ buc[w];
if ( w > mxw ) mxw = w;
}
for ( int i = 2; i <= mxw; ++ i ) buc[i] += buc[i - 1];
for ( int i = 1; i < n; ++ i ) {
eset[i].w = buc[ori[buc[eset[i].w]] = eset[i].w] --;
link ( eset[i].u, eset[i].v, eset[i].w );
link ( eset[i].v, eset[i].u, eset[i].w );
}
BFS ( 1 );
int s = 0, t = 0;
for ( int i = 1; i <= n; ++ i ) {
if ( dep[i] > dep[s] ) s = i;
dep[i] = pre[i] = 0;
}
BFS ( s );
for ( int i = 1; i <= n; ++ i ) if ( dep[i] > dep[t] ) t = i;
for ( int i = dep[s] + dep[t] - 2 >> 1; i --; t = pre[t] );
init ( t ), DP ( t );
g = frepos += dep[t] + 1, frepos += dep[t] + 1;
LL ans = 0;
for ( int i = 1; i < n; ++ i ) ans += ori[i] * update ( down[i] );
printf ( "%lld\n", ans );
return 0;
}
Solution -「LOCAL」画画图的更多相关文章
- Solution -「LOCAL」二进制的世界
\(\mathcal{Description}\) OurOJ. 给定序列 \(\{a_n\}\) 和一个二元运算 \(\operatorname{op}\in\{\operatorname{ ...
- Solution -「LOCAL」大括号树
\(\mathcal{Description}\) OurTeam & OurOJ. 给定一棵 \(n\) 个顶点的树,每个顶点标有字符 ( 或 ).将从 \(u\) 到 \(v\) ...
- Solution -「LOCAL」过河
\(\mathcal{Description}\) 一段坐标轴 \([0,L]\),从 \(0\) 出发,每次可以 \(+a\) 或 \(-b\),但不能越出 \([0,L]\).求可达的整点数. ...
- Solution -「LOCAL」Drainage System
\(\mathcal{Description}\) 合并果子,初始果子的权值在 \(1\sim n\) 之间,权值为 \(i\) 的有 \(a_i\) 个.每次可以挑 \(x\in[L,R]\) ...
- Solution -「LOCAL」Burning Flowers
灼之花好评,条条生日快乐(假装现在 8.15)! \(\mathcal{Description}\) 给定一棵以 \(1\) 为根的树,第 \(i\) 个结点有颜色 \(c_i\) 和光亮值 ...
- Solution -「LOCAL」ZB 平衡树
\(\mathcal{Description}\) OurOJ. 维护一列二元组 \((a,b)\),给定初始 \(n\) 个元素,接下来 \(m\) 次操作: 在某个位置插入一个二元组: 翻 ...
- Solution -「LOCAL」舟游
\(\mathcal{Description}\) \(n\) 中卡牌,每种三张.对于一次 \(m\) 连抽,前 \(m-1\) 次抽到第 \(i\) 种的概率是 \(p_i\),第 \(m\) ...
- Solution -「LOCAL」充电
\(\mathcal{Description}\) 给定 \(n,m,p\),求序列 \(\{a_n\}\) 的数量,满足 \((\forall i\in[1,n])(a_i\in[1,m])\l ...
- Solution -「LOCAL」「cov. 牛客多校 2020 第五场 C」Easy
\(\mathcal{Description}\) Link.(完全一致) 给定 \(n,m,k\),对于两个长度为 \(k\) 的满足 \(\left(\sum_{i=0}^ka_i=n\r ...
随机推荐
- maven pom.xml 的 spring-boot-maven-plugin 红色报错 解决
解决方法,添加对应的spring boot 版本号即可
- CentOS7中安装pip的方法
1.安装epel-release [root@localhost ~]# yum -y install epel-release 2.安装python-pip [root@localhost ~]# ...
- spring cloud --- Ribbon 客户端负载均衡 + RestTemplate + Hystrix 熔断器 [服务保护] ---心得
spring boot 1.5.9.RELEASE spring cloud Dalston.SR1 1.前言 当超大并发量并发访问一个服务接口时,服务器会崩溃 ,不仅导致这个接口无法 ...
- Elasticsearch打造全文搜索引擎(一)
带着问题上路--ES是如何产生的? (1)思考:大规模数据如何检索? 如:当系统数据量上了10亿.100亿条的时候,我们在做系统架构的时候通常会从以下角度去考虑问题: 1)用什么数据库好?(mysq ...
- Centos安装DenyHosts
一.背景 个人申请的腾讯云主机被扫描端口,数百次登录失败的尝试 解决方法:安装工具,记录并屏蔽恶意访问. 二.检查环境 系统安装的sshd是否支持tcp_wrappers(默认都支持) ldd /us ...
- JMM模型基础知识笔记
概述 内存模型可以理解为在特定的操作协议下,对特定的内存或者高速缓存进行读写访问的过程抽象,不同架构下的物理机拥有不一样的内存模型,Java虚拟机也有自己的内存模型,即Java内存模型(JavaMem ...
- 基于华为云服务器的FTP站点搭建
前言 主要介绍了华为云上如何使用弹性云服务器的Linux实例使用vsftpd软件搭建FTP站点.vsftpd全称是"very secure FTP daemon",是一款在Linu ...
- 【记录一个问题】在goland中的_test.go文件中,点右键点run,无法执行测试用例
比较奇怪的是: 在命令行下,用 test -v alloc_test.go -test.run TestAlloc_utilJoinCPUAndGpu alloc.go 可以执行测试用例 比较奇怪的是 ...
- 【记录一个问题】cuda核函数可能存在栈溢出,导致main()函数退出后程序卡死30秒CUDA
调试一个CUDA核函数过程中发现一个奇怪的问题:调用某个核函数,程序耗时33秒,并且主要时间是main()函数结束后的33秒:而注释掉此核函数,程序执行不到1秒. 由此可见,可能是某种栈溢出,导致了程 ...
- Redis的过期删除策略(和内存淘汰机制)-转
版权声明:本文为CSDN博主「奥修诺斯」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn.net/qq_39944869/ ...