[Split The Tree][dfs序+树状数组求区间数的种数]
Split The Tree
时间限制: 1 Sec 内存限制: 128 MB
提交: 46 解决: 11
[提交] [状态] [讨论版] [命题人:admin]
题目描述
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.
输入
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
输出
样例输入
3
1 1
1 2 2
样例输出
3
题意:给一棵树,每个节点都有权值,删除一条边会变成两棵树,每棵树的价值是树上不同权值的个数,求max(两棵树的价值和)
题解:dfs序可以将树上的问题转化为易于操作的线性区间问题(子树是dfs序中连续的区间)。这题通过dfs序转化为求区间数字的种数,通过树状数组求解。注意求区间数的种数的方法:通过vector存储区间左端点,从总区间的起始点开始往后遍历,不断update树状数组中当前点代表权值的位置(即把当前点权值的上一个位置的值-1,把当前位置+1),并且用树状数组计算以当前点为终点的区间种数和
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
struct edge{
int x;
int y;
int nex;
}e[];
struct pot{
int pre;
int id;
};
vector<struct pot>g[];
int n,cnt,tot,head[],q[],w[],c[],dfn[],r[],vis[],ans[];
void adde(int x1,int y1){
e[cnt].x=x1;
e[cnt].y=y1;
e[cnt].nex=head[x1];
head[x1]=cnt++;
}
int lowbit(int x){
return x&(-x);
}
void update(int x,int y,int n){
for(int i=x;i<=n;i+=lowbit(i))
c[i] += y;
}
int query(int x){
int ak=;
for(int i=x;i>;i-=lowbit(i)){
ak+=c[i];
}
return ak;
}
void dfs(int u,int fa){
w[++tot]=u;
w[tot+n]=u;
dfn[u]=tot;
for(int i=head[u];i!=-;i=e[i].nex){
int v=e[i].y;
if(v==fa)continue;
if(dfn[v])continue;
dfs(v,u);
}
r[u]=tot;
}
int main(){
scanf("%d",&n);
memset(head,-,sizeof(head));
for(int i=;i<=n;i++){
int a;
scanf("%d",&a);
adde(a,i);
adde(i,a);
}
for(int i=;i<=n;i++){
scanf("%d",&q[i]);
}
dfs(,-);
for(int i=;i<cnt;i+=){
int u=e[i].x;
int v=e[i].y;
int L=dfn[u];
int R=r[u];
int L1=dfn[v];
int R1=r[v];
struct pot aaa,bbb;
if(R!=n){
aaa.id=i;
aaa.pre=L;
bbb.id=i;
bbb.pre=R+;
g[R].push_back(aaa);
g[L-+n].push_back(bbb);
}
else{
aaa.id=i;
aaa.pre=L1;
bbb.id=i;
bbb.pre=R1+;
g[R1].push_back(aaa);
g[L1-+n].push_back(bbb);
}
}
for(int i=;i<=n*;i++){
if(vis[q[w[i]]]){
update(vis[q[w[i]]],-,n*);
}
vis[q[w[i]]]=i;
update(vis[q[w[i]]],,n*);
for(int j=;j<g[i].size();j++){
struct pot ccc=g[i][j];
ans[ccc.id]+=query(i)-query(ccc.pre-);
}
}
int ans0=;
for(int i=;i<cnt;i+=){
ans0=max(ans0,ans[i]);
}
cout<<ans0<<endl;
return ;
}
[Split The Tree][dfs序+树状数组求区间数的种数]的更多相关文章
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- [poj3321]Apple Tree(dfs序+树状数组)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26762 Accepted: 7947 Descr ...
- POJ3321Apple Tree Dfs序 树状数组
出自——博客园-zhouzhendong ~去博客园看该题解~ 题目 POJ3321 Apple Tree 题意概括 有一颗01树,以结点1为树根,一开始所有的结点权值都是1,有两种操作: 1.改变其 ...
- POJ 3321 Apple Tree DFS序 + 树状数组
多次修改一棵树节点的值,或者询问当前这个节点的子树所有节点权值总和. 首先预处理出DFS序L[i]和R[i] 把问题转化为区间查询总和问题.单点修改,区间查询,树状数组即可. 注意修改的时候也要按照d ...
- Codeforces Round #381 (Div. 2) D. Alyona and a tree dfs序+树状数组
D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...
- POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)
http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...
- HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca
Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...
随机推荐
- P3601 签到题
思路 注意到求的qiandao(x)就是\(x-\phi(x)\) 但是\(l,r\le 10^{12}\),所以不能直接杜教筛 但是\(r-l\le 10^{6}\),所以可以先筛出1e6(\(\s ...
- 【HNOI 2018】寻宝游戏
Problem Description 某大学每年都会有一次 \(Mystery\ Hunt\) 的活动,玩家需要根据设置的线索解谜,找到宝藏的位置,前一年获胜的队伍可以获得这一年出题的机会. 作为新 ...
- Maven的pom配置文件
1.1 Maven的pom配置文件 1.1.1 pom文件内容和作用 Pom.xml文件用来设置项目的项目依赖.插件.项目版本等信息,其中必须的是xml的根元 ...
- Matlab:线性热传导(抛物线方程)问题
函数文件1:real_fun.m function f=real_fun(x0,t0) f=(x0-x0^2)*exp(-t0); 函数文件2:fun.m function f=fun(x0,t0) ...
- 颜色(color)透明
颜色写法: 1.英文单词 backgroud-color:red: 2. #fff background-color:#fff; 3.#fefefe background-color:fefefe; ...
- CCF关于NOIP复赛网络申诉问题的公告
CCF NOI竞赛委员会将NOIP复赛网络申诉的有关情况公告如下.凡属于以下情况的申诉,均不予受理 1.非公示期限内提出的申诉,不予受理: 2.与个人名次.他人成绩和他人名次有关的申诉,不予受理: 3 ...
- 用python做一个烟花show
烟花效果如图(截了几个时刻的静态图): 源代码如下: # -*- coding: utf-8 -*- # Nola import tkinter as tk from PIL import Image ...
- flask-session 在redis中存储session
依赖: Flask Flask-Session redis import os from flask import Flask, session, request from flask_session ...
- vscode 创建.net core mvc
cd 进一个文件夹 1,创建一个sln 工程文件 [ dotnet new sln -n Demo1 ] 2,创建一个mvc项目 [ dotnet new mvc -n Demo1.Web ] 3, ...
- oracle数据库连接 ORA-12638:身份证明检索失败
连数据库的时候突然报了一个这个 查找各种办法,发现自己从10g换成了11g,不过这个没有什么关系,跟oracle的安全设置有关系, 首先从开始菜单找到Net Manager 打开,选择本地,概要文件, ...