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. Linux开启SSH连接

    1. 查看是否安装 openssh-server:yum list installed | grep openssh-server 已安装成功,如下图 2.如果没有任何输出显示表示没有安装openss ...

  2. 可变形卷积系列(三) Deformable Kernels,创意满满的可变形卷积核 | ICLR 2020

    论文提出可变形卷积核(DK)来自适应有效感受域,每次进行卷积操作时都从原卷积中采样出新卷积,是一种新颖的可变形卷积的形式,从实验来看,是之前方法的一种有力的补充.   来源:晓飞的算法工程笔记 公众号 ...

  3. python爬虫爬取科技报告数据,共计40余万条(来自国家科技报告服务系统)

    按学科分类[中图分类] 共计三十余万条科技报告数据 爬取的网址:https://www.nstrs.cn/kjbg/navigation !!! 如果要完整地跑起来代码,需要先看一下我的这篇博客,完成 ...

  4. Scala 传名参数和传值参数

    1 package com.atguigu.chapter04 2 3 object ControlAbstraction { 4 def main(args: Array[String]): Uni ...

  5. Java实现软件设计模式---抽象工厂模式(性别产品等级结构肤色产品族)

    一.题目要求 二.画出对应的类图 三.文件目录结构 四.具体实现代码 Black.java 1 package com.a004; 2 3 public class Black implements ...

  6. .Net接入AzureOpenAI、OpenAI、通义千问、智谱AI、讯飞星火、文心一言大语言模型。

    前言 现在在网上搜索.NET接入大模型的帖子很少,有些官方案例只提供java和python的SDK,所以有了这篇.Net的接入大模型文章,目前仅实现对话模型的调用. 这里仅举例通义千问,其他模型实现可 ...

  7. yml和properties打印SQL日志信息

    1.配置文件里面配置 第一种是properties类型如下 logging.level.com.datayes.mdi.dao.rdb.mommp.**=debug其中 com.datayes.mdi ...

  8. list集合中的实现类LinkedList

    LinkedList: 底层是一个双向链表,方便数据的频繁出入.便于快速插入,删除元素,不太方便进行查询 toArray(): 以正确的顺序(从第一个到最后一个素)返回一个包含此列表中所有元素的数组 ...

  9. #期望,树的直径#51nod 1803 森林直径

    题目 有一棵 \(n\) 个结点的树,按顺序给出树边 \((fa[i],i)\), \(Q\) 次询问查询如果只选取第 \([l,r]\) 条树边,问森林的直径 \(fa[i]\) 的生成方式为 \( ...

  10. OpenAtom OpenHarmony三方库创建发布及安全隐私检测

    OpenAtom OpenHarmony三方库(以下简称"三方库"或"包"),是经过验证可在OpenHarmony系统上可重复使用的软件组件,可帮助开发者快速开 ...