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】的更多相关文章

  1. 【DSU on tree】【CF741D】Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths

    Description 给定一棵 \(n\) 个节点的树,每条边上有一个字符,字符集大小 \(22\),求每个节点的子树内最长的简单路径使得路径上的字符经过重排后构成回文串. Limitation \ ...

  2. CF 570D. Tree Requests [dsu on tree]

    传送门 题意: 一棵树,询问某棵子树指定深度的点能否构成回文 当然不用dsu on tree也可以做 dsu on tree的话,维护当前每一个深度每种字母出现次数和字母数,我直接用了二进制.... ...

  3. HDU1325 Is It A Tree? 【并查集】

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  4. 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 ...

  5. cdoj32-树上战争(Battle on the tree) 【记忆化搜索】

    http://acm.uestc.edu.cn/#/problem/show/32 树上战争(Battle on the tree) Time Limit: 12000/4000MS (Java/Ot ...

  6. BZOJ2588 Count on a tree 【树上主席树】

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MB Submit: 7577  Solved: 185 ...

  7. POJ 3237 Tree 【树链剖分】+【线段树】

    <题目链接> 题目大意: 给定一棵树,该树带有边权,现在对该树进行三种操作: 一:改变指定编号边的边权: 二:对树上指定路径的边权全部取反: 三:查询树上指定路径的最大边权值. 解题分析: ...

  8. 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 ...

  9. HDU6430 Problem E. TeaTree【dsu on tree】

    Problem E. TeaTree Problem Description Recently, TeaTree acquire new knoledge gcd (Greatest Common D ...

随机推荐

  1. Docker一秒进阶

    tar包: 从tar包导入:docker load < xxxx.tar docker run -d -p 8080:80 --name [名字] -v `pwd`:/usr/share/ngi ...

  2. Shiro配置Session检测时Quartz版本冲突

    项目背景: shiro 1.3 + quartz 2.x 2018-9-11 22:20:35补充: 经过测试,本人发现 ,通过实现 org.apache.shiro.session.mgt.Exec ...

  3. 【JavaWeb】Cookie&Session

    Cookie&Session Cookie 什么是 Cookie Cookie 即饼干的意思 Cookie 是服务器通知客户端保存键值对的一种技术 客户端有了 Cookie 后,每次请求都发送 ...

  4. 安装weblogic 11g

    参考 https://blog.csdn.net/z69183787/article/details/38401013 https://blog.csdn.net/wjf8882300/article ...

  5. Java并发包源码学习系列:ReentrantReadWriteLock读写锁解析

    目录 ReadWriteLock读写锁概述 读写锁案例 ReentrantReadWriteLock架构总览 Sync重要字段及内部类表示 写锁的获取 void lock() boolean writ ...

  6. Lniux 入门:03 用户及文件权限管理

    1.1 实验内容 Linux 中创建.删除用户,及用户组等操作. Linux 中的文件权限设置. 1.2 实验知识点 Linux 用户管理 Linux 权限管理 通过第一节课程的学习,你应该已经知道, ...

  7. django 中连接mysql数据库的操作步骤

    django中连接mysql数据库的操作步骤: 1 settings配置文件中 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mys ...

  8. JavaWeb三大框架基础架构——CRUD的基础功能搭建

    @ 目录 介绍 注意 applicationContext.xml mybatis-config.xml web.xml 结束语 介绍 项目前端采用了bootstrap,后端是ssm三大框架 注意 这 ...

  9. 解决 win10 无法安装VS2019,visual studio installer下载进度始终为0

    解决 win10 无法安装VS2019,visual studio installer下载进度始终为0 目录 解决 win10 无法安装VS2019,visual studio installer下载 ...

  10. 接口 Interfaces

    Interfaces - zope.interface 5.0.2.dev0 documentation https://zopeinterface.readthedocs.io/en/latest/ ...