其他分享
首页 > 其他分享> > 题解 P6419 【[COCI2014-2015#1] Kamp】

题解 P6419 【[COCI2014-2015#1] Kamp】

作者:互联网

题解 - P6419\mathrm{P6419}P6419

题目意思

Sol\mathrm{Sol}Sol

Code\mathrm{Code}Code

#include <bits/stdc++.h>
#define For(i,a,b) for ( int i=(a);i<=(b);i++ )
#define Dow(i,b,a) for ( int i=(b);i>=(a);i-- )
#define GO(i,x) for ( int i=head[x];i;i=e[i].nex )
#define mem(x,s) memset(x,s,sizeof(x))
#define cpy(x,s) memcpy(x,s,sizeof(x))
#define YES return puts("YES"),0
#define NO return puts("NO"),0
#define GG return puts("-1"),0
#define pb push_back
#define int long long
using namespace std;

inline int read()
{
	int sum=0,ff=1; char ch=getchar();
	while(!isdigit(ch))
	{
		if(ch=='-') ff=-1;
		ch=getchar();
	}
	while(isdigit(ch))
		sum=sum*10+(ch^48),ch=getchar();
	return sum*ff;
}

const int mod=1e9+7;
const int mo=998244353;
const int N=5e5+5;

int n,m,cnt,head[N],f[N],g[N];
int dis[N][2],a[N],up[N],siz[N],ans;

struct Node
{
	int nex,to,w;
};
Node e[N<<1];

inline void jia(int u,int v,int w)
{
	e[++cnt].nex=head[u];
	head[u]=cnt;
	e[cnt].to=v;
	e[cnt].w=w;
}

inline void dfs(int u,int fa)
{
	siz[u]=a[u];
	GO(i,u)
	{
		int v=e[i].to;
		if(v==fa) continue;
		dfs(v,u);
		siz[u]+=siz[v];
		if(siz[v])
		{
			f[u]+=(f[v]+2*e[i].w);
			if(dis[u][0]<dis[v][0]+e[i].w) 
				dis[u][1]=dis[u][0],dis[u][0]=dis[v][0]+e[i].w;
			else if(dis[u][1]<dis[v][0]+e[i].w) dis[u][1]=dis[v][0]+e[i].w;
		}
	}
}

inline void dp(int u,int fa)
{
	GO(i,u)
	{
		int v=e[i].to;
		if(v==fa) continue;
		if(m-siz[v])
		{
			g[v]=g[u]+(f[u]-f[v]);
			if(!siz[v]) g[v]+=2*e[i].w; 
			if(dis[v][0]+e[i].w==dis[u][0]) 
				up[v]=max(up[u],dis[u][1])+e[i].w;
			else up[v]=max(up[u],dis[u][0])+e[i].w;
		}
		dp(v,u);
	}
}

signed main()
{
	n=read();m=read();
	For(i,1,n-1)
	{
		int x,y,w;
		x=read(),y=read(),w=read();
		jia(x,y,w);
		jia(y,x,w);
	}
	For(i,1,m) a[read()]++;
	dfs(1,0);
	dp(1,0);
	For(i,1,n) printf("%lld\n",f[i]+g[i]-max(up[i],dis[i][0]));
	return 0;
}

标签:ch,fu,int,题解,sum,COCI2014,uuu,2015,define
来源: https://blog.csdn.net/wangyiyang2/article/details/106960723