题目大意:给出一棵树和若干条可以加入的边,要求加入若干条边使图是仙人掌并且加入的边权和最大,仙人掌定义为没有一个点属于超过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的更多相关文章

  1. Codeforces 856D - Masha and Cactus(树链剖分优化 dp)

    题面传送门 题意: 给你一棵 \(n\) 个顶点的树和 \(m\) 条带权值的附加边 你要选择一些附加边加入原树中使其成为一个仙人掌(每个点最多属于 \(1\) 个简单环) 求你选择的附加边权值之和的 ...

  2. CodeForces - 907A Masha and Bears

    A. Masha and Bears time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. 2017 Russian Code Cup (RCC 17), Final Round

    2017 Russian Code Cup (RCC 17), Final Round A Set Theory 思路:原题转换一下就是找一个b数组,使得b数组任意两个数的差值都和a数组任意两个数的差 ...

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

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

  6. Codeforces 231E - Cactus

    231E - Cactus 给一个10^5个点的无向图,每个点最多属于一个环,规定两点之间的简单路:从起点到终点,经过的边不重复 给10^5个询问,每个询问两个点,问这两个点之间有多少条简单路. 挺综 ...

  7. codeforces 887B Cubes for Masha 两种暴力

    B. Cubes for Masha time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  8. Codeforces Round #524 (Div. 2) C. Masha and two friends

    C. Masha and two friends 题目链接:https://codeforc.es/contest/1080/problem/C 题意: 给出一个黑白相间的n*m的矩阵,现在先对一个子 ...

  9. CodeForces - 789B B. Masha and geometric depression---(水坑 分类讨论)

    CodeForces - 789B 当时题意理解的有点偏差,一直wa在了14组.是q等于0的时候,b1的绝对值大于l的时候,当b1的绝对值大于l的时候就应该直接终端掉,不应该管后面的0的. 题意告诉你 ...

随机推荐

  1. [福大软工] W班 软件产品案例分析

    作业要求 https://edu.cnblogs.com/campus/fzu/FZUSoftwareEngineering1715W/homework/1300 评分细则 第一部分 调研,评测 (3 ...

  2. Mysql5.7.17中文乱码问题

    写Java web调数据库,老是出现汉字乱码,一直没理睬,今天决定好好"整治"一下,却发现并没有那么简单.从网上找的方法,大部分都尝试了一遍都有一些问题. 有的改完了,数据库启动不 ...

  3. 2018年3月份的PTA(一)

    写程序证明p++等价于(p)++还是等价于(p++)? 由程序说明p++等价于(p)++,因为(p++)在程序中是没有地址的,而输出中p++和(p)++的地址不同是由于在线C语言开发环境地址是动态的 ...

  4. Java的HelloWorld程序

    Java的HelloWorld程序 1.新建文本文档,编写HelloWorld程序,最后保存时记得保存成.java格式 2.在D盘新建一个HelloJava文件夹用于保存java程序 3.使用WIN+ ...

  5. iOS中CocoaPods的安装及错误详解

    什么是CocoaPods CocoaPods是OS X和iOS下的一个第三类库管理工具,通过CocoaPods工具我们可以为项目添加被称为"Pods"的依赖库(这些类库必须是Coc ...

  6. oc中protocol、category和继承的区别

    OC中protocol.category和继承的区别以前还是有点迷糊,面试的时候说的有点混乱,现在结合一些资料总结一下. 利用继承,多态是一个很好的保持"对扩展开放.对更改封闭"( ...

  7. [USACO13JAN] Seating

    https://www.luogu.org/problem/show?pid=3071 题目描述 To earn some extra money, the cows have opened a re ...

  8. 使用 HttpClient 请求 Web Api

    1.获取 post 请求 body 内容 [HttpPost] public string GetId() { //如果方法参数里面有 [FromBody],则需要重新调整内容指针,再进行读取. // ...

  9. 开发一个http代理服务器

    参考链接: http://www.cnblogs.com/jivi/archive/2013/03/10/2952860.html https://www.2cto.com/kf/201405/297 ...

  10. ASP.NET CORE系列【二】使用Entity Framework Core进行增删改查

    介绍 EntityFrameworkCore EF core 是一个轻量级的,可扩展的EF的跨平台版本.对于EF而言 EF core 包含许多提升和新特性,同时 EF core 是一个全新的代码库,并 ...