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

设三元组(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. 见招拆招:绕过WAF继续SQL注入常用方法

    Web Hacker总是生存在与WAF的不断抗争之中的,厂商不断过滤,Hacker不断绕过.WAF bypass是一个永恒的话题,不少基友也总结了很多奇技怪招.那今天我在这里做个小小的扫盲吧.先来说说 ...

  2. ZeroMQ(java)中的数据流SessionBase与SocketBase

    前面的文章中已经比较的清楚了ZeroMQ(java)中如何在底层处理IO, 通过StreamEngine对象来维护SelectableChannel对象以及IO的事件回调,然后通过Poller对象来维 ...

  3. Largest Divisible Subset

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  4. 使用kettle转换中的JavaScript对密码进行加密和解密

    日常开发中,为了确保账号和密码的安全,时常要对密码进行加密和解密.然而kettle是怎么对密码进行加密和解密的呢? 下面的代码需要再转换中的JavaScript中运行. var encrypted_p ...

  5. 利用VMware虚拟机(Android-x86 2.2)和eclipse,调试安卓代码

    下载 android-x86-2.2-generic.iso (这里包含eth0)  http://www.android-x86.org/download XP32位 只能使用 VMware Wor ...

  6. Java for LeetCode 169 Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  7. 猪八戒吃西瓜(wmelon)-排序-查找

    问题 A: 猪八戒吃西瓜(wmelon) 时间限制: 1 Sec  内存限制: 64 MB提交: 30  解决: 14[提交][状态][讨论版] 题目描述 有一天,贪吃的猪八戒来到了一个大果园,果园里 ...

  8. 【leetcode】clone-graph

    写在前面的话: 看了看自己的博客,从一月底开始就没怎么更新过,我也确实将近5个月没怎么写代码了.今天突然觉得有些心慌,感觉手都已经生疏了.果然,随便找了道题就卡住了.隐约感觉要用map但又不太记得用法 ...

  9. HDU1004 查气球

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  10. 今天连续几次被其他电脑客户端踢下线,也不知是否是ip冲突

    可能是利用ip然后我的网络也没有关掉共享,其次就是远程开启了一个叫做"学生端"的应用,好在我改密码连续两次改的快