题意
题目里有形式化。
解法
我们把最优路径上每条边走了多少次给画出来,这是样例的第二组询问,因为第一组啥都看不出来。
我们先将最小的能包含所有关键点的一棵树抠出来,也就是:
把起点到终点的路径在这棵树上的部分标出来。
再把每条边的经过次数贴上去之后,发现答案就是抠出来的树的边权值和减去红色路径权值和再乘以二加上起点到终点路径的权值和,画一下也是很好理解的。
然后就没了吗?
给样例最后一组数据扣下来,发现答案少了,因为可能存在路径与抠出来的树完全不交的情况,这种情况加上终点到抠出来的树的路径上的权值和也就是红色这一部分:
然后就没了,写法有很多种,写出来时间从 到 都有,反正都能过,这里贴一种很傻子的 做法和优化之后的 大常做法数。
代码
// 39 X 03
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
struct Edge {
int x, c;
bool operator < ( const Edge &i ) const {
return x < i.x || x == i.x && c < i.c;
}
};
int n, q, k, val, cnt, a[N], mark[N], dep[N], top[N], fat[N], hvy[N], siz[N], dep1[N], fat1[N], hvy1[N], siz1[N], top1[N], quan[N][2], t[N], dfs[N];
set<Edge> g[N], nbr[N];
void add ( int x, int k ) {
for (; x <= cnt; x += (x & -x)) {
t[x] += k;
}
}
int ask ( int x ) {
int res = 0;
for (; x; x -= (x & -x)) {
res += t[x];
}
return res;
}
void s1 ( int x, int fa ) {
dep[x] = dep[fa] + 1, fat[x] = fa, siz[x] = 1;
for (auto to : g[x]) {
if (to.x ^ fa) {
quan[to.x][0] = quan[x][0] + to.c, s1 (to.x, x);
siz[x] += siz[to.x];
if (siz[to.x] > siz[hvy[x]]) {
hvy[x] = to.x;
}
}
}
}
void s2 ( int x, int fa, int topx ) {
top[x] = topx, dfs[x] = ++cnt;
if (hvy[x]) s2 (hvy[x], x, topx);
for (auto to : g[x]) {
if (to.x ^ fa && to.x ^ hvy[x]) {
s2 (to.x, x, to.x);
}
}
}
void s3 ( int x, int fa ) {
dep1[x] = dep1[fa] + 1, fat1[x] = fa, siz1[x] = 1;
for (auto to : nbr[x]) {
if (to.x ^ fa) {
quan[to.x][1] = quan[x][1] + to.c, s3 (to.x, x);
siz1[x] += siz1[to.x];
if (siz1[to.x] > siz1[hvy1[x]]) {
hvy1[x] = to.x;
}
}
}
}
void s4 ( int x, int fa, int topx ) {
top1[x] = topx;
if (hvy1[x]) s4 (hvy1[x], x, topx);
for (auto to : nbr[x]) {
if (to.x ^ fa && to.x ^ hvy1[x]) {
s4 (to.x, x, to.x);
}
}
}
void s ( int x, int fa ) {
for (auto to : g[x]) {
if (to.x ^ fa) {
s (to.x, x);
a[x] |= a[to.x];
}
}
}
void s5 ( int x, int fa ) {
mark[x] = 1;
for (auto to : g[x]) {
if (to.x ^ fa && a[to.x]) {
add (dfs[to.x], to.c);
val += to.c, s5 (to.x, x);
}
}
}
int LCA ( int x, int y ) {
while (top[x] ^ top[y]) {
if (dep[top[x]] < dep[top[y]]) swap (x, y);
x = fat[top[x]];
}
return (dep[x] > dep[y] ? y : x);
}
int LCA1 ( int x, int y ) {
while (top1[x] ^ top1[y]) {
if (dep1[top1[x]] < dep1[top1[y]]) swap (x, y);
x = fat1[top1[x]];
}
return (dep1[x] > dep1[y] ? y : x);
}
int dis1 ( int x, int y ) {
return 1ll * quan[x][0] + quan[y][0] - quan[LCA (x, y)][0] * 2;
}
int dis2 ( int x, int y ) {
return 1ll * quan[x][1] + quan[y][1] - quan[LCA1 (x, y)][1] * 2;
}
int Calc ( int x, int y ) {
int res = 0;
while (top[x] ^ top[y]) {
if (dep[top[x]] < dep[top[y]]) swap (x, y);
res += ask (dfs[x]) - ask (dfs[top[x]] - 1), x = fat[top[x]];
}
res += (dep[x] > dep[y] ? ask (dfs[x]) - ask (dfs[y]) : ask (dfs[y]) - ask (dfs[x]));
return res;
}
int main () {
cin >> n >> q >> k;
for (int i = 1, x, y, c; i < n; ++i) {
cin >> x >> y >> c, g[x].insert ({y, c}), g[y].insert ({x, c});
}
s1 (1, 0), s2 (1, 0, 1);
int lca = 0;
for (int i = 1, x; i <= k; ++i) {
cin >> x, a[x] = 1, lca = (lca ? LCA (lca, x) : x);
}
s (1, 0), s5 (lca, fat[lca]), val <<= 1;
for (int i = 1; i <= n; ++i) {
for (auto to : g[i]) {
if (mark[i]) {
if (mark[to.x]) continue;
nbr[n + 1].insert (to), nbr[to.x].insert ({n + 1, to.c});
} else {
if (mark[to.x]) {
nbr[i].insert ({n + 1, to.c});
nbr[n + 1].insert ({i, to.c});
} else {
nbr[i].insert (to), nbr[to.x].insert ({i, to.c});
}
}
}
}
s3 ((a[1] ? n + 1 : 1), 0), s4 ((a[1] ? n + 1 : 1), 0, (a[1] ? n + 1 : 1));
for (int x, y; q--; ) {
cin >> x >> y;
long long res = 1ll * val + dis1 (x, y) + 2 * (!mark[x] && !mark[y]) * dis2 (LCA1 (x, y), n + 1), sub = Calc (x, y) << 1;
cout << res - sub << endl;
}
return 0;
}容易发现 set 与树状数组都是可以忽略掉的。
// 39 X 03
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
struct Edge {
int x, c;
};
int n, q, k, val, cnt, a[N], mark[N], dep[N], top[N], fat[N], hvy[N], siz[N], dep1[N], fat1[N], hvy1[N], siz1[N], top1[N], quan[N][2], t[N], dfs[N];
vector<Edge> g[N], nbr[N];
void s1 ( int x, int fa ) {
dep[x] = dep[fa] + 1, fat[x] = fa, siz[x] = 1;
for (auto to : g[x]) {
if (to.x ^ fa) {
quan[to.x][0] = quan[x][0] + to.c, s1 (to.x, x);
siz[x] += siz[to.x];
if (siz[to.x] > siz[hvy[x]]) {
hvy[x] = to.x;
}
}
}
}
void s2 ( int x, int fa, int topx ) {
top[x] = topx, dfs[x] = ++cnt;
if (hvy[x]) s2 (hvy[x], x, topx);
for (auto to : g[x]) {
if (to.x ^ fa && to.x ^ hvy[x]) {
s2 (to.x, x, to.x);
}
}
}
void s3 ( int x, int fa ) {
if (dep1[x]) return ;
dep1[x] = dep1[fa] + 1, fat1[x] = fa, siz1[x] = 1;
for (auto to : nbr[x]) {
if (to.x ^ fa) {
quan[to.x][1] = quan[x][1] + to.c, s3 (to.x, x);
siz1[x] += siz1[to.x];
if (siz1[to.x] > siz1[hvy1[x]]) {
hvy1[x] = to.x;
}
}
}
}
void s4 ( int x, int fa, int topx ) {
if (top1[x]) return ;
top1[x] = topx;
if (hvy1[x]) s4 (hvy1[x], x, topx);
for (auto to : nbr[x]) {
if (to.x ^ fa && to.x ^ hvy1[x]) {
s4 (to.x, x, to.x);
}
}
}
void s ( int x, int fa ) {
for (auto to : g[x]) {
if (to.x ^ fa) {
s (to.x, x), a[x] |= a[to.x];
}
}
}
void s5 ( int x, int fa ) {
mark[x] = 1;
for (auto to : g[x]) {
if (to.x ^ fa && a[to.x]) {
t[dfs[to.x]] += to.c, val += to.c, s5 (to.x, x);
}
}
}
int LCA ( int x, int y ) {
while (top[x] ^ top[y]) {
if (dep[top[x]] < dep[top[y]]) swap (x, y);
x = fat[top[x]];
}
return (dep[x] > dep[y] ? y : x);
}
int LCA1 ( int x, int y ) {
while (top1[x] ^ top1[y]) {
if (dep1[top1[x]] < dep1[top1[y]]) swap (x, y);
x = fat1[top1[x]];
}
return (dep1[x] > dep1[y] ? y : x);
}
int dis1 ( int x, int y ) {
return 1ll * quan[x][0] + quan[y][0] - quan[LCA (x, y)][0] * 2;
}
int dis2 ( int x, int y ) {
return 1ll * quan[x][1] + quan[y][1] - quan[LCA1 (x, y)][1] * 2;
}
int Calc ( int x, int y ) {
int res = 0;
while (top[x] ^ top[y]) {
if (dep[top[x]] < dep[top[y]]) swap (x, y);
res += t[dfs[x]] - t[dfs[top[x]] - 1], x = fat[top[x]];
}
res += (dep[x] > dep[y] ? t[dfs[x]] - t[dfs[y]] : t[dfs[y]] - t[dfs[x]]);
return res;
}
int main () {
// freopen ("carrier.in", "r", stdin);
// freopen ("carrier.out", "w", stdout);
ios :: sync_with_stdio (0), cin.tie (0);
cin >> n >> q >> k;
for (int i = 1, x, y, c; i < n; ++i) {
cin >> x >> y >> c, g[x].push_back ({y, c}), g[y].push_back ({x, c});
}
s1 (1, 0), s2 (1, 0, 1);
int lca = 0;
for (int i = 1, x; i <= k; ++i) {
cin >> x, a[x] = 1, lca = (lca ? LCA (lca, x) : x);
}
s (1, 0), s5 (lca, fat[lca]), val <<= 1;
for (int i = 1; i <= n; ++i) t[i] += t[i - 1];
for (int i = 1; i <= n; ++i) {
for (auto to : g[i]) {
if (mark[i]) {
if (mark[to.x]) continue;
nbr[n + 1].push_back (to);
} else {
if (mark[to.x]) {
nbr[n + 1].push_back ({i, to.c});
} else {
nbr[i].push_back (to);
}
}
}
}
s3 ((a[1] ? n + 1 : 1), 0), s4 ((a[1] ? n + 1 : 1), 0, (a[1] ? n + 1 : 1));
for (int x, y; q--; ) {
cin >> x >> y;
long long res = 1ll * val + dis1 (x, y) + 2 * (!mark[x] && !mark[y]) * dis2 (LCA1 (x, y), n + 1), sub = Calc (x, y) << 1;
cout << res - sub << '\n';
}
return 0;
}


