Codeforces 1059E. Split the Tree
题目:http://codeforces.com/problemset/problem/1059/E
用倍增可以在nlog内求出每个节点占用一个sequence 时最远可以向父节点延伸到的节点,对每个节点作为sequence 的最后一个元素向上延伸时,将节点的父节点属性合并(类似于并查集的操作),
存在优先队列里保证每次先操作dep最大的节点
#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<vector>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<fstream>
#include<cstdlib>
#include<ctime>
#include<list>
#include<climits>
#include<bitset>
#include<random>
#include <ctime>
#include <cassert>
#include <complex>
#include <cstring>
#include <chrono>
using namespace std;
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("input.in", "r", stdin);freopen("output.in", "w", stdout);
#define left asfdasdasdfasdfsdfasfsdfasfdas1
#define set asfdasdasdfasdfsdfasfsdfasfdas2
#define tan asfdasdasdfasdfasfdfasfsdfasfdas
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
typedef unsigned int un;
const int desll[][]={{,},{,-},{,},{-,}};
const int mod=1e9+;
const int maxn=1e5+;
const int maxm=1e5;
const double eps=1e-;
const int csize=;
int n,k,m,ar[maxn];
int f[maxn],tan[maxn][],v[maxn],in[maxn],mark;
ll ss[maxn][];
bool ma[maxn],que[maxn];
priority_queue<pair<int,int> > qu;
vector<int> ve[maxn];
int dep[maxn];
void dfs(int u,int pre)
{
if(pre>=)dep[u]=dep[pre]+;
for(int i=;i<ve[u].size();i++){
int v=ve[u][i];
if(v==pre)continue;
dfs(v,u);
}
}
int main()
{
int l;
ll s,mx=;f[]=;
scanf("%d%d%I64d",&n,&l,&s);
memset(in,,sizeof(in));
memset(tan,,sizeof(tan));
memset(ma,,sizeof(ma));
memset(que,,sizeof(que));
for(int i=;i<=n;i++)scanf("%d",&ar[i]),mx=max(mx,1LL*ar[i]);
for(int i=;i<=n;i++)scanf("%d",&f[i]),in[f[i]]++,ve[f[i]].push_back(i);
dep[]=;
dfs(,-);
if(mx>s){
printf("-1\n");
return ;
}
for(int i=;i<=n;i++){
tan[i][]=f[i];
ss[i][]=ar[f[i]];
}
for(int j=;j<;j++){//倍增预处理
for(int i=;i<=n;i++){
if(i + (<<j) >n)break;
tan[i][j]=tan[tan[i][j-]][j-];
ss[i][j] = ss[i][j-]+ss[tan[i][j-]][j-];
}
}
for(int i=;i<=n;i++){
ll lmid=l-,smid=s-ar[i];
int ins=,x=i;
while(ins>=){
if(tan[x][ins]==)ins--;
else if((<<ins) > lmid)ins--;
else if(ss[x][ins]<=smid){
smid-=ss[x][ins];
lmid -= (<<ins);
x=tan[x][ins];
}
else ins--;
}
v[i]=x;//v[i]代表i节点作为sequence尾节点时最远可以向上延伸到的节点
}
for(int i=;i<=n;i++){
if(v[i]==)exit();
}
for(int i=;i<=n;i++){
if(in[i]==){
qu.push(make_pair(dep[i],i));
que[i]=;
}
}
int ans=;
while(qu.size()){
int u=qu.top().second;qu.pop();
if(ma[u])continue;//如果已经标记过,代表有其他节点可以延伸到此节点,跳过即可
mark=v[u];
int mid=f[u],pre=u;
while(dep[mid]>dep[mark]){//对ma数组进行标记,合并f数组,类似于并查集的操作
ma[pre]=;
f[pre]=mark;
pre=mid;
mid=f[mid];
}
ma[pre]=;
ma[mark]=;
if(pre!=mark)f[pre]=mark;
ans++;
//cout<<mark<<" "<<f[mark]<<" "<<ma[f[mark]]<<" "<<que[f[mark]]<<endl;
if(f[mark]> && !ma[f[mark]] && !que[f[mark]]){
qu.push(make_pair(dep[f[mark]],f[mark]));
que[f[mark]]=;
}
}
printf("%d\n",ans); return ;
}
Codeforces 1059E. Split the Tree的更多相关文章
- Codeforces 461B. Appleman and Tree[树形DP 方案数]
B. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces 461B Appleman and Tree(木dp)
题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...
- [Split The Tree][dfs序+树状数组求区间数的种数]
Split The Tree 时间限制: 1 Sec 内存限制: 128 MB提交: 46 解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 You are given ...
- Codeforces 1129 E.Legendary Tree
Codeforces 1129 E.Legendary Tree 解题思路: 这题好厉害,我来复读一下官方题解,顺便补充几句. 首先,可以通过询问 \(n-1\) 次 \((S=\{1\},T=\{ ...
- Codeforces 280C Game on tree【概率DP】
Codeforces 280C Game on tree LINK 题目大意:给你一棵树,1号节点是根,每次等概率选择没有被染黑的一个节点染黑其所有子树中的节点,问染黑所有节点的期望次数 #inclu ...
- Split The Tree
Split The Tree 时间限制: 1 Sec 内存限制: 128 MB 题目描述 You are given a tree with n vertices, numbered from 1 ...
- [CodeForces1059E] Split the Tree
树形DP. 用倍增处理出来每个点往上能延伸出去的最远路径,nlogn 对于每个节点,如果它能被后代使用过的点覆盖,就直接覆盖,这个点就不使用,否则就ans++,让传的Max改成dp[x] #inclu ...
- Codeforces A. Game on Tree(期望dfs)
题目描述: Game on Tree time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- 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 ...
随机推荐
- 2017南开ACM校赛(网络赛) 民间题解
orz 首先说一下这个只是民间题解,可能会有很多错误 程序还没有评测,所以可能存在问题 C题比赛的时候没想到..后来发现是个模板题,所以没有代码 希望这份题解能对读者有所启发吧... A题 直接倒序枚 ...
- Hadoop 学习之MapReduce
MapReduce充分利用了分而治之,主要就是将一个数据量比较大的作业拆分为多个小作业的框架,而用户需要做的就是决定拆成多少份,以及定义作业本身,用户所要做的操作少了又少,真是Very Good! 一 ...
- oracle与mysql与sqlserver的分页
假设当前是第PageNo页,每页有PageSize条记录,现在分别用Mysql.Oracle和SQL Server分页查询student表. 1.Mysql的分页查询: 1 SELECT 2 * 3 ...
- Linux下rsync 安装与配置
1.什么是rsync Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.Rsync使用所谓的“Rsync算法”来使本地和远 程两个 ...
- white-space——处理元素内的空白
定义和用法 white-space 属性设置如何处理元素内的空白.这个属性声明建立布局过程中如何处理元素中的空白符.值 pre-wrap 和 pre-line 是 CSS 2.1 中新增的. 默认 ...
- Python爬虫学习笔记之爬取新浪微博
import requests from urllib.parse import urlencode from pyquery import PyQuery as pq from pymongo im ...
- java封装的使用
一:前言 其实以前我们来学习java特性的时候,对于封装好想觉得没什么用处,至少我那个时候的感觉(不知道是不是我学的太浅薄了~),现在由于项目从零开始做得,在做得过程中我感觉到原来封装是这样用的. 二 ...
- maven在add dependecy时搜索不出jar包的解决办法
一:前言 其实我一直都很头疼maven的项目管理的,因为觉得用起来还是没有那么方便的啊,不过今天我自己算是小弄了下maven项目的故那里,是一个同事在配置maven的项目,我去凑了下热闹而已,现在自己 ...
- mysql root设置密码 linux
成功方案 mysqld_safe --user=mysql --skip-grant-tables --skip-networking & [root@localhost ~]# mysql ...
- jzoj2701 【GDKOI2012模拟02.01】矩阵
传送门:https://jzoj.net/senior/#main/show/2701 [题目大意] 给出矩阵A,求矩阵B,使得