洛谷 P6419 [COCI 2014/2015 #1] Kamp
前言:
题解
定义
定义
定义
定义
那么答案就是
//#define FILE_INPUT
#include <iomanip>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
bool Mbe;
#define rep(i, a, b) for (int i = a, END##i = b; i <= END##i; i++)
#define per(i, a, b) for (int i = a, END##i = b; i >= END##i; i--)
#define DEBUG(x) cerr << #x << " = " << x << '\n'
using LL = long long;
using ULL = unsigned long long;
inline LL read() {
LL s = 0, fu = 1; char ch = getchar();
while (ch < '0' || ch > '9') ch == '-' ? fu = -1 : 0, ch = getchar();
while (ch >= '0' && ch <= '9') s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
return s * fu;
}
const int Mod = 1e9 + 7;
const int Inf = 0x3f3f3f3f;
const LL InfLL = 0x3f3f3f3f3f3f3f3f;
const int N = 5e5 + 10;
int head[N], nxt[N << 1], to[N << 1], w[N << 1], idx;
void add_edge(int a, int b, int c) {
to[++idx] = b, w[idx] = c, nxt[idx] = head[a], head[a] = idx;
}
LL f[N], g[N], h1[N], up[N], n, K, h2[N], sz[N];
bool mp[N];
void dp1(int u, int fa) {
sz[u] = mp[u];
for (int i = head[u]; i; i = nxt[i]) {
int v = to[i];
if (v == fa) continue;
dp1(v, u);
sz[u] += sz[v];
if (sz[v]) {
f[u] += f[v] + 2 * w[i];
int val = w[i] + h1[v];
if (h1[u] <= val) h2[u] = h1[u], h1[u] = val;
else if (h2[u] < val) h2[u] = val;
}
}
}
void dp2(int u, int fa) {
for (int i = head[u]; i; i = nxt[i]) {
int v = to[i];
if (v == fa) continue;
if (K - sz[v]) {
g[v] = g[u] + f[u] - f[v];
if (!sz[v]) g[v] += 2 * w[i];
if (h1[v] + w[i] != h1[u]) up[v] = max(up[u], h1[u]) + w[i];
else up[v] = max(up[u], h2[u]) + w[i];
}
dp2(v, u);
}
}
void Init() {
}
void Solve() {
n = read(), K = read();
rep(i, 1, n - 1) {
int a = read(), b = read(), c = read();
add_edge(a, b, c), add_edge(b, a, c);
}
rep(i, 1, K) mp[read()] = 1;
dp1(1, 0);
dp2(1, 0);
rep(i, 1, n) printf("%ll;d\n", g[i] + f[i] - max(up[i], h1[i]));
}
bool Med;
signed main() {
#ifdef FILE_INPUT
freopen("input.in", "r", stdin);
#endif
int T = 1;
// T = read();
while (T--) {
Init();
Solve();
}
cerr << "Memory: " << fixed << setprecision(3) << (&Mbe - &Med) / 1048576.0 << " MB\n";
cerr << "Time: " << fixed << setprecision(3) << 1e3 * clock() / CLOCKS_PER_SEC << " ms\n";
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!