E:Link

枚举路径两端的颜色 \(k\)。

令 \(g[x]\) 表示满足以下条件的点 \(y\) 数量。

  • $ y \in subtree[x]$
  • \(col[y] = k\)
  • \(y\) 到 \(fa[x]\) 的路径中不存在其他颜色为 \(k\) 的点。

那么

\[\begin{equation}
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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  4. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  5. 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 ...

  6. 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 ...

  7. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  8. 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 < ...

  9. Educational Codeforces Round 48 (Rated for Div. 2) CD题解

    Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...

  10. Educational Codeforces Round 60 (Rated for Div. 2) 题解

    Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...

随机推荐

  1. 镭速Raysync v6.6.8.0版本发布

    最近镭速发布了v6.6.8.0版本,已经发布上线了.主要更新内容有服务器下发任务支持指定客户端,客户端增加日志清理和日志压缩,自动删除源文件保持源目录结构,支持将文件投递给其他成员等功能,详细的更新内 ...

  2. KingbaseES V8R6 集群运维系列 -- trusted_server

    案例说明: 在KingbaseES V8R3及V8R6早期的版本,对于读写分离的集群如果网关地址无法连通,将会导致整个集群关闭,数据库服务无法访问.在后期版本的改进中,降低了对网关的依赖性,当网关地址 ...

  3. KingbaseESV8R6汉字首字母排序

    目的 本文目的实现汉字首字母排序. 排序规则和字符集的关系如下. select sys_encoding_to_char(collencoding) as encoding,collname,coll ...

  4. KingbaseES 的角色和权限管理

    KingbaseES使用角色的概念管理数据库访问权限.为了方便权限管理,用户可以建立多个角色,对角色进行授权和权限回收,并把角色授予其他用户. 数据库初始化时,会创建一个超级用户的角色:system( ...

  5. Hadoop_05 使用xsync脚本命令分发,手动配置脚本

    在/usr/local/bin 目录下创建 xsync 文件,向里面添加 1 #!/bin/sh 2 # 获取输入参数个数,如果没有参数,直接退出 3 pcount=$# 4 if((pcount== ...

  6. #线段树,离线#CF1000F One Occurrence

    题目 给定一个长度为\(n\)序列,\(m\)个询问,每次询问给定一个区间\([l,r]\), 如果这个区间里存在只出现一次的数,输出这个数(如果有多个就输出任意一个),没有就输出0 分析 考虑离线, ...

  7. #树链剖分,线段树#洛谷 2486 [SDOI2011]染色

    题目 分析 就是把维护颜色段和树结合起来, 注意拼接的时候要减去中间相同的部分 代码 #include <cstdio> #include <cctype> #include ...

  8. OpenAtom OpenHarmony分论坛圆满举办,生态与产业发展迈向新征程

    7月27日,2022开放原子全球开源峰会OpenAtom OpenHarmony分论坛在北京成功举办.本次论坛以"万物互联,使能千行百业"为主题,OpenHarmony共建单位.生 ...

  9. 深入解析 Java 面向对象编程与类属性应用

    Java 面向对象编程 面向对象编程 (OOP) 是一种编程范式,它将程序组织成对象.对象包含数据和操作数据的方法. OOP 的优势: 更快.更易于执行 提供清晰的结构 代码更易于维护.修改和调试 提 ...

  10. Docker 14 Docker Compose

    概述 使用 Docker 的时候,定义 Dockerfile 文件,然后使用 docker build.docker run 等命令操作容器. 然而微服务架构的应用系统一般包含若干个微服务,每个微服务 ...