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

设三元组(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. [POJ1936]All in All

    [POJ1936]All in All 试题描述 You have devised a new encryption technique which encodes a message by inse ...

  2. thinkphp 前台html调用函数 格式化输出

    仅仅是输出变量并不能满足模板输出的需要,内置模板引擎支持对模板变量使用调节器和格式化功能,其实也就是提供函数支持,并支持多个函数同时使用.用于模板标签的函数可以是PHP内置函数或者是用户自定义函数,和 ...

  3. 利用cocostudio库函数 实现左右滑动的背包栏UI (cocos2d-x 2.2.0)

    .h #ifndef __COMMON_COMPONENTS__ #define __COMMON_COMPONENTS__ #include "cocos2d.h" #inclu ...

  4. Apple Swift中英文开发资源集锦[apple swift resources]

    找到的一些Apple Swift中英文资源原文链接,希望对大家有所帮助.欢迎大家补充,原始资源链接最好! The Swift Programming Language https://develope ...

  5. Sybase IQ导出文件的几种方式

    IQ有四种方法,将表的数据导出为文本文件:1.重定向 SELECT * FROM TABLE1 ># D:MYDATATABLE1.TXT -- 文件生成在执行语句的客户端上 2.通过选项导出 ...

  6. iOS 基于UIWebView的应用特点

    现在有许多ios应用都是基于UIWebView的应用,比如phonegap做出的那些程序.最近我也接触到了一个类似的程序,今天在这里分享一下这类程序的特点. 用UIWebView来实现程序,自然少不了 ...

  7. PHP负数判空

    2014年11月6日 10:08:09 $a = -1; $b = '-1'; $c = empty($a); $d = empty($b); var_dump($c, $d); // bool(fa ...

  8. Sql Server 深入的探讨锁机制

    一: 当select遇到性能低下的update会怎么样? 1. 还是使用原始的person表,插入6条数据,由于是4000字节,所以两条数据就是一个数据页,如下图: 1 DROP TABLE dbo. ...

  9. CodeForces - 407A

    Triangle Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Sta ...

  10. LightOJ 1315 - Game of Hyper Knights(博弈sg函数)

    G - Game of Hyper Knights Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & ...