Split The Tree

时间限制: 1 Sec  内存限制: 128 MB
提交: 46  解决: 11
[提交] [状态] [讨论版] [命题人:admin]

题目描述

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.

输入

Input is given from Standard Input in the following format:
n
p2 p3  . . . pn
w1 w2  . . . wn
Constraints
2 ≤ n ≤ 100000 ,1 ≤ pi < i
1 ≤ wi ≤ 100000(1 ≤ i ≤ n), and they are integers
pi means there is a edge between pi and i

输出

Print one number denotes the maximal score.

样例输入

3
1 1
1 2 2

样例输出

3

思路:

把序列延长一倍,每段子树的两边的区间连起来,将问题转化成维护区间中不同数字个数的最大值。

#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
vector<pair<int,int> >vi[maxn];
int tree[maxn];
inline int lowbit(int x){return x&-x;}
void update(int x,int val) {for (int i=x; i<maxn; i+=lowbit(i)) tree[i]+=val;}
int Query(int x){int res=0;for (int i=x; i; i-=lowbit(i)) res+=tree[i]; return res;}
int st[maxn],dfn[maxn],ed[maxn],tot=0;
struct Edge{int u,v,next;}e[maxn];
int head[maxn],cnt=0;
inline void add(int u,int v){e[cnt]=Edge{u,v,head[u]};head[u]=cnt++;}
int n,vis[maxn],w[maxn];
void dfs(int x,int fa=0){st[x]=++tot;dfn[tot]=x;dfn[n+tot]=x;for (int v,i=head[x]; ~i; i=e[i].next){v=e[i].v;if(v==fa) continue;dfs(v,x);}ed[x]=tot;
}
int ans[maxn],res=0;
int main(){memset(head,-1,sizeof(head));scanf("%d",&n);for (int p,i=2; i<=n; i++) scanf("%d",&p),add(p,i),add(i,p);for (int i=1; i<=n; i++) scanf("%d",&w[i]);dfs(1);vi[ed[1]].push_back({st[1],1});for (int i=2; i<=n; i++){vi[ed[i]].push_back({st[i],i});vi[st[i]-1+n].push_back({ed[i]+1,i});}for (int i=1; i<=n+n; i++){if(vis[w[dfn[i]]]) update(vis[w[dfn[i]]],-1);update(i,1);vis[w[dfn[i]]]=i;for (auto u:vi[i])ans[u.second]+=Query(i)-Query(u.first-1);}for (int i=1; i<=n; i++) res=max(res,ans[i]);printf("%d\n",res);return 0;
}

View Code

转载于:https://www.cnblogs.com/acerkoo/p/9593896.html

Split The Tree(dfs序+树状数组)相关推荐

  1. [Split The Tree][dfs序+树状数组求区间数的种数]

    Split The Tree 时间限制: 1 Sec  内存限制: 128 MB 提交: 46  解决: 11 [提交] [状态] [讨论版] [命题人:admin] 题目描述 You are giv ...

  2. Apple Tree(dfs序+树状数组)

    题目(传送门poj3321) Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 40714 Accepted: ...

  3. poj 3321 Apple Tree(dfs序+树状数组求和模型)

    题目链接:http://poj.org/problem?id=3321 解题思路: 先dfs求出序列,将子树转化到dfs序列的区间内,接下来就是简单的树状数组求和模型了.水题. #include< ...

  4. 【dfs序+树状数组】多次更新+求结点子树和操作,牛客小白月赛24 I题 求和

    前置知识点 dfs遍历 树状数组/线段树知识 链接 I题 求和. 题意 已知有 n 个节点,有 n−1 条边,形成一个树的结构. 给定一个根节点 k,每个节点都有一个权值,节点i的权值为 vi 给 m ...

  5. 计蒜客(青出于蓝胜于蓝) dfs序+树状数组

    武当派一共有 n 人,门派内 n 人按照武功高低进行排名,武功最高的人排名第 1,次高的人排名第 2,... 武功最低的人排名 第 n.现在我们用武功的排名来给每个人标号,除了祖师爷,每个人都有一个师 ...

  6. HDU - 5788 Level Up(主席树+dfs序+树状数组)

    题目链接:点击查看 题目大意:给出一棵有向树,每个节点都有一个初始的权值 a[ i ] ,和一个通过计算得到的权值 mid[ i ] ,mid 数组的计算方法如下:mid[ u ] 为结点 u 及其子 ...

  7. 【POJ - 3321】 Apple Tree(dfs序 + 线段树维护 或 dfs序 + 树状数组维护)

    题干: There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the t ...

  8. 【POJ 3321】Apple Tree(树的dfs序+树状数组)

    传送门 Solution: 我们只需要采用和树链剖分近似的思想--把整个树的dfs序整理出来,排成线型. 这样一个节点的子树肯定是连续的一段,于是乎就可以用树状数组维护单点修改+区间查询的任务了. # ...

  9. 【BZOJ2434】[NOI2011]阿狸的打字机 AC自动机+DFS序+树状数组

    [BZOJ2434][NOI2011]阿狸的打字机 Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P ...

  10. 【bzoj2434】[Noi2011]阿狸的打字机 AC自动机+Dfs序+树状数组

    题目描述 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母. 经阿狸研究发现,这个打字机是这样工作的: l 输入小 ...

最新文章

  1. RxJava 内置多种用于调度的线程类型
  2. Tomcat服务器的常用配置
  3. boost::type_traits模块用法的一些示例
  4. 最近面试一些厂的面经整理(阿里,腾讯,字节等)
  5. Scala Array sum recursive call
  6. Angular Remove me测试应用的工作原理
  7. 从HMM到MEMM再到CRF
  8. 不同浏览器input file样式不一样
  9. php array 删除末尾,PHP array_pop():删除数组末尾的元素
  10. android9 system_server 访问sd卡_Leez学院丨手把手教你栗子板Android9固件编译
  11. nginx、tomcat负载均衡
  12. C语言的printf输出格式控制
  13. 新手平面设计师如何在网上接单赚钱?
  14. conda报错-Collecting package metadata (current_repodata.json): failed
  15. html+js画一颗心形,用SVG和Vanilla JS框架创建一个“星形变心形”的动画效果
  16. 函数调用之特殊三位数
  17. 给折腾ramdisk的朋友们一点建议
  18. App测试中有哪些常见的性能测试指标?出具App测试报告的软件测试机构推荐
  19. 一加7t人脸识别_一加7T新机设计图发布 这款手机的外观设计如何
  20. Unity内实现MMD

热门文章

  1. 《ZigBee开发笔记》第四部分 提高篇-第9章 CC2530基于GenericApp无线收发实验
  2. 复旦微电子CPU卡发卡流程
  3. Element-plus Notification 自定义动态图标
  4. 保姆级教程 | 表格自动行合并实现
  5. java日历记事本界面_GitHub - wusongxue/DuangCalender: 简易日历记事本(java+android端)...
  6. 365天英语口语学习_03,居家住房
  7. 计算机二级报名付款页面弹不出来怎么办,12123支付页面弹不出来怎么办
  8. Steam VR设备连接问题,求解答!
  9. ESP32使用Arduino驱动180 360度舵机
  10. NTFS学习笔记(1):DBR