[CF1830D] Mex Tree
题目描述
You are given a tree with $ n $ nodes. For each node, you either color it in $ 0 $ or $ 1 $ .
The value of a path $ (u,v) $ is equal to the MEX $ ^\dagger $ of the colors of the nodes from the shortest path between $ u $ and $ v $ .
The value of a coloring is equal to the sum of values of all paths $ (u,v) $ such that $ 1 \leq u \leq v \leq n $ .
What is the maximum possible value of any coloring of the tree?
$ ^{\dagger} $ The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance:
- The MEX of $ [2,2,1] $ is $ 0 $ , because $ 0 $ does not belong to the array.
- The MEX of $ [3,1,0,1] $ is $ 2 $ , because $ 0 $ and $ 1 $ belong to the array, but $ 2 $ does not.
- The MEX of $ [0,3,1,2] $ is $ 4 $ because $ 0 $ , $ 1 $ , $ 2 $ , and $ 3 $ belong to the array, but $ 4 $ does not.
输入格式
Each test contains multiple test cases. The first line of input contains a single integer $ t $ ( $ 1 \le t \le 10^4 $ ) — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer $ n $ ( $ 1 \le n \le 2 \cdot 10^5 $ ) — the number of nodes in the tree.
The following $ n-1 $ lines of each test case contains $ 2 $ integers $ a_i $ and $ b_i $ ( $ 1 \leq a_i, b_i \leq n, a_i \neq b_i $ ) — indicating an edge between vertices $ a_i $ and $ b_i $ . It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of $ n $ across all test cases does not exceed $ 2 \cdot 10^5 $ .
考虑 dp。
正着dp 不好做,先设所有的路径的 mex 都是 2,然后减去不为 2 的路径的贡献。
路径的 mex 不为 2,当且仅当两个路径在一个同色连通块中。
考虑对极大同色连通块进行 dp,一个极大 \(j\) 色连通块的贡献是 \(\frac{i(i+1)(j+1)}2\)
发现黑白染色的代价是 \(1.5n\) 左右的,所以最优方案里极大同色连通块是 \(O(\sqrt{n})\) 级别的。
所以可以直接树形dp,可以证明总复杂度是 \(O(\sqrt{n})\) 的。
但是空间开不下?
儿子节点只会在父亲时才会 dp 到,所以父亲用完就释放掉。由于一个点只开了 \(sz_x\) 大小的数组,所以空间是 \(O(n)\) 的。
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5,INF=1e9;
int t,n,hd[N],e_num,sz[N],ss;
vector<int>dp[N][2];
int ans;
struct edge{
int v,nxt;
}e[N<<1];
int read()
{
int s=0;
char ch=getchar();
while(ch<'0'||ch>'9')
ch=getchar();
while(ch>='0'&&ch<='9')
s=s*10+ch-48,ch=getchar();
return s;
}
void add_edge(int u,int v)
{
e[++e_num]=(edge){v,hd[u]};
hd[u]=e_num;
e[++e_num]=(edge){u,hd[v]};
hd[v]=e_num;
}
void dfs(int x,int y)
{
sz[x]=1;
dp[x][0].resize(2);
dp[x][1].resize(2);
for(int i=hd[x];i;i=e[i].nxt)
{
if(e[i].v==y)
continue;
dfs(e[i].v,x);
dp[x][0].resize(min(ss,sz[x]+sz[e[i].v]+1),INF),dp[x][1].resize(min(ss,sz[x]+sz[e[i].v]+1),INF);
int r1=INF,r2=INF;
for(int j=1;j<dp[e[i].v][0].size();j++)
{
r1=min(r1,dp[e[i].v][0][j]+j*(j+1)/2);
r2=min(r2,dp[e[i].v][1][j]+j*(j+1));
}
for(int k=dp[x][0].size()-1;k;k--)
{
dp[x][0][k]+=r2;
dp[x][1][k]+=r1;
for(int j=max(1,k-sz[x]);j<k&&j<dp[e[i].v][0].size();j++)
{
dp[x][0][k]=min(dp[x][0][k],dp[e[i].v][0][j]+dp[x][0][k-j]);
dp[x][1][k]=min(dp[x][1][k],dp[e[i].v][1][j]+dp[x][1][k-j]);
}
}
dp[e[i].v][0].clear(),dp[e[i].v][1].clear();
dp[e[i].v][0].shrink_to_fit(),dp[e[i].v][1].shrink_to_fit();
sz[x]+=sz[e[i].v];
}
}
int main()
{
t=read();
while(t--)
{
n=read();
ss=sqrt(n)*2;
for(int i=1;i<=n;i++)
{
dp[i][0].clear(),dp[i][0].shrink_to_fit();
dp[i][1].clear(),dp[i][1].shrink_to_fit();
hd[i]=0;
}
e_num=0;
for(int i=1;i<n;i++)
add_edge(read(),read());
dfs(1,0);
ans=INF;
for(int i=1;i<dp[1][1].size();i++)
ans=min({ans,dp[1][0][i]+i*(i+1)/2,dp[1][1][i]+i*(i+1)});
printf("%lld\n",n*(n+1LL)-ans);
}
}
[CF1830D] Mex Tree的更多相关文章
- [CF1527D] MEX Tree (lca)
题面 给你一棵 n n n 个结点的树,对于所有的 k ∈ [ 0 , n ] k\in[0,n] k∈[0,n] ,求出 M E X = k {\rm MEX}=k MEX=k 的路径数量. 一条路 ...
- SPOJ COT3 Combat on a tree(Trie树、线段树的合并)
题目链接:http://www.spoj.com/problems/COT3/ Alice and Bob are playing a game on a tree of n nodes.Each n ...
- UOJ#266. 【清华集训2016】Alice和Bob又在玩游戏 博弈,DSU on Tree,Trie
原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ266.html 题解 首先我们可以直接暴力 $O(n^2)$ 用 sg 函数来算答案. 对于一个树就是枚举 ...
- 主席树||可持久化线段树+离散化 || 莫队+分块 ||BZOJ 3585: mex || Luogu P4137 Rmq Problem / mex
题面:Rmq Problem / mex 题解: 先离散化,然后插一堆空白,大体就是如果(对于以a.data<b.data排序后的A)A[i-1].data+1!=A[i].data,则插一个空 ...
- 【BZOJ3585】mex
Description 有一个长度为n的数组{a1,a2,-,an}.m次询问,每次询问一个区间内最小没有出现过的自然数. Input 第一行n,m. 第二行为n个数. 从第三行開始,每行一个询问l, ...
- Educational Codeforces Round 23 F. MEX Queries 离散化+线段树
F. MEX Queries time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- CF1083C Max Mex 线段树
题面 CF1083C Max Mex 题解 首先我们考虑,如果一个数x是某条路径上的mex,那么这个数要满足什么条件? 1 ~ x - 1的数都必须出现过. x必须没出现过. 现在我们要最大化x,那么 ...
- 【线段树】【CF1083C】 Max Mex
Description 给定一棵有 \(n\) 个点的树,每个节点有点权.所有的点权构成了一个 \(0~\sim~n - 1\) 的排列.有 \(q\) 次操作,每次操作 \(1\) 为交换两个点的点 ...
- codeforces:MEX Queries分析和实现
首先说明一下MEX,设S是自然数集合N的一个子集,那么S的MEX则为min(N\S),即不包含于S的最小自然数. 题目大意是存在一个空集S,提供n组输入(n<10^5),每组输入对应下面的一个指 ...
- HDU 4747 Mex(线段树)(2013 ACM/ICPC Asia Regional Hangzhou Online)
Problem Description Mex is a function on a set of integers, which is universally used for impartial ...
随机推荐
- 2.go语言基础类型漫游
本篇前瞻 本篇是go语言的基础篇,主要是帮助大家梳理一下go语言的基本类型,注意本篇有参考go圣经,如果你有完整学习的需求可以看一下,另外,go语言的基本类型比较简单,介绍过程就比较粗暴. 基本类型 ...
- mall :sa-token项目源码解析
目录 一.mall开源项目 1.1 来源 1.2 项目转移 1.3 项目克隆 二.Sa-Toekn框架 2.1 Sa-Token 简介 2.2 分布式后端项目的使用流程 2.3 分布式后端项目的使用场 ...
- 非全自研可视化表达引擎-RuleLinK
说在前面 工作中经常会遇到这样的场景: 帮忙把小贝拉门店 商品金额在5w以内,产康订单最多95折. 帮忙把圣贝拉门店 开业时间在6个月内,折扣低于7折要发起审批 帮忙把宁波太平洋店设置独立合同模板 帮 ...
- RabbitMQ 如何实现延迟队列?
延迟队列是指当消息被发送以后,并不是立即执行,而是等待特定的时间后,消费者才会执行该消息. 延迟队列的使用场景有以下几种: 未按时支付的订单,30 分钟过期之后取消订单. 给活跃度比较低的用户间隔 N ...
- 【题解】Educational Codeforces Round 142(CF1792)
没有手速,再加上被 E 卡了,废掉了. A.GamingForces 题目描述: Monocarp 正在玩电脑游戏.他打算杀死 \(n\) 个怪兽,第 \(i\) 个的血量为 \(h_i\). Mon ...
- 探秘移动端BI:发展历程与应用前景解析
什么是移动端BI 维基百科 上对于 移动端商业智能的定义是这样的 > Mobile BI is a system that presents historical and real-time i ...
- 从 5s 到 0.5s!CompletableFuture 异步任务优化技巧,确实优雅!
一个接口可能需要调用 N 个其他服务的接口,这在项目开发中还是挺常见的.举个例子:用户请求获取订单信息,可能需要调用用户信息.商品详情.物流信息.商品推荐等接口,最后再汇总数据统一返回. 如果是串行( ...
- python入门基础(14)--类的属性、成员方法、静态方法以及继承、重载
上一篇提到过类的属性,但没有详细介绍,本篇详细介绍一下类的属性 一 .类的属性 方法是用来操作数据的,而属性则是建模必不的内容,而且操作的数据,大多数是属性,比如游戏中的某个boss类,它的生命值就是 ...
- mpi转以太网连接300PLC与施耐德 Quantum PLC 通讯
S7300 PLC转以太网无需编程与施耐德 Quantum PLC modbusTCP通信 方案介绍: 西门子300PLC转以太网不需要编写程序通过兴达易控MPI-ETH-XD1.0与施耐德 Quan ...
- 【c#版本Openfeign】Net8 自带OpenFeign实现远程接口调用
引言 相信巨硬,我们便一直硬.Net版本到现在已经出了7了,8也已经在预览版了,相信在一个半月就会正式发布,其中也有很多拭目以待的新功能了,不仅仅有Apm和Tap的结合,TaskToAscynResu ...