可以发现,每个特殊点可以贡献的部分在树上是一条链。

设三元组(v,x,y)表示路径长度,需要更新的端点,与当前点的lca为y。

对于每个节点x,通过两遍树形DP可以求出:

d[x]:x到x子树内的某个特殊点的最优解。

u[x]:x到x子树外的某个特殊点的最优解。

pre[x]:x以及x之前的兄弟的d[]的最优解。

suf[x]:x以及x之后的兄弟的d[]的最优解。

然后在树上打标记,最后dfs一遍统计答案即可。

时间复杂度$O(n)$。

#include<cstdio>
#define N 100010
int n,m,i,x,y,z,vip[N],ans1,ans2;
int g[N],v[N<<1],w[N<<1],nxt[N<<1],ed,dis[N],q[N],t,tag[N],cnt[N];
struct P{
int v,x,y;
P(){v=-1;}
P(int _v,int _x,int _y){v=_v,x=_x,y=_y;}
}d[N],u[N],pre[N],suf[N],fin[N];
inline void read(int&a){char c;while(!(((c=getchar())>='0')&&(c<='9')));a=c-'0';while(((c=getchar())>='0')&&(c<='9'))(a*=10)+=c-'0';}
inline void add(int x,int y,int z){v[++ed]=y;w[ed]=z;nxt[ed]=g[x];g[x]=ed;}
void dfs1(int x,int f){
if(vip[x])d[x]=P(0,x,x);
for(int i=g[x];i;i=nxt[i])if(v[i]!=f){
int y=v[i];dis[y]=w[i];
dfs1(y,x);
if(d[y].v<0)continue;
if(d[y].v+w[i]>d[x].v)d[x]=d[y],d[x].v+=w[i];
else if(d[y].v+w[i]==d[x].v)d[x].x=x;
}
d[x].y=x;
fin[x]=d[x];
}
void dfs2(int x,int f){
if(vip[x]&&u[x].v<0)u[x]=P(0,x,x);
t=0;
for(int i=g[x];i;i=nxt[i])if(v[i]!=f)q[++t]=v[i];
for(int i=1;i<=t;i++){
int y=q[i];
pre[i]=pre[i-1];
if(d[y].v<0)continue;
if(d[y].v+dis[y]>pre[i].v)pre[i]=d[y],pre[i].v+=dis[y];
else if(d[y].v+dis[y]>pre[i].v)pre[i].x=x;
}
suf[t+1]=P();
for(int i=t;i;i--){
int y=q[i];
suf[i]=suf[i+1];
if(d[y].v<0)continue;
if(d[y].v+dis[y]>suf[i].v)suf[i]=d[y],suf[i].v+=dis[y];
else if(d[y].v+dis[y]>suf[i].v)suf[i].x=x;
}
for(int i=1;i<=t;i++){
int y=q[i];
P B=pre[i-1];
if(B.v<suf[i+1].v)B=suf[i+1];
else if(B.v==suf[i+1].v)B.x=x;
B.y=x;
if(B.v<u[x].v)B=u[x];
else if(B.v==u[x].v)B.x=x;
if(~B.v)B.v+=dis[y];
u[y]=B;
if(!vip[y])continue;
if(B.v>fin[y].v)fin[y]=B;
else if(B.v==fin[y].v)fin[y].v=-1;
}
for(int i=g[x];i;i=nxt[i])if(v[i]!=f)dfs2(v[i],x);
}
inline void modify(int x,int y,int z){tag[x]++,tag[y]++,tag[z]-=2,cnt[z]++;}
void dfs3(int x,int f){
for(int i=g[x];i;i=nxt[i])if(v[i]!=f)dfs3(v[i],x),tag[x]+=tag[v[i]];
cnt[x]+=tag[x];
}
int main(){
read(n),read(m);
for(i=1;i<=m;i++)read(x),vip[x]=1;
for(i=1;i<n;i++)read(x),read(y),read(z),add(x,y,z),add(y,x,z);
dfs1(1,0),dfs2(1,0);
for(i=1;i<=n;i++)if(vip[i])if(~fin[i].v)modify(i,fin[i].x,fin[i].y);
dfs3(1,0);
for(i=1;i<=n;i++)if(!vip[i]){
if(cnt[i]>ans1)ans1=cnt[i],ans2=1;
else if(cnt[i]==ans1)ans2++;
}
return printf("%d %d",ans1,ans2),0;
}

  

