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题意:给一棵树,每个节点都有权值,删除一条边会变成两棵树,每棵树的价值是树上不同权值的个数,求max(两棵树的价值和)题解:dfs序可以将树上的问题转化为易于操作的线性区间问题(子树是dfs序中连续的区间)。这题通过dfs序转化为求区间数字的种数,通过树状数组求解。注意求区间数的种数的方法:通过vector存储区间左端点,从总区间的起始点开始往后遍历,不断update树状数组中当前点代表权值的位置(即把当前点权值的上一个位置的值-1,把当前位置+1),并且用树状数组计算以当前点为终点的区间种数和

  1 #include<iostream>
  2 #include<cstring>
  3 #include<algorithm>
  4 #include<cmath>
  5 #include<cstdio>
  6 #include<vector>
  7 #include<queue>
  8 using namespace std;
  9 typedef long long ll;
 10 struct edge{
 11     int x;
 12     int y;
 13     int nex;
 14 }e[200005];
 15 struct pot{
 16     int pre;
 17     int id;
 18 };
 19 vector<struct pot>g[200005];
 20 int n,cnt,tot,head[100005],q[100005],w[200005],c[200005],dfn[100005],r[100005],vis[100005],ans[200005];
 21 void adde(int x1,int y1){
 22     e[cnt].x=x1;
 23     e[cnt].y=y1;
 24     e[cnt].nex=head[x1];
 25     head[x1]=cnt++;
 26 }
 27 int lowbit(int x){
 28     return x&(-x);
 29 }
 30 void update(int x,int y,int n){
 31     for(int i=x;i<=n;i+=lowbit(i))
 32         c[i] += y;
 33 }
 34 int query(int x){
 35     int ak=0;
 36     for(int i=x;i>0;i-=lowbit(i)){
 37         ak+=c[i];
 38     }
 39     return ak;
 40 }
 41 void dfs(int u,int fa){
 42     w[++tot]=u;
 43     w[tot+n]=u;
 44     dfn[u]=tot;
 45     for(int i=head[u];i!=-1;i=e[i].nex){
 46         int v=e[i].y;
 47         if(v==fa)continue;
 48         if(dfn[v])continue;
 49         dfs(v,u);
 50     }
 51     r[u]=tot;
 52 }
 53 int main(){
 54     scanf("%d",&n);
 55     memset(head,-1,sizeof(head));
 56     for(int i=2;i<=n;i++){
 57         int a;
 58         scanf("%d",&a);
 59         adde(a,i);
 60         adde(i,a);
 61     }
 62     for(int i=1;i<=n;i++){
 63         scanf("%d",&q[i]);
 64     }
 65     dfs(1,-1);
 66     for(int i=0;i<cnt;i+=2){
 67         int u=e[i].x;
 68         int v=e[i].y;
 69         int L=dfn[u];
 70         int R=r[u];
 71         int L1=dfn[v];
 72         int R1=r[v];
 73         struct pot aaa,bbb;
 74         if(R!=n){
 75             aaa.id=i;
 76             aaa.pre=L;
 77             bbb.id=i;
 78             bbb.pre=R+1;
 79             g[R].push_back(aaa);
 80             g[L-1+n].push_back(bbb);
 81         }
 82         else{
 83             aaa.id=i;
 84             aaa.pre=L1;
 85             bbb.id=i;
 86             bbb.pre=R1+1;
 87             g[R1].push_back(aaa);
 88             g[L1-1+n].push_back(bbb);
 89         }
 90     }
 91     for(int i=1;i<=n*2;i++){
 92         if(vis[q[w[i]]]){
 93             update(vis[q[w[i]]],-1,n*2);
 94         }
 95         vis[q[w[i]]]=i;
 96         update(vis[q[w[i]]],1,n*2);
 97         for(int j=0;j<g[i].size();j++){
 98             struct pot ccc=g[i][j];
 99             ans[ccc.id]+=query(i)-query(ccc.pre-1);
100         }
101     }
102     int ans0=0;
103     for(int i=0;i<cnt;i+=2){
104         ans0=max(ans0,ans[i]);
105     }
106     cout<<ans0<<endl;
107     return 0;
108 }

View Code

 

转载于:https://www.cnblogs.com/MekakuCityActor/p/10639162.html

[Split The Tree][dfs序+树状数组求区间数的种数]相关推荐

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

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

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

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

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

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

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

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

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

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

  6. 树状数组求区间和模板 区间可修改 参考题目:牛客小白月赛 I 区间

    从前有个东西叫树状数组,它可以轻易实现一些简单的序列操作,比如单点修改,区间求和;区间修改,单点求值等. 但是我们经常需要更高级的操作,比如区间修改区间查询.这时候树状数组就不起作用了,只能选择写一个 ...

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

最新文章

  1. feign调用走不走网关全局拦截_feign服务端出异常客户端处理的方法
  2. 微酒店微信界面返回信息有html源码
  3. messenger支持查找附近的人功能吗_最新的 macOS Catalina 正式版,值得更新吗?
  4. 用Curl测试POST
  5. CSS盒子模型的使用及其注意事项——响应式Web系列学习笔记
  6. 移动应用安全初创企业Seworks获820万美元A轮融资
  7. 【数据结构】——冒泡排序、插入排序、选择排序
  8. 【vue开发】vue插件的install方法
  9. 骁龙855加持!一加5G原型机将亮相MWC2019:价格却不太友好
  10. Windows10/Servers 2016的TrustedInstaller权限获取(及乱改System后救砖
  11. [转] Java之ACM速成
  12. 在Python 3中使用深度森林(Deep Forest)进行分类
  13. 2017年经典hadoop体系课程-徐培成-专题视频课程
  14. 何加盐深度揭秘:我是怎么找资料的?
  15. 用命令从FTP服务器下载文件
  16. 1KB文件夹快捷方式病毒清除(转)
  17. hive的一些常用命令
  18. 2022 年项目经理薪酬趋势:找工作就这么谈
  19. Oracle启用、禁用触发器
  20. 163net邮箱,对商务办公人士有哪些帮助?

热门文章

  1. JS笔试题(3)【阿里巴巴】
  2. 获得理想薪资的11大技巧
  3. 阿里华为员工跳槽到微软,被鄙视了:他们工资低,整天996,把气氛搞得很差!...
  4. 苹果cmsv10仿奇热影院响应式简约好看的免费模板
  5. 车载以太网 | 测试之实锤-1000BASE-T1物理层PMA测试实践
  6. H3CSE路由-路由引入
  7. 易优cms 后台网址是多少 Eyoucms快速入门
  8. java 导出 excel “String literals in formulas can‘t be bigger than 255 characters ASCII“ 问题
  9. vue的路由组件的引入以及路由组件懒加载和router-link
  10. 最新BAT大厂面试者整理的Android面试题目模板,在线面试指南