HDU6504 Problem E. Split The Tree【dsu on tree】
Problem E. Split The Tree
Problem Description
You are given a tree with n vertices, numbered from 1 to n. ith vertex has a value wi
We define the weight of a tree as the number of different vertex value in the tree.
If we delete one edge in the tree, the tree will split into two trees. The score is the sum of these two trees’ weights.
We want the know the maximal score we can get if we delete the edge optimally
给出一棵根为\(1\)的树,每个节点有权值,现在断开一条边,把树一分为二,记一棵树的贡献为这棵树里所有节点中不同权值的数量,现在要计算断开边之后两棵树的贡献的和的最大值
现在有一棵子树,为了得到答案,我们只要知道它里面出现过的各个权值有多少个,记原树中各个权值出现的数量是\(cnt[i]\),分开之后的一棵子树的各个权值出现的数量使\(dcnt[i]\),只要出现\(dcnt[i]\ne cnt[i]\ and\ dcnt[i]\ne 0\),对答案的贡献就\(+1\),这个可以\(O(1)\)处理,那么就可以树上启发式合并了
//#pragma GCC optimize("O3")
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
const int MAXN = 1e5+7;
int n,w[MAXN],ret,tmp,cnt[MAXN],dcnt[MAXN],son[MAXN],sz[MAXN],st[MAXN],ed[MAXN],id[MAXN],dfn;
vector<int> G[MAXN];
void dfs(int u){
sz[u] = 1; son[u] = 0;
st[u] = ++dfn; id[dfn] = u;
for(int v : G[u]){
dfs(v);
sz[u] += sz[v];
if(sz[v]>sz[son[u]]) son[u] = v;
}
ed[u] = dfn;
}
void update(int val, int inc){
if(inc==1){
if(!dcnt[val]) tmp++;
dcnt[val]++;
if(dcnt[val]==cnt[val]) tmp--;
}
else{
if(dcnt[val]==cnt[val]) tmp++;
dcnt[val]--;
if(!dcnt[val]) tmp--;
}
}
void search(int u, bool clear){
for(int v : G[u]) if(v!=son[u]) search(v,true);
if(son[u]) search(son[u],false);
for(int v : G[u]) if(v!=son[u]) for(int i = st[v]; i <= ed[v]; i++) update(w[id[i]],1);
update(w[u],1);
ret = max(ret,tmp);
if(clear) for(int i = st[u]; i <= ed[u]; i++) update(w[id[i]],-1);
}
void solve(){
dfn = 0;
for(int i = 1; i <= n; i++) G[i].clear();
for(int i = 2; i <= n; i++){
int par; scanf("%d",&par);
G[par].push_back(i);
}
tmp = 0;
for(int i = 1; i <= n; i++){
scanf("%d",&w[i]);
if(!cnt[w[i]]) tmp++;
cnt[w[i]]++;
}
ret = tmp;
dfs(1);
search(1,true);
printf("%d\n",ret);
for(int i = 1; i <= n; i++) cnt[w[i]]--;
}
int main(){
while(scanf("%d",&n)!=EOF) solve();
return 0;
}
HDU6504 Problem E. Split The Tree【dsu on tree】的更多相关文章
- 【DSU on tree】【CF741D】Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths
Description 给定一棵 \(n\) 个节点的树,每条边上有一个字符,字符集大小 \(22\),求每个节点的子树内最长的简单路径使得路径上的字符经过重排后构成回文串. Limitation \ ...
- CF 570D. Tree Requests [dsu on tree]
传送门 题意: 一棵树,询问某棵子树指定深度的点能否构成回文 当然不用dsu on tree也可以做 dsu on tree的话,维护当前每一个深度每种字母出现次数和字母数,我直接用了二进制.... ...
- HDU1325 Is It A Tree? 【并查集】
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- 2017 Multi-University Training Contest - Team 9 1001&&HDU 6161 Big binary tree【树形dp+hash】
Big binary tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- cdoj32-树上战争(Battle on the tree) 【记忆化搜索】
http://acm.uestc.edu.cn/#/problem/show/32 树上战争(Battle on the tree) Time Limit: 12000/4000MS (Java/Ot ...
- BZOJ2588 Count on a tree 【树上主席树】
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MB Submit: 7577 Solved: 185 ...
- POJ 3237 Tree 【树链剖分】+【线段树】
<题目链接> 题目大意: 给定一棵树,该树带有边权,现在对该树进行三种操作: 一:改变指定编号边的边权: 二:对树上指定路径的边权全部取反: 三:查询树上指定路径的最大边权值. 解题分析: ...
- LC 431. Encode N-ary Tree to Binary Tree 【lock,hard】
Design an algorithm to encode an N-ary tree into a binary tree and decode the binary tree to get the ...
- HDU6430 Problem E. TeaTree【dsu on tree】
Problem E. TeaTree Problem Description Recently, TeaTree acquire new knoledge gcd (Greatest Common D ...
随机推荐
- JavaScript DOM编程艺术(第2版)的简单总结
介绍 JavaScript DOM编程艺术(第2版)主要讲述了 JavaScript.DOM 和 HTML5 的基础知识,着重讲述了 DOM 编程,并通过几个实例演示了具有专业水准的网页开发. 下面介 ...
- WebApi 中请求的 JSON 数据字段作为 POST 参数传入
使用 POST 方式请求 JSON 数据到服务器 WebAPI 接口时需要将 JSON 格式封装成数据模型接收参数.即使参数较少,每个接口仍然需要单独创建模型接收.下面方法实现了将 JSON 参数中的 ...
- Unity 编辑器(移除missing)
移除 Missing(Mono Script) ` private static void FindMissingReferences() { GameObject[] pAllObjects = ( ...
- Loadrunner与kylinPET的能力对比测试--web动态请求
概述 在<性能测试工具选择策略--仿真度对比测评分析报告>一文详细分析了使用相同的web页面,分别使用LoadRunner,Jmeter,kylinTOP工具进行录制脚本并执行得出在静态请 ...
- ctfhub技能树—web前置技能—http协议—响应包源代码
打开靶机环境 查看网页是一个贪吃蛇小游戏 根据提示查看源码 发现flag 至此HTTP协议结束
- windows下如何安装Python、pandas
windows下如何安装Python.pandas 本篇主要涵盖以下三部分内容: Python.Pycharm的安装 使用Pycharm创建.运行Python程序 安装pandas 1.Python. ...
- leetcode刷题录-1395
目录 题目 思考过程 查看别人分享的思路 总结 题目 题目地址:https://leetcode-cn.com/problems/count-number-of-teams/ n 名士兵站成一排.每个 ...
- 【Not BUG】微软Winform窗体中设计上的Bug,会导致程序编译失败?不,这不是BUG!
这不是BUG!!! 原文地址: https://www.cnblogs.com/thanks/p/14302011.html 现在让我们回忆一下原文 原文的操作步骤: 1. 新建一个Window Fo ...
- 翻译 - ASP.NET Core 基本知识 - 通用主机 (Generic Host)
翻译自 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-5.0 ...
- python_mmdt:从0到1--实现简单恶意代码分类器(二)
概述 上篇文章python_mmdt:一种基于敏感哈希生成特征向量的python库(一)我们介绍了一种叫mmdt_hash(敏感哈希)生成方法,并对其中的概念做了基本介绍.本篇,我们重点谈谈mmdt_ ...