HDU 6035 - Colorful Tree | 2017 Multi-University Training Contest 1
/*
HDU 6035 - Colorful Tree [ DFS,分块 ]
题意:
n个节点的树,每个节点有一种颜色(1~n),一条路径的权值是这条路上不同的颜色的数量,问所有路径(n*(n-1)/2条) 权值之和是多少?
分析:
考虑单种颜色,这种颜色的贡献是 至少经过一次这种颜色的路径数 = 总路径数(n*(n-1)/2) - 没有经过这种颜色的路径数
求没有经过这种颜色的路径数,即这种颜色的点将整棵树分块,每个分块中的总路径数
*/
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int N = 200005;
struct Edge {
int to, next;
}edge[N<<1];
int tot, head[N];
void init() {
memset(head, -1, sizeof(head));
tot = 0;
}
void addedge(int u, int v) {
edge[tot].to = v; edge[tot].next = head[u];
head[u] = tot++;
}
int n;
int c[N], last[N], rem[N], cut[N];
LL ans;
LL sum2(LL x) {
return x*(x-1)/2;
}
int dfs(int u, int pre)
{
int su = 1, fa = last[c[u]];
last[c[u]] = u;
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (v == pre) continue;
cut[u] = 0;
int sv = dfs(v, u);
su += sv;
ans -= sum2(sv-cut[u]);
}
(fa ? cut[fa] : rem[c[u]]) += su;
last[c[u]] = fa;
return su;
}
int main()
{
int tt = 0;
while (~scanf("%d", &n))
{
init();
for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
for (int i = 1; i < n; i++)
{
int x, y; scanf("%d%d", &x, &y);
addedge(x, y); addedge(y, x);
}
memset(last, 0, sizeof(last));
memset(cut, 0, sizeof(cut));
memset(rem, 0, sizeof(rem));
ans = n*sum2(n);
dfs(1, 1);
for (int i = 1; i <= n; i++)
ans -= sum2(n-rem[i]);
printf("Case #%d: %lld\n", ++tt, ans);
}
}
//----------------------------------------------------------------------
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int N = 200005;
vector<int> c[N], G[N];
int n;
int L[N], R[N], s[N], f[N];
void dfs(int u, int pre, int&& ncnt)
{
f[u] = pre;
L[u] = ++ncnt;
s[u] = 1;
for (auto& v : G[u])
{
if (v == pre) continue;
dfs(v, u, move(ncnt));
s[u] += s[v];
}
R[u] = ncnt;
}
bool cmp(int a, int b) {
return L[a] < L[b];
}
int main()
{
int tt = 0;
while (~scanf("%d", &n))
{
for (int i = 0; i <= n; i++) c[i].clear(), G[i].clear();
for (int i = 1; i <= n; i++)
{
int x; scanf("%d", &x);
c[x].push_back(i);
}
for (int i = 1; i < n; i++)
{
int x, y; scanf("%d%d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
G[0].push_back(1);
dfs(0, 0, 0);
LL ans = (LL)n * n * (n-1)/2;
for (int i = 1; i <= n; i++)
{
if (c[i].empty()) {
ans -= (LL)n*(n-1)/2;
continue;
}
c[i].push_back(0);
sort(c[i].begin(), c[i].end(), cmp);
for (auto& x : c[i])
for (auto& y : G[x])
{
if (y == f[x]) continue;
int size = s[y];
int k = L[y];
while (1)
{
L[n+1] = k;
auto it = lower_bound(c[i].begin(), c[i].end(), n+1, cmp);
if (it == c[i].end() || L[*it] > R[y]) break;
size -= s[*it];
k = R[*it]+1;
}
ans -= (LL)size * (size-1)/2;
}
}
printf("Case #%d: %lld\n", ++tt, ans);
}
}
HDU 6035 - Colorful Tree | 2017 Multi-University Training Contest 1的更多相关文章
- hdu 6035:Colorful Tree (2017 多校第一场 1003) 【树形dp】
题目链接 单独考虑每一种颜色,答案就是对于每种颜色至少经过一次这种的路径条数之和.反过来思考只需要求有多少条路径没有经过这种颜色即可. 具体实现过程比较复杂,很神奇的一个树形dp,下面给出一个含较详细 ...
- 2017 Multi-University Training Contest - Team 1 1003&&HDU 6035 Colorful Tree【树形dp】
Colorful Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- HDU 6035 Colorful Tree(补集思想+树形DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6035 [题目大意] 给出一颗树,一条路径的价值为其上点权的种类数,求路径总价值 [题解] 单独考虑 ...
- HDU 6035 Colorful Tree (树形DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6035 [题目大意] 给出一颗树,一条路径的价值为其上点权的种类数,求路径总价值 [题解] 我们计算 ...
- 2017ACM暑期多校联合训练 - Team 1 1003 HDU 6035 Colorful Tree (dfs)
题目链接 Problem Description There is a tree with n nodes, each of which has a type of color represented ...
- HDU 6035 Colorful Tree(dfs)
题意:一棵有n个点的树,树上每个点都有颜色c[i],定义每条路径的值为这条路径上经过的不同颜色数量和.求所有路径的值的和. 可以把问题转化为对每种颜色有多少条不同的路径至少经过这种颜色的点,然后加和. ...
- hdu 6035 Colorful Tree(虚树)
考虑到树上操作:首先题目要我们求每条路径上出现不同颜色的数量,并把所有加起来得到答案:我们知道俩俩点之间会形成一条路径,所以我们可以知道每个样例的总的路径的数目为:n*(n-1)/2: 这样单单的求, ...
- HDU 6170 - Two strings | 2017 ZJUT Multi-University Training 9
/* HDU 6170 - Two strings [ DP ] | 2017 ZJUT Multi-University Training 9 题意: 定义*可以匹配任意长度,.可以匹配任意字符,问 ...
- hdu 6301 Distinct Values (2018 Multi-University Training Contest 1 1004)
Distinct Values Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
随机推荐
- Java 常提到的自然序(Natural Ordering)
Natural Ordering常在容器中被提到,和迭代器一起出现. 在Comparable接口的API规范中找到了描述. (https://docs.oracle.com/javase/8/docs ...
- 在Window Server 2016中使用Web Deploy方式发布.NET Web应用
1.在IIS里面点击获取新的Web平台组件 2.下载Web平台组件并安装 3.在其中搜索Web Deploy,找到3.5版本,并安装 4.继续搜索Web Deploy 3.6版本,并安装 安装好之后, ...
- 15-Perl 格式化输出
1.Perl 格式化输出Perl 是一个非常强大的文本数据处理语言.Perl 中可以使用 format 来定义一个模板,然后使用 write 按指定模板输出数据.Perl 格式化定义语法格式如下:fo ...
- Eclipse连接数据库报错Local variable passwd defined in an enclosing scope must be final or effectively final
其实原因很简单,就是翻译的结果 匿名内部类和局部内部类只能引用外部的fianl变量 把变量变成fianl就行了 第一次知道啊 记小本本.......
- ASP.NET_正则表达式_匹配HTML中的一行或多行
一.匹配数字串/flash/([0-9]+).htm 二.匹配不含双引号的字符串<p class=\"w490\">([^\"]+)</p> 三. ...
- win DLL 笔记
DLL 头文件: #ifdef DLL_API #else #define DLL 导出类 class DLL_API point { public: void aaa() { } } 导出类中函数 ...
- scrapy操作指南
Scrapy安装:(scrapy依赖包过多推荐使用下面的方法) 先安装Anaconda,然后 运行conda install Scrapy 创建scrapy项目: 1,scrapy startproj ...
- css 单位
CSS 有几个不同的单位用于表示长度. 一些设置 CSS 长度的属性有 width, margin, padding, font-size, border-width, 等. 长度有一个数字和单位组成 ...
- Sql Server 导出数据库表结构的SQL查询语句
--导出数据库所有表 SELECT 表名 Then D.name Else '' End, 表说明 Then isnull(F.value,'') Else '' End, 字段序号 = A.colo ...
- vue封装swiper
参考:https://github.com/surmon-china/vue-awesome-swiper npm install vue-awesome-swiper --save 全局引入 imp ...