[Codeforces]856D - Masha and Cactus
题目大意:给出一棵树和若干条可以加入的边,要求加入若干条边使图是仙人掌并且加入的边权和最大,仙人掌定义为没有一个点属于超过1个环。(n,m<=200,000)
做法:这题的仙人掌跟平时见到的不太一样,是以点为判定条件,比较坑……发现与选若干条不相交的链权值和最大等价,dp,f[i]表示i的子树内最大权值和,每次枚举一条lca为i的链转移,需要求出这条链以外的子树的dp值,树剖后线段树维护每个点轻儿子dp值之和,重儿子的暴力算一算就可以了,复杂度O((n+m)log^2)。
代码:
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
inline int read()
{
int x;char c;
while((c=getchar())<''||c>'');
for(x=c-'';(c=getchar())>=''&&c<='';)x=x*+c-'';
return x;
}
#define MN 200000
#define N 262144
#define mp(x,y) make_pair(x,y)
struct edge{int nx,t;}e[MN+];
int h[MN+],en,s[MN+],mx[MN+],d[MN+],fa[MN+],f[MN+],l[MN+],p[MN+],cnt,t[N*+],F[MN+];
vector<pair<pair<int,int>,int> > v[MN+];
inline void ins(int x,int y){e[++en]=(edge){h[x],y};h[x]=en;}
void add(int k,int x){for(k+=N;k;k>>=)t[k]+=x;}
int query(int l,int r)
{
int res=;
if(l<=r)for(l+=N-,r+=N+;l^r^;l>>=,r>>=)
{
if(~l&)res+=t[l+];
if( r&)res+=t[r-];
}
return res;
}
void dfs(int x)
{
s[x]=;
for(int i=h[x];i;i=e[i].nx)
{
fa[e[i].t]=x;d[e[i].t]=d[x]+;
dfs(e[i].t);
s[x]+=s[e[i].t];
if(s[e[i].t]>s[mx[x]])mx[x]=e[i].t;
}
}
void build(int x)
{
p[l[x]=++cnt]=x;
if(mx[x])f[mx[x]]=f[x],build(mx[x]);
for(int i=h[x];i;i=e[i].nx)if(e[i].t!=mx[x])build(f[e[i].t]=e[i].t);
}
int lca(int x,int y)
{
while(f[x]!=f[y])if(d[f[x]]>d[f[y]])x=fa[f[x]];else y=fa[f[y]];
return d[x]<d[y]?x:y;
}
int up(int x,int y)
{
for(;f[x]!=f[y];x=fa[f[x]])
if(fa[f[x]]==y)return f[x];
return p[l[y]+];
}
int Q(int x,int y)
{
int res=;
for(;f[x]!=f[y];x=fa[f[x]])
res+=query(l[f[x]],l[x])+F[mx[x]]-F[f[x]];
return res+query(l[y],l[x])+F[mx[x]];
}
void dp(int x)
{
for(int i=h[x];i;i=e[i].nx)dp(e[i].t),F[x]+=F[e[i].t];
add(l[x],F[x]-F[mx[x]]);
for(int i=;i<v[x].size();++i)
{
int y=up(v[x][i].first.first,x);
F[x]=max(F[x],Q(v[x][i].first.first,y)+Q(v[x][i].first.second,x)-F[y]+v[x][i].second);
}
}
int main()
{
int n,m,i,x,y;
n=read();m=read();
for(i=;i<=n;++i)ins(read(),i);
dfs();build(f[]=);
while(m--)
{
i=lca(x=read(),y=read());
if(x==i)swap(x,y);
v[i].push_back(mp(mp(x,y),read()));
}
dp();
printf("%d",F[]);
}
[Codeforces]856D - Masha and Cactus的更多相关文章
- Codeforces 856D - Masha and Cactus(树链剖分优化 dp)
题面传送门 题意: 给你一棵 \(n\) 个顶点的树和 \(m\) 条带权值的附加边 你要选择一些附加边加入原树中使其成为一个仙人掌(每个点最多属于 \(1\) 个简单环) 求你选择的附加边权值之和的 ...
- CodeForces - 907A Masha and Bears
A. Masha and Bears time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 2017 Russian Code Cup (RCC 17), Final Round
2017 Russian Code Cup (RCC 17), Final Round A Set Theory 思路:原题转换一下就是找一个b数组,使得b数组任意两个数的差值都和a数组任意两个数的差 ...
- Codeforces Round #524 (Div. 2) C. Masha and two friends(矩形相交)
C. Masha and two friends time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Codeforces Round #143 (Div. 2) E. Cactus 无向图缩环+LCA
E. Cactus A connected undirected graph is called a vertex cactus, if each vertex of this graph bel ...
- Codeforces 231E - Cactus
231E - Cactus 给一个10^5个点的无向图,每个点最多属于一个环,规定两点之间的简单路:从起点到终点,经过的边不重复 给10^5个询问,每个询问两个点,问这两个点之间有多少条简单路. 挺综 ...
- codeforces 887B Cubes for Masha 两种暴力
B. Cubes for Masha time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces Round #524 (Div. 2) C. Masha and two friends
C. Masha and two friends 题目链接:https://codeforc.es/contest/1080/problem/C 题意: 给出一个黑白相间的n*m的矩阵,现在先对一个子 ...
- CodeForces - 789B B. Masha and geometric depression---(水坑 分类讨论)
CodeForces - 789B 当时题意理解的有点偏差,一直wa在了14组.是q等于0的时候,b1的绝对值大于l的时候,当b1的绝对值大于l的时候就应该直接终端掉,不应该管后面的0的. 题意告诉你 ...
随机推荐
- C语言第六次博客作业--数据类型
一.PTA实验作业 题目1:区位码输入法 1. 本题PTA提交列表 2. 设计思路 (1)定义整型变量code放区位码,areacode放区码,digitcode放位码,one放个位数,two放十位数 ...
- C语言博客作业—结构体
一.PTA实验作业 题目1:结构体数组按总分排序 1. 本题PTA提交列表 2. 设计思路 void calc //函数calc求出p指针所指的结构体数组中 n 名学生各自的总分 { 定义循环变量i: ...
- alpha-咸鱼冲刺day1-紫仪
总汇链接 一,合照 emmmmm.自然是没有的. 二,项目燃尽图 三,项目进展 登陆界面随意写了一下.(明天用来做测试的) 把学姐给我的模板改成了自家的个人主页界面,侧边栏啥的都弄出来了(快撒花花 ...
- choose the max from numbers, use scanf and if else (v1:21.9.2017,v2:23.9.2017)
#include<stdio.h> int main(){ int a,b,c,max; printf("请输入一个数值: "); scanf("%d&quo ...
- Python科学计算(一)
作者 J.R. Johansson (robert@riken.jp) http://dml.riken.jp/~rob/ 最新版本的 IPython notebook 课程文件 http://git ...
- selenium 爬虫
from selenium import webdriver import time driver = webdriver.PhantomJS(executable_path="D:/pha ...
- 不看就亏了:DELL EqualLogic PS6100详解及数据恢办法
DELL EqualLogic PS6100采用虚拟ISCSI SAN阵列,为远程或分支办公室.部门和中小企业存储部署带来企业级功能.智能化.自动化和可靠性,支持VMware.Solaris.Linu ...
- 不高兴的小名 nyoj
不高兴的小明 时间限制:3000 ms | 内存限制:65535 KB 难度:1 描述 小明又出问题了.妈妈认为聪明的小明应该更加用功学习而变的更加厉害,所以小明除了上学之外,还要参加妈 ...
- httpClient 中的post或者get请求
httpClient相对于java自带的请求功能更加强大,下面就以例子的形式给出: //HttpClient Get请求 private static void register() { try { ...
- 【深度学习】深入理解Batch Normalization批标准化
这几天面试经常被问到BN层的原理,虽然回答上来了,但还是感觉答得不是很好,今天仔细研究了一下Batch Normalization的原理,以下为参考网上几篇文章总结得出. Batch Normaliz ...