You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.5 KiB

2 years ago
#include <bits/stdc++.h>
#define ll long long
using namespace std;
inline ll read() {
char c = getchar();
ll a = 0, b = 1;
for (; c < '0' || c > '9'; c = getchar())
if (c == '-') b = -1;
for (; c >= '0' && c <= '9'; c = getchar()) a = a * 10 + c - 48;
return a * b;
}
struct edge {
ll next, to, v;
} e[1000501];
ll head[1000501], tot, n, k, vis[1000501], f[1000001], ans[1000001], sum[1000001];
ll len[1000501], slen[1000501], dis[1000501], sdis[1000501]; // ·Ö±ðÊÇÀëi×îÔ¶µÄµãºÍ´ÎÔ¶µÄµãµÄ¾àÀ룬ÒÔ¼°ÕâÌõÁ´µÄµÚÒ»¸öµãÔÚÄÄÀï
ll up[1000501];
inline void add(ll i, ll j, ll k) {
e[++tot].next = head[i];
e[tot].to = j;
e[tot].v = k;
head[i] = tot;
}
void dfsfirst(ll x, ll fa) {
if (vis[x]) sum[x]++;
for (ll i = head[x]; i != 0; i = e[i].next) {
ll u = e[i].to;
if (u == fa) continue;
dfsfirst(u, x);
if (sum[u]) {
ll now = len[u] + e[i].v;
f[x] += f[u] + e[i].v * 2;
if (len[x] < now)
sdis[x] = dis[x],
dis[x] = u,
slen[x] = len[x],
len[x] = now;
else if (slen[x] < now)
sdis[x] = u,
slen[x] = now;
}
sum[x] += sum[u];
}
}
void dfsecond(ll x, ll fa) // ¸üÐÂÈ«¾Ö´ð°¸
{
for (ll i = head[x]; i != 0; i = e[i].next) {
ll u = e[i].to;
if (u == fa) continue;
if (sum[u] == 0) {
ans[u] = ans[x] + e[i].v * 2;
up[u] = max(up[x], len[x]) + e[i].v;
} else if (sum[u] == k) {
ans[u] = f[u];
up[u] = 0;
} else {
ans[u] = ans[x];
if (dis[x] == u)
up[u] = max(up[x], slen[x]) + e[i].v; // Ϊʲô´Î³¤¾Í²»»á¾­¹ý,ÒòΪÎÒÉÏÃæÃ¿Ìõ¶¼Ö»È¡ÁËÒ»Ìõ×î´óÖµ°¡¡£¡£¡£
else
up[u] = max(up[x], len[x]) + e[i].v;
}
dfsecond(u, x);
}
}
int main() {
n = read();
k = read();
for (ll i = 1; i < n; i++) {
ll a = read(), b = read(), c = read();
add(a, b, c);
add(b, a, c);
}
for (ll i = 1; i <= k; i++) {
vis[read()] = 1;
}
dfsfirst(1, 0);
ans[1] = f[1];
up[1] = 0;
dfsecond(1, 0);
for (ll i = 1; i <= n; i++) {
cout << ans[i] - max(up[i], len[i]) << endl;
}
return 0;
}