BZOJ4342 : CF348 Pilgrims的更多相关文章

  1. Codeforces-348E Pilgrims

    #4342. CF348 Pilgrims 此题同UOJ#11 ydc的大树 Online Judge:Bzoj-4342,Codeforces-348E,Luogu-CF348E,Uoj-#11 L ...

  2. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  3. 题解-CF348E Pilgrims

    题面 CF348E Pilgrims 有一棵 \(n\) 个点的 带权 树和 \(m\) 个关键点,要求杀了一个不关键的点,满足最多的关键点到离它最远的所有关键点的路径都被打断.求可以满足的最多关键点 ...

  4. 越狱Season 1- Episode 18: Bluff

    Season 1, Episode 18: Bluff -Michael: Scofield Scofield Michael Scofield Michael Scofield -Patoshik: ...

  5. python瓦登尔湖词频统计

    #瓦登尔湖词频统计: import string path = 'D:/python3/Walden.txt' with open(path,'r',encoding= 'utf-8') as tex ...

  6. 每日英语:Who Ruined The Humanities?

    You've probably heard the baleful reports. The number of college students majoring in the humanities ...

  7. A Child's History of England.28

    By such means, and by taxing and oppressing the English people in every possible way, the Red King b ...

随机推荐

  1. HDFS深入浅析

    导读 Hadoop分布式文件系统(HDFS)被设计成适合运行在通用硬件(commodity hardware)上的分布式文件系统.它和现有的分布式文件系统有很多共同点.但同时,它和其他的分布式文件系统 ...

  2. Unity3D脚印6——模型动画

    原地址:http://www.cnblogs.com/ybgame/archive/2013/02/21/2920009.html 如何导入一个模型,导入的模型在unity3d的project面板中是 ...

  3. [BZOJ1220][POJ1091][HNOI2002]跳蚤

    [BZOJ1220][POJ1091][HNOI2002]跳蚤 试题描述 Z城市居住着很多只跳蚤.在Z城市周六生活频道有一个娱乐节目.一只跳蚤将被请上一个高空钢丝的正中央.钢丝很长,可以看作是无限长. ...

  4. 在pc游览器端模拟移动端幻灯片

    用简单的思路写了下pc端模拟web端的图片滑动效果... 效果卡,bug多,完毕,继续学习c3方法写这个,iscroll就是可以模拟这种效果,还在学习中,难点<触点判断> 代码一份 < ...

  5. Linux Apache prefork和worker的原理详解

    prefork(多进程,每个进程产生子进程)和worker(多进程,每个进程生成多个线程)    prefork的工作原理是,控制进程在最初建立“StartServers”个子进程后,为了满足MinS ...

  6. JS匿名函数的理解

    js匿名函数的代码如下:(function(){ // 这里忽略jQuery 所有实现 })(); 半年前初次接触jQuery 的时候,我也像其他人一样很兴奋地想看看源码是什么样的.然而,在看到源码的 ...

  7. outlook.office365.com传参

    string m_html = string.Empty; m_html += "<script>"; m_html += string.Format("wi ...

  8. 在windows下用cygwin和eclipse搭建cocos2dx的android开发环境

    在windows下用cygwin和eclipse搭建cocos2dx(2.1.4)的android开发环境,2013-8-1更新. 一.准备工作 需要下载和安装以下内容,请根据自己的操作系统选择x86 ...

  9. iOS 保存CGRect,CGPoint到NSArray'的方法

    由于CGRect和CGPoint等对象是Struct,即结构体,不是继承于NSObject的,所以需要先用NSValue的方法,把他们转化成NSValue对象,之后就可以存入NSArray了! @in ...

  10. 昂贵的聘礼(poj 1062)

    Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低 ...