Educational Codeforces Round 162 (Rated for Div. 2) E
E:Link
枚举路径两端的颜色 \(k\)。
令 \(g[x]\) 表示满足以下条件的点 \(y\) 数量。
- $ y \in subtree[x]$
- \(col[y] = k\)
- \(y\) 到 \(fa[x]\) 的路径中不存在其他颜色为 \(k\) 的点。
那么
g[x]=\left\{
\begin{aligned}
1 \quad col[x] = k\\
\sum g[y] \quad col[x] \ne k ,\quad y \in son[x]
\end{aligned}
\right
.
\end{equation}
\]
统计最高点为 \(x\) 的合法路径数。
如果 \(col[x] = k\),\(x\) 只能作为端点之一,贡献等于 \(\sum g[y]\)。
否则 \(x\) 作为路径中转点,对于任意两棵子树 \(y\) 和 \(z\),贡献为 \(g[y] \times g[z]\)。
虚树优化即可。
#include<bits/stdc++.h>
#define rep(i, a, b) for(int i = (a); i <= (b); ++ i)
#define per(i, a, b) for(int i = (a); i >= (b); -- i)
#define pb emplace_back
#define All(X) X.begin(), X.end()
using namespace std;
using ll = long long;
constexpr int N = 2e5 + 5;
vector<int> G[N];
int fa[N][19], dep[N], dfn[N], timestamp;
void dfs(int x) {
dfn[x] = ++ timestamp;
for(auto y : G[x]) {
if(y != fa[x][0]) {
dep[y] = dep[x] + 1;
fa[y][0] = x;
rep(i, 1, 18) fa[y][i] = fa[fa[y][i - 1]][i - 1];
dfs(y);
}
}
}
int lca(int x, int y) {
if(dep[x] < dep[y]) swap(x, y);
per(i, 18, 0) if(dep[fa[x][i]] >= dep[y]) x = fa[x][i];
if(x == y) return x;
per(i, 18, 0) if(fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
return fa[x][0];
}
int n, c[N];
vector<int> col[N], H[N];
set<int> se;
ll ans, g[N];
void dp(int x, int k) {
g[x] = 0;
for(int y : H[x]) {
dp(y, k);
}
if(c[x] == k) {
g[x] = 1;
for(int y : H[x]) {
ans += g[y];
}
}
else {
ll t = 0;
for(int y : H[x]) {
ans += g[y] * t;
t += g[y];
g[x] += g[y];
}
}
}
bool cmp(int x, int y) {
return dfn[x] < dfn[y];
}
void calc(vector<int> &a, int k) {
a.pb(1);
sort(All(a), cmp);
int sz = a.size();
rep(i, 1, sz - 1) a.pb(lca(a[i - 1], a[i]));
sort(All(a), cmp);
a.erase(unique(All(a)), a.end());
rep(i, 1, a.size() - 1) H[lca(a[i - 1], a[i])].pb(a[i]);
dp(1, k);
rep(i, 1, a.size() - 1) H[lca(a[i - 1], a[i])].clear();
}
void solve() {
cin >> n;
rep(i, 1, n) cin >> c[i], col[c[i]].pb(i), se.insert(c[i]);
rep(i, 2, n) {
int x, y; cin >> x >> y;
G[x].pb(y);
G[y].pb(x);
}
dfs(dep[1] = 1);
for(int k : se) {
calc(col[k], k);
}
cout << ans << '\n';
rep(i, 1, n) G[i].clear(), col[i].clear();
se.clear();
ans = 0;
timestamp = 0;
}
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int T = 1;
cin >> T;
while(T --) solve();
return 0;
}
Educational Codeforces Round 162 (Rated for Div. 2) E的更多相关文章
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- Educational Codeforces Round 39 (Rated for Div. 2) G
Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...
- Educational Codeforces Round 48 (Rated for Div. 2) CD题解
Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...
- Educational Codeforces Round 60 (Rated for Div. 2) 题解
Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...
随机推荐
- ios应用免签+微信分身
一句话概括:用TrollStore自动加签安装微信ipa文件,实现ios上微信应用分身. 工具: 1. ios14.1 2. GTA Car Tracher 这个应用程序并不是真正的 GTA Car ...
- KingbaseES V8R6集群运维案例之---securecmdd工具usePAM配置
案例说明: 默认在部署securecmdd工具后,在配置文件securecmdd_config中配置参数usePAM=yes,在有的系统主机环境,会导致securecmd客户端连接失败. 适用版本: ...
- KingabseES执行计划-分区剪枝(partition pruning)
概述 分区修剪(Partition Pruning)是分区表性能的查询优化技术 .在分区修剪中,优化器分析SQL语句中的FROM和WHERE子句,以在构建分区访问列表时消除不需要的分区.此功能使数据库 ...
- KingbaseES V8R3集群部署案例之---通用机无ssh环境脚本部署集群
案例说明: 在一些通用机的生产环境,不允许主机之间通过ssh通讯,或者不允许root用户建立ssh互信或登录.默认KingbaseES V8R3集群通用机环境部署需要建立数据库用户及root用户,在集 ...
- 算法学习笔记【4】| 动态规划(Atcoder DP 26题)
动态规划(Atcoder DP 26题) on Atcoder on Luogu 洛谷博客观看体验更佳: Atcoder DP contest 题解 Frog 1 $N$ 个石头,编号为 $1,2,. ...
- .NET Core WebApi 多语言本地化,动态切换多语言
.NET Core WebApi 多语言本地化,动态切换多语言 原生的.net core webapi 动态多语言本地话 具体更多详细内容,可以参考官方文档 首先看效果图 整体项目结构图 开始前需要讲 ...
- .Net单元测试xUnit和集成测试指南(1)
引言 在现代化的软件开发中,单元测试和集成测试是确保代码质量和可靠性的关键部分.ASP.NET Core 社区内提供了强大的单元测试框架,xUnit 是其中之一,它提供了简单.清晰和强大的测试功能,编 ...
- flutter系列之:按比例缩放的AspectRatio和FractionallySizedBox
目录 简介 AspectRatio FractionallySizedBox 总结 简介 我们在构建UI的时候,为了适应不同的屏幕大小,通常需要进行一些自适应的配置,而最常见的自适应就是根据某个宽度或 ...
- Numpy数组索引和切片
数组可以通过索引或切片的方式进行访问或修改,数组切片x[start:stop:step],与Ptyhon内置的list标准索引和切片类似,只是数组产生的是一个非副本视图,根据条件索引的值如果修改,直接 ...
- C# 发布你的程序包到Nuget
1.新建一个.NET Standard 的类库项目 2.选择项目属性,在 package 栏目下填写我们的nuget包信息 3.选择我们的项目,点击"Pack" 打包 主要注意的是 ...