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 ...
随机推荐
- python 下安装pymysql应用
前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持3.x版本. 本文测试python版本:2.7.11. ...
- 理解twisted中的reactor和deferred(一)
Deferred是一个延迟加载对象,这个概念类似于tornado future,是调用异步操作返回的一个对象,其中包括了操作成功后的回调处理,错误后的回调处理. 简单讲,当我们需要执行一个耗时操作,比 ...
- Mf175-用户注册功能-埋点敏捷方案
在不了解埋点系统的情况下,花了五六个小时 帮一位PM朋友做的方案.记录下来.以备后续参考 Mf178-用户注册功能-埋点敏捷方案 版本号 时间 撰写人 描述 V1.0 20190515-10:50:0 ...
- Linux系列:进阶之jdk、X window安装与使用
1.安装x window 分为两步: 1.安装 x window,执行指令yum groupinstall “X Window” 2.安装GNOME Desktop,执行指令yum groupinst ...
- 【重启C++】 关于 【类】
1.什么是抽象类 带有纯虚函数成员的类,称为抽象类.抽象类不能被实例化,因为如果能实例化,调用这个纯虚的成员函数时怎么处理. 2.什么是纯虚函数 纯虚函数是在声明虚函数时被“初始化”为0的函数.声明纯 ...
- HDU-6704 K-th occurrence (后缀自动机father树上倍增建权值线段树合并)
layout: post title: HDU-6704 K-th occurrence (后缀自动机father树上倍增建权值线段树合并) author: "luowentaoaa&quo ...
- flask项目配置
config.py: class Config(object): """项目的配置""" DEBUG = True SECRET_KEY = ...
- 刨根究底字符编码之十——Unicode字符集的编码方式以及码点、码元
Unicode字符集的编码方式以及码点.码元 一.字符编码方式CEF的选择 1. 由于Unicode字符集非常大,有些字符的编号(码点值)需要两个或两个以上字节来表示,而要对这样的编号进行编码,也必须 ...
- (三)创建基于maven的javaFX+springboot项目创建
创建基于maven的javaFx+springboot项目有两种方式,第一种为通过非编码的方式来设计UI集成springboot:第二种为分离用户界面(UI)和后端逻辑集成springboot,其中用 ...
- ES6入门二:默认值与默认值表达式
默认值 默认值表达式 需要注意的是,这种默认值和默认表达式在IE的最新版本中仍然没有得到兼容,只能通过编译转码的方式降级到ES5使用. 一.默认值 在函数声明时可以给形参赋默认值,当调用函数时不传入或 ...