题目大意:给出一棵树和若干条可以加入的边,要求加入若干条边使图是仙人掌并且加入的边权和最大,仙人掌定义为没有一个点属于超过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. 201621123062《java程序设计》第13周作业总结

    1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 思维导图: 2. 为你的系统增加网络功能(购物车.图书馆管理.斗地主等)-分组完成 为了让你的系统可以被多 ...

  2. 随机ID添加

    var http = require("http"); var fs = require("fs"); var server = http.createServ ...

  3. vue2.X简单翻页/分页

    由于业务需要 公司把后台所有数据一次性给前端,数据过多,所以前端需要做一些分页的处理,比较简单的翻页. html代码 <table class="three_td"> ...

  4. 个人技术博客(alpha)

    APP的权限校验不同于web网页端,web一般使用session记录用户的状态信息,而app则使用token令牌来记录用户信息.有这样一个场景,系统的数据量达到千万级,需要几台服务器部署,当一个用户在 ...

  5. RAID6三块硬盘离线导致的数据丢失恢复过程

    小编我最近参与了一例非常成功的数据恢复的案例,在这里分享给大家.用户是一组6块750G磁盘的 RAID6,先后有两块磁盘离线,但维护人员在此情况下依然没有更换磁盘,所以在第三块硬盘离线后raid直接崩 ...

  6. 学习UI的总结

    学习前端有一段时间了,一直在看书上的理论知识,而实战项目却很少.老师常说,想要知道自己的实力有多少,知识掌握了多少,最好的方法就是去实践了,实践出真知嘛.于是在学习中,总要是通过项目的实践以及理论知识 ...

  7. 新概念英语(1-141)Sally's first train ride

    Lesson 141 Sally's first train ride 萨莉第一交乘火车旅行 Listen to the tape then answer this question. Why was ...

  8. HTTP协议的消息头:Content-Type和Accept的作用

    一.背景知识 1.概述 Http报头分为通用报头,请求报头,响应报头和实体报头. 请求方的http报头结构:通用报头|请求报头|实体报头 响应方的http报头结构:通用报头|响应报头|实体报头 Acc ...

  9. 新概念英语(1-119)who call out to the thieves in the dark?

    who call out to the thieves in the dark? A true story Do you like stories? I want to tell you a true ...

  10. mybatis配置多数据源(利用spring的AbstractRoutingDataSource)

    主要是利用了spring的AbstractRoutingDataSource. 直接上配置了: spring-mybatis.xml <bean name="dataSource&qu ...