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 ...
随机推荐
- Could not create connection to database server.Attempted reconnect 3 times .Giving up 解决
错误信息 Could not create connection to database server.Attempted reconnect 3 times .Giving up. message ...
- MongoDB java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Integer
详细报错如下: java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Integer at or ...
- 立创EDA的使用
立创EDA的使用 1.实验原理 最近在使用立创EDA来做电路作业,这里记录一下立创EDA的基本操作,以后小型的电路设计可以在其主页完成.立创EDA是一个可以线上完成电路设计仿真以及布线的免费设计工具, ...
- KingbaseES V8R6 集群环境备库不结束旧事务快照将影响主库的vacuum操作
前言 昨天同事遇到了一个有关vacuum的典型问题. V8R6读写分离集群环境,一主多备. 版本:kingbaseesv008r006c004 问题现象: 主库日常巡检发现日志大量记录: waring ...
- 深入理解HashMap和LinkedHashMap的区别
目录 简介 LinkedHashMap详解 插入 访问 removeEldestEntry 总结 深入理解HashMap和LinkedHashMap的区别 简介 我们知道HashMap的变量顺序是不可 ...
- 赵海鹏:如何进行 OpenHarmony 音频特性架构设计和开发工作
编者按:在 OpenHarmony 生态发展过程中,涌现了大批优秀的代码贡献者,本专题旨在表彰贡献.分享经验,文中内容来自嘉宾访谈,不代表 OpenHarmony 工作委员会观点. 赵海鹏 江苏润和软 ...
- selenium报错:Message: stale element reference: element is not attached to the page document
在使用selenium时,报了一个错误 报错的原因: 所引用的元素已过时,不再依附于当前页面.通常情况下,这是因为页面进行了刷新或跳转 解决方法: 重新定位元素 代码示例: # 旧代码(报错) lis ...
- 用于多视角人群计数的协同通信图卷积网络 Co-Communication Graph Convolutional Network for Multi-View Crowd Counting
Multi-Camara Methods Co-Communication Graph Convolutional Network for Multi-View Crowd Counting 论文ur ...
- WAF 原理入门
WAF 入门 WAF 功能 WAF 全称叫 Web Application Firewall,和传统防火墙的区别是,它是工作在应用层的防火墙,主要对 web 请求/响应进行防护.那么 WAF 有什么功 ...
- Android 开发入门(1)
0x01 准备 (1)概述 安卓(Android)基于 Linux 内核开发的操作系统,由 Google 等领导开发. (2)版本 Android 版本号 API 发布时间 Android 14 - ...