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 ...
随机推荐
- C# MySQL导出表结构到Excel
软件如图,输入基础信息,点击"测试登录" 连接MySQL需要安装驱动,如下图 连接成功如下图 登录成功后,自动获取所有表信息 双击表名称,右侧查看表结构信息 导出表结构效果如下图 ...
- Java字符串比较 == 和 equals方法的区别
今天在排除一个bug的时候出现了一个很低级但是也很容易被忽视的错误,在此写了一个小例子做记录. 首先我先说一下错误的场景,我读取了一段json数据,并使用JSONObject的实例对象的getStri ...
- Refresh 重构(Refactor)
最近在闲暇之余重(第)温(一..次)此书, 首先能感受到的, 无论你是新程序员还是老程序员, 这本书都已经不具备太多的可读性了. 由于本书成书年代久远, 那个时候软件行业还不够发达, 面向对象还没有被 ...
- #拓扑排序#洛谷 4645 [COCI2006-2007 Contest#3] BICIKLI
题目 这个地方有 \(n\) 个城镇,从 \(1\sim n\) 编号, 其中有 \(m\) 条单向道路连接它们. 比赛将在 \(1\) 号城镇开始并在 \(2\) 号城镇结束. 主办方想知道,一共有 ...
- 使用OHOS SDK构建box2d
参照OHOS IDE和SDK的安装方法配置好开发环境. 从github下载源码,当前最新的提交记录ID为411acc32eb6d4f2e96fc70ddbdf01fe5f9b16230. 执行如下命令 ...
- Windows系统编译libhv带SSL,开启WITH_OPENSSL
需要开发一个https的服务,使用libhv来做,需要重新编译libhv,需要开启 WITH_OPENSSL,前面编译一直很顺利,但是打开VS生成动态库的时候,报错,找不到ssl相关的文件,看了官方的 ...
- HarmonyOS如何高效上架原子化服务?这个平台帮你搞定!
以往HarmonyOS应用和原子化服务都是在AGC(App Gallery Connect)上架,二者的上架流程一样.但应用的形态更加复杂庞大,上架时有很多必填字段,审核标准也相对复杂,而原子化服务的 ...
- centos7或者centos8下安装google-chrome谷歌浏览器 亲测成功 20220302
第一步: wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm 第二步: 安装 Google ...
- 美团二面:如何保证Redis与Mysql双写一致性?连续两个面试问到了!
引言 Redis作为一款高效的内存数据存储系统,凭借其优异的读写性能和丰富的数据结构支持,被广泛应用于缓存层以提升整个系统的响应速度和吞吐量.尤其是在与关系型数据库(如MySQL.PostgreSQL ...
- 架构设计|基于 raft-listener 实现实时同步的主备集群
背景以及需求 线上业务对数据库可用性可靠性要求较高,要求需要有双 AZ 的主备容灾机制. 主备集群要求数据和 schema 信息实时同步,数据同步平均时延要求在 1s 之内,p99 要求在 2s 之内 ...